+ 2

Write a program that will ask the user to continuously enter a number until such time that the entered value is not a number.

#include <iostream> #include <iomanip> using namespace std; int main(){ int number; int sum = 0; int product = 1; int loopcount = 0; int totalnum = 0; std:: cout << "Enter total of numbers you want to add or multiply" <<endl; std:: cin>> std::setw(1) >> totalnum; std::cin.clear(); std:: cout<< "Enter"<<totalnum<< "numbers to add or multiply: " << endl; while (loopcount < totalnum){ std::cout << "Input a number or 'x' to stop: " << endl; std::cin>> std::setw(1)>> number; std::cin.ignore(); std::cin.clear(); sum+= number; product*= number; loopcount= loopcount + 1; if ( !( cin >> std::setw(1)>> number ) ) {break;} std::cin.clear(); std::cin.ignore(INT_MAX, '\n'); } std:: cout<<"The sum is :" <<sum<<endl; std:: cout<<"The product is :" <<product<<endl; return 0; } I have run this code it reads the numbers and the sum and product is correct but the problem is It allows the user to input two numbers in every prompt

1st Oct 2021, 12:45 PM
shlee_
shlee_ - avatar
11 Answers
+ 1
Found the problem
1st Oct 2021, 1:11 PM
Eashan Morajkar
Eashan Morajkar - avatar
+ 2
Ashlie Jane, Add <climits> for the definition of INT_MAX.
1st Oct 2021, 1:19 PM
Ipang
+ 1
while (true) { std::cout << "Enter number: "; cin >> num; sum += num; product *= num; }
1st Oct 2021, 12:56 PM
Eashan Morajkar
Eashan Morajkar - avatar
+ 1
Saw the problem
1st Oct 2021, 1:06 PM
Eashan Morajkar
Eashan Morajkar - avatar
+ 1
Ashlie Jane In this if statement, you're again taking the input: if ( !( cin >> std::setw(1)>> number ) ) {break;}
1st Oct 2021, 1:12 PM
Eashan Morajkar
Eashan Morajkar - avatar
+ 1
This program does the desired: #include <iostream> #include <iomanip> using namespace std; int main(){ int number; int sum = 0; int product = 1; int loopcount = 0; int totalnum = 0; std:: cout << "Enter total of numbers you want to add or multiply"<<endl; std:: cin>> std::setw(1) >> totalnum; std::cin.clear(); std:: cout<< "Enter "<<totalnum<< " numbers to add or multiply: " << endl; while (loopcount < totalnum){ std::cout << "Input a number or x to stop: " << endl; std::cin >> std::setw(1) >> number; std::cin.ignore(); std::cin.clear(); sum+= number; product*= number; loopcount= loopcount + 1; if (number==((int)'x')) { break; } } std:: cout<<"The sum is : "<<sum<<endl; std:: cout<<"The product is : "<<product<<endl; return 0; }
1st Oct 2021, 1:17 PM
Eashan Morajkar
Eashan Morajkar - avatar
+ 1
But if you exit with 'x', then it still prints the remaining "Enter a number: " statements, it just prints them
1st Oct 2021, 1:18 PM
Eashan Morajkar
Eashan Morajkar - avatar
+ 1
Thank you so much 💗
1st Oct 2021, 1:19 PM
shlee_
shlee_ - avatar
+ 1
You're welcome
1st Oct 2021, 1:20 PM
Eashan Morajkar
Eashan Morajkar - avatar
0
How about input validation ? and also the limits I have run this code and it reads the numbers on the first line entered by the user but the problem it allows you to input two numbers in every prompt what is the wrong with my codes?
1st Oct 2021, 12:58 PM
shlee_
shlee_ - avatar
0
How can I make the output of product = 0 when totalnum enter by user is 0 ?
2nd Oct 2021, 1:26 AM
shlee_
shlee_ - avatar