Hey I am very new to C++ and I have been doing some tutorials about it and then i decided I will try and make a calculator in c++
In the code below i want the user to input their numbers as many times as they would like. But when they press c it cancels the loop. But if plus is a int it cant process c so i changed int plus to string plus but then it won't total + plus;
I did some reseach and i found this atoi to convert the int into a string but it didnt work because it is really confusing and i dont know where to place the code.
So i would really appreciate for someone to help me figure out how to convert the int into a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int addition()
{
int plus;
int total = 0;
cout << "What is the first number you will like to add? or type c to cancel. \n";
cin >> plus;
while(plus != "c"){
total = total + plus;
cout << "what is the second number you will like to add? or type c to cancel. \n";
cin >> plus;
}
cout << "The numbers you have entered add up to " << total << endl << endl << endl;
return retry();
int addition()
{
int plus;
char ch ;
int total = 0;
cout << "What is the first number you will like to add? or type c to cancel. \n";
cin >> plus;
do
{
total = total + plus;
cout<<"\n Do you want to continue .. press y / Y";
cin>>ch ;
if( ch == 'y' || ch == 'Y')
cout << "what is the second number you will like to add? or type c to cancel. \n";
cin >> plus;
}while(ch != 'c');
cout << "The numbers you have entered add up to " << total << endl << endl << endl;
return 0;
}
other wise as JLBorges say ostringstream should solve your purpose
Ok i just tried bluecoder code it everything was working until I press c to get out of the loop. But it just prints out endless amounts of "what is the second number you will like to add? or type c to cancel."
> In the code below i want the user to input their numbers as many times as they would like.
> But when they press c it cancels the loop
...
>I just tried the ostringstream code but im getting an error at != cause it is incompatible
The code to read either a "c" or an integer from stdin would be something like this:
#include <iostream>
#include <string>
#include <sstream>
int main()
{
int number ;
std::string str ;
// read a string from stdin
while( std::cout << "enter an integer or 'c' to cancel: " && std::cin >> str )
{
if( ( str == "c" ) || ( str == "C" ) ) // if user entered 'c'
{
std::cout << "cancelled\n" ;
return 1 ; // exit program
}
else // try to convert the string to an int
{
std::istringstream stm(str) ;
// if the string contains a number and nothing else
if( stm >> number && stm.eof() )
{
std::cout << "the integer is " << number << '\n' ;
break ;
}
else // inform the user and try once again
std::cout << "error in input '" << str << "'\n" ;
}
}
// use number
}
do
{
total = total + plus;
cout<<"\n Do you want to continue .. press y / Y";
cin>>ch ;
if( ch == 'y' || ch == 'Y')
{
cout << "what is the second number you will like to add? or type c to cancel. \n";
cin >> plus;
}
}while(ch != 'c');