C++ program to read integers until 0 is entered using sentinel.

Hello, I'm trying to write a C++ program to read integers until 0 is entered using sentinel.
(1) Display the sum of the two-digit numbers (both positive and negative)
(2) Display the largest of the positive integers
(3) Display the smallest of the negative integers
(4) Display how many numbers are divisible by 7
I'm mainly having trouble figuring out how to enter however many numbers the user wants to enter and then ending it at 0. The other stuff seems pretty simple.
Reading the numbers is simple.
1
2
3
4
5
6
7
int number;
while (std::cin >> number)
{
  if(number == 0)
    break;
  // do sth. with number
}
A code snippet that is a bit more advanced, and fails "gracefully" when 0 or any non-numeric data is entered.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
   int num;

   while ((std::cout << "Enter a number, 0 to quit: ")
          && std::cin >> num
          && num != 0)
   {
      std::cout << "User entered: " << num << '\n';

      // well, what do you do with the entered number?
   }

   // you've gathered your data, now what?
   std::cout << "Done!\n";
}
Enter a number, 0 to quit: 5
User entered: 5
Enter a number, 0 to quit: 12
User entered: 12
Enter a number, 0 to quit: 0
Done!
Enter a number, 0 to quit: 12
User entered: 12
Enter a number, 0 to quit: 14
User entered: 14
Enter a number, 0 to quit: -144
User entered: -144
Enter a number, 0 to quit: a
Done!

How you deal with the properly entered data awaits being coded.

FYI, thmm's code will also "die" if non-numeric data is entered as well.

I moved the if check for 0 into the while statement as well as displaying a prompt for the input.
Last edited on
Another style of loop that works the same as the while loop above:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
   // define any variables you want to use within and after the loop

   for (int num; (std::cout << "Enter a number, 0 to quit: ")
                  && std::cin >> num
                  && num != 0;)
   {
      std::cout << "User entered: " << num << '\n';

      // well, what do you do with the entered number?
   }

   // you've gathered your data, now what?
   std::cout << "Done!\n";
}
I may be just stupid but I can't seem to get this to work the way I want it to. Essentially I want to be able to enter however many numbers and the program then displays what I showed above (1, 2, 3, and 4). When they are done entering the numbers they wish to enter they put in 0 to mark the end of the numbers they want to read. The problem I'm having right now with the code provided is it ends the program before it reads the numbers and does the calculations. For example, if I entered 1 2 3 4 0, I'd want it to read 1 2 3 and 4 and not 0 and calculate the sum. largest and smallest, and divisible by 7.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <limits>

const int MinInt = std::numeric_limits<int>::min();
const int MaxInt = std::numeric_limits<int>::max();

int main()
{
  int number, sum = 0, smallest = MaxInt, largest = MinInt;
  while (std::cin >> number)
  {
    if(number == 0)
      break;
    sum += number;
    if (number > largest)
      largest = number;
    if (number < smallest)
      smallest = number;
  }
  std::cout << "Smallest: " << smallest << "\n";
  std::cout << "Largest: " << largest << "\n";
  std::cout << "Sum: " << sum << "\n";
}


Input 1 2 3 4 0

Output:

Smallest: 1
Largest: 4
Sum: 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <limits>

const int MinInt = std::numeric_limits<int>::min();
const int MaxInt = std::numeric_limits<int>::max();

int main()
{
  int number, sum = 0, smallest = MaxInt, largest = MinInt;
  while (std::cin >> number)
  {
    if(number == 0)
      break;
    sum += number;
    if (number > largest)
      largest = number;
    if (number < smallest)
      smallest = number;
  }
  std::cout << "Smallest: " << smallest << "\n";
  std::cout << "Largest: " << largest << "\n";
  std::cout << "Sum: " << sum << "\n";
}


Input: 1 2 3 4 0

Output:
Smallest: 1
Largest: 4
Sum: 10
@thmm, this is exactly what I needed, thank you so much!
Topic archived. No new replies allowed.