0

I need some advice on how to echo the input from the user. A bit of background on the program that this is needed first. I created this program so that it asks the user how many values they would like to enter and then they enter the number in positive integers. Then the program computes to figure out if the numbers are even, odd, or zero and displays said information. I'm stuck on how I can create something that can output all of the enter values on the same line. For example, if the user chooses to enter in 4 values, being 1,2,3, and 4, with my current program it will read, Enter a number on one line and the next would be the number 1. Then it would ask to enter a number again and on another line the number 2. When I want it to read, The values you entered are 1,2,3,4. I'm confused as to how it works to display all the input on one line with a call by reference. Any advice or explanation would be greatly appreciated! Code below

#include<iostream>
using namespace std;
void initialize(); //Function declarations
void get_number(int x);
void classify_number(int, int& zero_count, int& even_count, int& odd_count);
void print_results();
int N; //The number of values that the user wants to enter
//variables declared so that all functions can access it
int even_count;
int odd_count;
int zero_count;
int main()
{
    cout << "Enter number of values to be read, then press return.\n"; //Prompting the user for how many values they will want to input
    cin >> N;
    get_number(N);
    return 0;
}
void initialize() //This function is making sure that all the variables being used are initialized
{

    even_count = 0;
    odd_count = 0;
    zero_count = 0;

}
void get_number(int N) //This function is getting the input from the user and then calling upon the previous function
{

    int count = 0;
    int x;

    initialize(); //Calling upon the previous function to uses the variables
    do {
        cout << "Enter a positive whole number, then press return.\n";
        cin >> x; //The number the user enters
      
        //call the funtion and increment their count
        classify_number(x, zero_count, even_count, odd_count); //Calling upon the function classify to update
        count++;

    } while (count < N);
    //then print the count
    print_results(); //Calling upon the print function

}
void classify_number(int x, int& zero_count, int& even_count, int& odd_count) //This function determines if it's an even,odd, or zero
{

    if (x == 0)
        zero_count++;

    else if (x % 2 == 0)
        even_count++;

    else
        odd_count++;
}
void print_results() //This is printing the results on the screen of the number of even, odds, and zeros.
{

    cout << "There are " << even_count << " number of evens.\n";
    cout << "There are " << zero_count << " number of zeros.\n";
    cout << "There are " << odd_count << " number of odds.\n";
}
zane5
  • 13
  • 2
  • You're going to need some kind of container to store the values of the numbers the user inputs until such time as you can call `print_results`. I would suggest familiarizing yourself with the `std::vector` to start with although in this case a `std::list` would also be appropriate. https://en.cppreference.com/w/cpp/container/vector – Nathan Pierson Oct 16 '20 at 03:56

2 Answers2

0

You could simply use an array of integers.

#include<iostream>

int main(int argc, char *argv[]){
    /* Declare an array great enough to store all possible user values. */
    /* Or use dynamic memory allocation when you are more experienced. */
    int my_array[16];
    int my_array_length = sizeof my_array / sizeof my_array[0];
    /* As your code only uses positive integers, we use this information */
    /* to our advantage and initialize our array with negative numbers. */
    for(int i = 0; i < my_array_length; i++){
        my_array[i] = -1;
    }
    
    /* Here is your own input program routine. I'll use some example values. */
    for(int i = 0; i < my_array_length; i++){
        if(i > 4){
            break;
        }
        my_array[i] = i;
    }
    
    /* Here is your output program routine. */
    for(int i = 0; i < my_array_length; i++){
        if(my_array[i] == -1){
            break;
        }
        std::cout << my_array[i] << std::endl;
    }
    
    return 0;
}

Or you could just count the amount of inputs in the first place.

paladin
  • 765
  • 6
  • 13
0

I believe that this much code will be sufficient :

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::size_t n;
    std::cout << "Enter number of elements : ";
    std::cin >> n;

    std::vector<int> v(n);
    std::cout << "Enter the numbers now : ";
    for (auto &i : v) std::cin >> i;

    std::cout << "The values you entered are : [ ";
    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, ","));
    std::cout << "\b ]\n";

    auto odd  = std::count_if(v.begin(), v.end(), [](auto i) { return i % 2; });
    auto zero = std::count(v.begin(), v.end(), 0);
    auto even = v.size() - (odd + zero);

    std::cout << "There are " << even << " even number(s).\n"
              << "There are " << zero << " zero(s).\n"
              << "There are " << odd << " odd number(s). \n";
}

Sample Run :

Enter number of elements : 6
Enter the numbers now : 0 1 2 3 4 6
The values you entered are : [ 0,1,2,3,4,6 ]
There are 3 even number(s).
There are 1 zero(s).
There are 2 odd number(s).

Ref. :

  1. std::vector

  2. Range-based for loop

  3. How to print out the contents of a vector?

  4. std::count, std::count_if

brc-dd
  • 10,788
  • 3
  • 47
  • 67