Hi there, im asked to make a simple program that takes two numbers and perform an arithemtic operation, the problem is that i have to store everything in strings, so i have one string with the first number, and a second string with the operator and the second number, i have to show which operation im making and the result in the same line, if i enter a q the program exits, something like this:
# include<iostream>
# include<string>
usingnamespace std;
int main ()
{
cout<<"Enter the first number: ";
string str1,str2;
cin>>str1;
float num1,num2,result;
while(str1!="q" || str1!="Q")
{
cout<<"\n\nEnter the operator and the second number: ";
cin>>str2;
if(str2=="q" || str2=="Q")
break;
/*till here everything was ok, but now the problem
is to make the numbers and operator useful and i
have no idea of how to do that cause c++ does not cast from
string to float, and that only would be for the numbers
*/
num1=(float)str1.substr(0,string::npos);/*wrong!!!, How do i do it??*/
num2=(float)str2.substr(1,string::npos);//wrong!!!
result=num1 operator num2;//how do a use the operator....
cout<<"\n\n"<<str1<<str2<<"="<<result;
}
cin.ignore(cin.rdbuf()->in_avail()+1);
return 0;
}
So if the program were correct , it would ask for number one, lets say "10" and for the operator and number two, lest say "+10", and then it would show "10+10=20", and keep asking for and operator and a number, applying them on the result of the previous oparation which i didnt include on the code, until a q is entered and the program ends. How do i do it? (use the numbers and an operator that are contained inside a string). And something else, how do i enter a string with spaces, lest say "!!!FIRST BLOOD!!", cuase with std::cin i can only enter single words. Thank u for ur time and patience. Alejandro from Colombia.
Damn man, i manage to get the numbers, but didnt read what u wrote about the operator, if u dont tell me to do a switch i would never have thought about it, hahaha , thanks, i was trying to use it directly hahaha, what an @ss.