Hi There how can i make a program that counts the amount of integers a user enters using a loop?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>
int main()
{
  int cnt = 0, sum = 0, value = 0;
    std::cout << "Please enter a set of numbers and then press ctrl+z and ENTER to add the numbers that you entered" << std::endl;
    if (cnt = value) 
    ++cnt;          
    while (std::cin >> value)

       sum += value;  

    std::cout << "the sum is: " << sum << std::endl;
    std::cout << "the amount of numbers you entered are: " << cnt << std::endl;

    return 0;

}



The if statement that I tried to do is wrong and does not count the amount of integers. I can't seem to figure out how to count the integers using a loop that the user enters into " value ." Thanks for the help in advance.
Last edited on
To give you an idea...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
  int cnt = 0, sum = 0, value = 0;
    std::cout << "Please enter a set of numbers and then press ctrl+z and ENTER to add the numbers that you entered" << std::endl;

    while (std::cin >> value)
    {
       sum += value;
       cnt++; // Keep counting each user input.
    }
    std::cout << "the sum is: " << sum << std::endl;
    std::cout << "the amount of numbers you entered are: " << cnt << std::endl;

    return 0;

}
have them enter it as a string and count the number of 0-9's.
Thanks!
Topic archived. No new replies allowed.