Reputation: 876
I am trying to create a while loop within a simple test program. This program is meant to sum all the integers that are entered by the user, and to display this sum and exit the loop after the user presses "a." If the user enters something other than an integer or "a," the program is meant to respond "Invalid input. Please try again" and allow the user to enter a different number.
My program successfully sums up integers, but if a non-integer other than "a" is entered, the loop ends without allowing the user to enter in a different value. The block of code starting with if (!cin) doesn't seem to be doing anything.
I think the issue is with (!cin)
and not with test = to_string(number)
. Is there an alternative method to detecting a non-integer that would allow this program to succeed?
Here is the program. Thank you as always for your help!
#include <iostream>
using namespace std;
#include <string>
int main()
{
int sum = 0;
int number;
string test;
cout << "Enter a series of integers; when you are done entering these integers, press a to display the sum of all integers.\n";
while (cin >> number)
{if (!cin)
{
test = to_string(number);
if (test == "a")
{break;}
else
{cout << "Invalid input. Please try again.\n";
continue;}
}
else
{sum += number;}
}
cout << "The total sum is " << sum << ".\n";
}
Upvotes: 0
Views: 1825
Reputation: 876
Here's a solution that combines a program from Phillip Siedler's Bumpy Road to Code blog (https://bumpyroadtocode.com/2017/07/11/sum-of-integer-range/) with Lakshmi's use of while (cin).
This seems to allow the program to break if "a" is entered, and to enter the invalid input message and allow the user to try again if something other than an integer or "a" is entered.
I'm somewhat mystified as to why cin.clear()
followed by cin >> test
works. If you clear cin
, how is there something in cin
for the cin >> test
line to pick up on? Perhaps I'm not understanding exactly what cin.clear()
does.
#include <iostream>
using namespace std;
#include <string>
//Solution borrows significantly from Philipp Siedler's solution for Chapter 5 Exercise 8 of Programming Practice and Principles: https://bumpyroadtocode.com/category/chapter-05-principles-and-practice-using-c/
int main()
{
int sum = 0;
int number;
string test;
cout << "Enter a series of integers; when you are done entering these integers, press a to display the sum of all integers.\n";
while (cin)
{
if (cin >> number)
{sum += number;}
else
{cin.clear();
cin >> test;
if (test == "a")
{break;}
else
{
cout << "Invalid input. Please try again.\n";
continue;}
}
}
cout << "The total sum is " << sum << ".\n";
}
Upvotes: 0
Reputation: 73
See inline comments for details:
#include <iostream>
using namespace std;
#include <string>
int main()
{
int sum = 0;
char ch; // Since we can't know whether user inputs an int or char
cout << "Enter a series of integers; when you are done entering these integers, press a to display the sum of all integers.\n";
while (cin) // Loop until user inputs
{
cin >> ch;
if (isalpha(ch)) // Check if input is type char and handle as needed
{
if (ch == 'a')
{
break;
}
else
{
cout << "Invalid input. Please try again.\n";
std::cin.clear(); // Delete the current stream
std::cin.ignore(32767,'\n'); // Revert back to previous input
continue;
}
}
else
{
sum += (ch - '0') % 48; // Since we are converting the char to int, handle it properly
}
}
cout << "The total sum is " << sum << ".\n";
}
Upvotes: 0