0

how i do this?

i wanna do the program that tell the user to input’=‘ to stop and calculate the sum. but i dont know ho to write it on c++

10th Jan 2023, 1:51 PM
cherry
cherry - avatar
3 Answers
+ 1
you can't cin to an int if you want to get the "=". do the integer casting (stoi) during addition. cin to a string. #include <iostream> #include <string> using namespace std; int main() { int sum; string num; cout<<"Enter any number and press = to calculate the sum "<<endl; while(true){ cout<<"Enter number : "; cin>>num; if(num!="=") sum+=stoi(num); else{ cout << "Sum = " << sum; break; } } }
10th Jan 2023, 2:19 PM
Bob_Li
Bob_Li - avatar
0
#include <iostream> #include <conio.h> #include <string> using namespace std; int main() { int num, sum; const char ESC = '='; cout<<"Enter any number and press = to calculate the sum "<<endl; cout<<"Enter number : "; cin>>num; while(num>=0 || num<=0){ sum+=num; cout<<"Enter number : "; cin>>num; } if( num == ESC ){ cout << "Sum = " << sum; } } how i can improve my code?
10th Jan 2023, 1:51 PM
cherry
cherry - avatar
0
okay thankyou
10th Jan 2023, 3:04 PM
cherry
cherry - avatar