Why Am I Getting Two Blocks of Zeroes in My Console Output?

Click For Summary
SUMMARY

The forum discussion addresses a programming issue where the user receives two blocks of zeroes in their console output due to incorrect function calls and logic errors in their C++ code. The user fails to call the functions createMultipleTwoList() and createIntList() properly, omitting parentheses, which prevents them from executing. Additionally, the createMultipleTwoList() function contains a logic flaw that causes it to return prematurely, resulting in an incomplete list of multiples of two. The discussion highlights the importance of addressing compiler warnings and ensuring proper function syntax.

PREREQUISITES
  • Understanding of C++ syntax and function calls
  • Familiarity with vector data structures in C++
  • Basic knowledge of control flow statements (if-else) in programming
  • Experience with debugging and interpreting compiler warnings
NEXT STEPS
  • Learn proper function syntax in C++ to avoid common pitfalls
  • Explore the use of the Sieve of Eratosthenes for generating prime numbers
  • Study vector manipulation techniques in C++ for efficient data handling
  • Investigate debugging strategies for resolving compiler warnings and errors
USEFUL FOR

Beginner to intermediate C++ programmers, software developers troubleshooting function execution issues, and anyone interested in optimizing their code for generating sequences of numbers.

Prof. 27
Messages
49
Reaction score
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;
        count << "Please enter the number of primes to be printed: ";
        cin >> numberOfPrimes;
        if (numberOfPrimes <= 100 && numberOfPrimes >= 1) {
            createMultipleTwoList;
            createIntList;
            for (i = 0; i <= 100; i++)
            {
                count << multiplesOfTwo[ i] << " ";
            }
            count << endl << endl << endl << endl;
            for (i = 0; i <= 100; i++)
            {
                count << intList[ i] << " ";
            }
            break;
        }
        else {
            count << "Please enter a valid integer" << endl;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
    count << endl;
    system("pause");
    return 0;
}

The Attempt at a Solution

 
Last edited by a moderator:
Physics news on Phys.org
Additionally, I'd add I'm setting up to use the Sieve of Eratosthenes.
 
In the future, please use code tags around your code --
C:
 at the top and
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
5
Views
2K
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 14 ·
Replies
14
Views
5K