How to keep a program running?

closed account (42hU7k9E)
I'm having a problem with my "basic-calculator". The idea of the program is, that the user types in two numbers, and the program multiplies, divies, subtracts and adds them together. When the program does the operations and outputs them, I want the program to begin again - I want it to repeat itself until the user inputs an arranged word like "exit" or smth. I tried with strings and do "while", but my knowledge of C++ is verry poor for now, so I want you to help me please.

Heres the code:

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
#include <iostream>
using namespace std;

int main ()
{



	double st1, st2, produkt, kolicnik, vsota, razlika;
	

	cout << "Put in the first num: " << endl;
	cin >> st1;
	cout << "Put in the second num: " << endl;
	cin >> st2;


	produkt = st1 * st2;
	cout << "Produkt je: " << produkt << endl;

	kolicnik = st1 / st2;
	cout << "Kolicnik je: " << kolicnik << endl;


	vsota = st1 + st2;
	cout << "Vsota je: " << vsota << endl;


	razlika = st1 - st2;
	cout << "Razlika je: " << razlika << endl;
	
	return 0;
}

The variables are in Slovenian language, but I think that wont bother you.
Last edited on
You are on the right track. Can you post one of your attempts and we can point out why it didn't work.
Try use goto and a Yes or No question (example:
1
2
3
4
5
6
7
8
9
char choice;
cout << "Want restart the program? Y/N";
cin >> choice;
if (choice == 'y')
goto ...
else if (choice == 'n')
void exit (int exitcode);
else
cout << "Please press Y or N!";
Last edited on
I would suggest not using goto. It's generally more acceptable to use loops and an exit condition.
+1 for NOT using goto. Use a while or do..while loop
Why not use goto? It's only dangerous when there is the risk of an infinite loop...
And I don't see how a while or a do...while loop can be used here...
It make's code less maintainable before it doesn't have normal scope blocks like a while/do loop. It's generally discouraged in the professional community because it's very difficult to follow the applications flow when goto's are being used.

edit:
1
2
3
4
5
6
7
8
9
10
11
string sinput = "";

cout << "please enter equation: ";
cin >> sinput;

while (sinput != "exit") {


cout << "please enter equation: ";
cin >> sinput;
}
Last edited on
It's because of this (which by your standards would be "ok"):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() {
   int a, b, c;
   cin>>a;
   cin>>b;
   cin>>c;
   if(a == 0) goto one;
   else goto two;
   one:if(b == 0) goto nine;
   else goto ten;
   two:if(c == 0) goto six;
   else goto seven;
   nine:if(a > 0) goto two;
   //etc etc
}
Here's another way to go about it:
char ch;
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
do
{
cout << "Put in the first num: " << endl;
	cin >> st1;
	cout << "Put in the second num: " << endl;
	cin >> st2;


	produkt = st1 * st2;
	cout << "Produkt je: " << produkt << endl;

	kolicnik = st1 / st2;
	cout << "Kolicnik je: " << kolicnik << endl;


	vsota = st1 + st2;
	cout << "Vsota je: " << vsota << endl;


	razlika = st1 - st2;
	cout << "Razlika je: " << razlika << endl;
	
       cout<<"Would you like to continue?(y/n):"<<endl;
       cin>>ch;
       }while(ch=='y' || ch=='Y')
Last edited on
Topic archived. No new replies allowed.