Oct 23, 2014 at 6:56pm UTC
I would like to loop back to entering the first number. I am a newbie at C++ and trying to learn : )
Thanks guys
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include<iostream>
using namespace std;
int main()
{
float a,c,ans;
char b;
cout<<"Enter first number." <<endl;
cin>>a;
cout<<"Enter second number." <<endl;
cin>>c;
cout<<"Enter + for addition" <<endl;
cout<<"Enter - for substraction" <<endl;
cout<<"Enter x for multiplication" <<endl;
cout<<"Enter / for division" <<endl;
cin>>b;
switch (b) {
case '+' :
ans=a+c;
break ;
case '-' :
ans=a-c;
break ;
case 'x' :
ans=a*c;
break ;
case '/' :
ans=a/c;
break ;
default :
cout <<"Invalid, plesae try again" <<endl;
}
cout <<"Answer is:" << ans << endl;
cout <<"Now try another sum" <<endl;
return 0;
}
Last edited on Oct 23, 2014 at 6:59pm UTC
Oct 23, 2014 at 7:49pm UTC
Use a loop tp iterate
1 2 3 4 5 6
char trial;
while (trial==condition)
{
///do something
cout <<"wanna try again?" ; cin>> trial;
}
Last edited on Oct 23, 2014 at 7:56pm UTC
Oct 23, 2014 at 7:53pm UTC
Can you please go into more detail, I have never heard of that
Oct 23, 2014 at 8:19pm UTC
Okay loops are used to do something repeatedly while a certain condition is met , there are different types of loops, for loop, while loops and do while loops
You can check out here
http://www.cplusplus.com/doc/tutorial/control/