How can I loop back to start of this simple programme?

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
closed account (SECMoG1T)
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
Can you please go into more detail, I have never heard of that
closed account (SECMoG1T)
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/
Topic archived. No new replies allowed.