Hi im new to c++! so basically i have made a simple calculator but i have no idea how to loop it to starting so users can use it again. so how do i loop this calculator to the starting?
#include <iostream>
usingnamespace std;
int main()
{
float a , b , ans ;
char op ;
cout << "Enter a math expression :) ";
cin >> a >> op >> b ;
if ( op == '+')
{
ans = a + b ;
cout << ans << endl;
}
elseif ( op == '-')
{
ans = a - b;
cout << ans << endl;
}
elseif ( op == '*')
{
ans = a * b;
cout << ans << endl;
}
elseif ( op == '/')
{
if ( b == 0)
{
cout << "Error" << endl;
}
else
{
ans = a / b;
cout << ans << endl;
}
}
else
cout << "Error" << endl;
system ("pause");
return 0;
}
Put do { under your variables then before your con maybe set them to default values then after the error message put } while( true ) and somewhere before that if a user hits a certain key break the loop you should google do/while loop
#include <iostream>
usingnamespace std;
int main()
{
float a , b , ans ;
char op ;
do{
cout << "Enter a math expression :) ";
cin >> a >> op >> b ;
if ( op == '+')
{
ans = a + b ;
cout << ans << endl;
}
elseif ( op == '-')
{
ans = a - b;
cout << ans << endl;
}
elseif ( op == '*')
{
ans = a * b;
cout << ans << endl;
}
elseif ( op == '/')
{
if ( b == 0)
{
cout << "Error" << endl;
}
else
{
ans = a / b;
cout << ans << endl;
}
}
else
cout << "Error" << endl;
}while(true);
system ("pause");
return 0;
}
Also make sure you don't place variables inside loops, because that's just, BAAD... (Personal experience).