2
#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 have is wrong and does not count the amount of integers the user enters into value.

How can I make the program count the amount of integers the user enters using a loop?

Anthon
  • 69,918
  • 32
  • 186
  • 246
  • You need to provide input and output cases – Ambrish Pathak May 21 '17 at 07:13
  • 1
    Please read the [tour]. Your title starts with a non-word and non-relevant information. The answer to your question is Yes. (which makes it a badly asked question). – Anthon May 21 '17 at 07:13
  • BTW, you should enable warnings. Than your compiler will probably warn about `if (cnt = value)`, see also https://stackoverflow.com/questions/17681535/variable-assignment-in-if-condition – stephan May 21 '17 at 07:21

1 Answers1

4

Solution explained

in order to count the number of integers provided, simply add 1 to cnt whenever a new input is given. (see line with //** comment below).

Also, the cnt==value check at start is not required (and there's one '=' character missing there).

updated code

To sum it all up, your code should be changes as follows:

#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++; //**
    }
    std::cout << "the sum is: " << sum << std::endl;
    std::cout << "the amount of numbers you entered are: " << cnt << std::endl;

    return 0;

}
ibezito
  • 5,782
  • 2
  • 22
  • 46