如何在 void 函数和 do-while 循环 C++ 中回显输入

How to echo input within a void function and do-while loop C++

我需要一些关于如何回应用户输入的建议。首先需要了解一下该程序的背景知识。我创建了这个程序,以便它询问用户他们想要输入多少个值,然后他们以正整数输入数字。然后程序计算出数字是偶数、奇数还是零并显示所述信息。我坚持如何创建可以在同一行上输出所有输入值的东西。例如,如果用户选择输入 4 个值,即 1、2、3 和 4,我当前的程序将读取,在一行上输入一个数字,下一行是数字 1。然后它会询问再次输入一个数字,在另一行输入数字 2。当我想让它读取时,您输入的值是 1、2、3、4。我对通过引用调用在一行中显示所有输入的工作原理感到困惑。任何建议或解释将不胜感激! 下面的代码

#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";
}

您可以简单地使用一个整数数组。

#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;
}

或者您可以首先计算输入的数量。

我相信这么多代码就足够了:

#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";
}

示例 运行 :

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).

参考。 :

  1. std::vector

  2. Range-based for loop

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

  4. std::count, std::count_if