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++
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;
}
}
}
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?
0
okay thankyou