Program Bug: Programming Class

In summary, the person is having trouble with their code and is asking for help. They have included their code and mentioned using the Sieve of Eratosthenes in the future. The code has a few mistakes, such as functions not being called correctly and a loop not running as intended. The person also received warnings while compiling the code.
  • #1
Prof. 27
50
1

Homework Statement


So, I'm getting two blocks of zeroes in my console output from this program. After much effort (and stack exchange) I'm still unable to get it working. Could someone point me to a solution?
Mod note: Added code tags

Homework Equations


C:
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>

using namespace std;

vector<int> intList(105);
vector<int> multiplesOfTwo(105);

int i;
int z;
int prime = 2;

void createMultipleTwoList()
{
    for (z = 2; z <= 100; z++)
    {
        if (z % 2 == 0)
        {
            multiplesOfTwo.push_back(z);
            return;
        }
        else
        {
            return;
        }
    }
}

void createIntList()
{
    for (i = 1; i <= 100;i++)
    {
        intList.push_back(i);
    }
}

int main()
{
    int number;
    int prime;

    for (;;) {
        int numberOfPrimes;
        cout << "Please enter the number of primes to be printed: ";
        cin >> numberOfPrimes;
        if (numberOfPrimes <= 100 && numberOfPrimes >= 1) {
            createMultipleTwoList;
            createIntList;
            for (i = 0; i <= 100; i++)
            {
                cout << multiplesOfTwo[ i] << " ";
            }
            cout << endl << endl << endl << endl;
            for (i = 0; i <= 100; i++)
            {
                cout << intList[ i] << " ";
            }
            break;
        }
        else {
            cout << "Please enter a valid integer" << endl;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
    cout << endl;
    system("pause");
    return 0;
}

The Attempt at a Solution

 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Additionally, I'd add I'm setting up to use the Sieve of Eratosthenes.
 
  • #3
In the future, please use code tags around your code -- [code=c] at the top and [/code] at the bottom.

There are a lot of problems with the code, as posted. The two functions you defined are not called correctly. To call a function, you must include the parentheses, even for functions with no arguments. When I compiled your code, I got four warnings -- you should pay more attention to warnings.

Your createMultipleTwoList function doesn't work as you intended it to. Both the if clause and the else clause return in the first iteration, so your loop doesn't run more than once.

There could be more problems, but these are the first few I found.
 

1. What is a program bug?

A program bug, also known as a software bug, is an error or flaw in a computer program that causes it to behave unexpectedly or produce incorrect results. These errors can occur due to mistakes in the code, logical errors, or unexpected inputs.

2. How do you identify and fix a program bug?

Identifying and fixing program bugs is a process known as debugging. It involves using tools and techniques such as code reviews, unit testing, and debugging software to locate and correct errors in the code. It may also involve analyzing and understanding the program's logic to identify and fix logical errors.

3. What are some common causes of program bugs?

Program bugs can be caused by a variety of factors, including errors in the code, incorrect assumptions, missing or incorrect data, and external factors such as hardware or network issues. They can also be caused by human error, such as typos or incorrect use of programming language syntax.

4. How can program bugs be prevented?

While it's impossible to completely prevent program bugs, there are steps that can be taken to minimize their occurrence. These include writing clean and well-structured code, using automated testing and debugging tools, and following best practices for software development such as code reviews and continuous integration.

5. Can program bugs have serious consequences?

Yes, program bugs can have serious consequences, especially in critical systems. They can lead to system crashes, data loss, security vulnerabilities, and financial losses. In some cases, program bugs have even caused accidents or disasters. This is why it's important for developers to thoroughly test and debug their code to minimize the risk of serious consequences.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
761
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
846
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top