1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Program Bug: Programming Class

  1. Apr 13, 2016 #1
    1. The problem statement, all variables and given/known data
    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
    2. Relevant equations
    Code (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;
    }
    3. The attempt at a solution
     
    Last edited by a moderator: Apr 14, 2016
  2. jcsd
  3. Apr 13, 2016 #2
    Additionally, I'd add I'm setting up to use the Sieve of Eratosthenes.
     
  4. Apr 14, 2016 #3

    Mark44

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Program Bug: Programming Class
  1. The program R (Replies: 2)

  2. MARIE Programming (Replies: 1)

  3. Programming question (Replies: 6)

  4. Matlab Programming (Replies: 5)

Loading...