Help with cycle

I write a program that work as a calculator at the end i want to insert a choice for close the program or restart from the beginning without exit. I post the calculator and want to insert something like the second 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <cstdlib>
#include <iostream>
#include <math.h>
 
using namespace std;
 
int main(int argc, char *argv[])
{   int a, ax, ay;
    double x, y, z, bq, dq, x1, x2, d;
   
    cout<<"Questo programma e' una vera e propria calcolatrice spero possa esserti utile\n";
   
    cout<<"\n1-Addizione\n2-Sottrazione\n3-Moltiplicazione\n4-Divisione\n5-Divisione di numeri interi con resto\n6-Potenza\n7-Radice\n8-Equazioni di secondo grado\n";
    cout<<"\nInserisci l' operatore: ";
    cin>>a;
   
    if (a==1)
    {
            cout<<"\nInserisci il primo numero: ";
            cin>>x;
   
            cout<<"\nInserisci secondo numero: ";
            cin>>y;
           
            cout<<"\n\n"<<x<<" + "<<y<<" = "<<x+y<<"\n\n";
             
    }
   
    else if (a==2)
    {
            cout<<"\nInserisci il primo numero: ";
            cin>>x;
   
            cout<<"\nInserisci secondo numero: ";
            cin>>y;
   
            cout<<"\n\n"<<x<<" - "<<y<<" = "<<x - y<<"\n\n";
    }
   
    else if (a==3)
    {
            cout<<"\nInserisci il primo numero: ";
            cin>>x;
   
            cout<<"\nInserisci secondo numero: ";
            cin>>y;
   
            cout<<"\n\n"<<x<<" x "<<y<<" = "<<x * y<<"\n\n";
    }
   
    else if (a==4)
    {
            cout<<"\nInserisci il primo numero: ";
            cin>>x;
   
            cout<<"\nInserisci secondo numero: ";
            cin>>y;
   
            cout<<"\n\n"<<x<<" / "<<y<<" = "<<x / y<<"\n\n";
    }
   
    else if (a==5)
    {
            cout<<"\nInserisci il primo numero: ";
            cin>>ax;
   
            cout<<"\nInserisci secondo numero: ";
            cin>>ay;
   
            cout<<"\n\n"<<ax<<" / "<<ay<<" = "<<ax / ay<<"resto"<<ax % ay;
    }
   
    else if (a==6)
    {
 
             cout<<"\nInserisci il numero: ";
             cin>>x;
             cout<<"\nInserisci l'esponete: ";
             cin>>y;
             z=pow(x,y);
             cout<<"\n"<<x<<"^"<<y<<" = "<<z<<"\n\n";
             
    }
   
    else if (a==7)
    {
 
             cout<<"\nInserisci il numero: ";
             cin>>x;
             cout<<"\nInserisci il radice: ";
             cin>>y;
             z=pow(x,1/y);
             cout<<"\nRadice "<<y<<" di "<<x<<" = "<<z<<"\n\n";
             
    }
   
    else if (a==8)
    {
 
             cout<<"\nInserisci il valore della x^2: ";
             cin>>x;
             cout<<"\nInserisci il valore della x: ";
             cin>>y;
             cout<<"\nInserisci il termine noto: ";
             cin>>z;
             bq=pow(y,2);
             d=bq-4*x*z;
             
             if (d>=0)
             {
              dq=pow(d,0.5);
              x2=((0-y)+dq)/(2*x);
              x1=((0-y)-dq)/(2*x);
             
              if (x1==x2)
              cout<<"\nLa soluzione e'"<<x1<<"\n\n";
             
              else
              cout<<"\nLe soluzioni sono "<<x1<<" e "<<x2<<"\n\n";
             
              }
             
             else
             {cout<<"\nLe soluzioni non esistono \n\n";}
             
             
    }
   
    else
    cout<<"\nErrore nella forma.\n\n";
   
   
    system("PAUSE");
    return EXIT_SUCCESS;
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 double b;

cout<<"\n1-Going to the top\n2-Exit\n";

cin>>b;

if (b==1)
{ [...] //something for going up}

else if (b==2)
{return EXIT_SUCCESS;}

else
{cout<<"\nError in the format.\n\n";}

system ("PAUSE");
return EXIT_SUCCESS;


exscusame for the English I'm Italian
Last edited on
Enter the do-while loop! This loop says:

1
2
3
4
5
do{
   
code in here at least once...

}while(this is true);


for example:
1
2
3
4
5
do{
   
cout << "1"

}while(1<2);


This would, of course, enter into an infinite loop outputting 1's - as 1 will always be less than 2.

Now for your code example - you already seem to have the hang of input/output so just put a similar input at the bottom of your code, that will help initialize the loop:

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <cstdlib>
#include <iostream>
#include <math.h>
 
using namespace std;
 
int main(int argc, char *argv[])
{   int a, ax, ay, again; //added int again
    double x, y, z, bq, dq, x1, x2, d;
   
   do //start the do!
   {
    system("cls"); // added to refresh screen in case re-run
    
	cout<<"Questo programma e' una vera e propria calcolatrice spero possa esserti utile\n";
   
    cout<<"\n1-Addizione\n2-Sottrazione\n3-Moltiplicazione\n4-Divisione\n5-Divisione di numeri interi con resto\n6-Potenza\n7-Radice\n8-Equazioni di secondo grado\n";
    cout<<"\nInserisci l' operatore: ";
    cin>>a;
   
    if (a==1)
    {
            cout<<"\nInserisci il primo numero: ";
            cin>>x;
   
            cout<<"\nInserisci secondo numero: ";
            cin>>y;
           
            cout<<"\n\n"<<x<<" + "<<y<<" = "<<x+y<<"\n\n";
             
    }
   
    else if (a==2)
    {
            cout<<"\nInserisci il primo numero: ";
            cin>>x;
   
            cout<<"\nInserisci secondo numero: ";
            cin>>y;
   
            cout<<"\n\n"<<x<<" - "<<y<<" = "<<x - y<<"\n\n";
    }
   
    else if (a==3)
    {
            cout<<"\nInserisci il primo numero: ";
            cin>>x;
   
            cout<<"\nInserisci secondo numero: ";
            cin>>y;
   
            cout<<"\n\n"<<x<<" x "<<y<<" = "<<x * y<<"\n\n";
    }
   
    else if (a==4)
    {
            cout<<"\nInserisci il primo numero: ";
            cin>>x;
   
            cout<<"\nInserisci secondo numero: ";
            cin>>y;
   
            cout<<"\n\n"<<x<<" / "<<y<<" = "<<x / y<<"\n\n";
    }
   
    else if (a==5)
    {
            cout<<"\nInserisci il primo numero: ";
            cin>>ax;
   
            cout<<"\nInserisci secondo numero: ";
            cin>>ay;
   
            cout<<"\n\n"<<ax<<" / "<<ay<<" = "<<ax / ay<<"resto"<<ax % ay;
    }
   
    else if (a==6)
    {
 
             cout<<"\nInserisci il numero: ";
             cin>>x;
             cout<<"\nInserisci l'esponete: ";
             cin>>y;
             z=pow(x,y);
             cout<<"\n"<<x<<"^"<<y<<" = "<<z<<"\n\n";
             
    }
   
    else if (a==7)
    {
 
             cout<<"\nInserisci il numero: ";
             cin>>x;
             cout<<"\nInserisci il radice: ";
             cin>>y;
             z=pow(x,1/y);
             cout<<"\nRadice "<<y<<" di "<<x<<" = "<<z<<"\n\n";
             
    }
   
    else if (a==8)
    {
 
             cout<<"\nInserisci il valore della x^2: ";
             cin>>x;
             cout<<"\nInserisci il valore della x: ";
             cin>>y;
             cout<<"\nInserisci il termine noto: ";
             cin>>z;
             bq=pow(y,2);
             d=bq-4*x*z;
             
             if (d>=0)
             {
              dq=pow(d,0.5);
              x2=((0-y)+dq)/(2*x);
              x1=((0-y)-dq)/(2*x);
             
              if (x1==x2)
              cout<<"\nLa soluzione e'"<<x1<<"\n\n";
             
              else
              cout<<"\nLe soluzioni sono "<<x1<<" e "<<x2<<"\n\n";
             
              }
             
             else
             {cout<<"\nLe soluzioni non esistono \n\n";}
             
             
    }
   
    else
    cout<<"\nErrore nella forma.\n\n";
    
       
    cout<<"1-Going to the top\n2-Exit\nSelection: "; // here's where you'll get your input!
    cin >> again;

	}while(again == 1); // end it!
   
    system("PAUSE");
    return EXIT_SUCCESS;
}


The main part of your code is untouched (minus declaring the int 'again', up top). But this should help you run it so long as the user wants to continue. Sorry I couldn't respond in Italian!
Last edited on
This isn't related to your question, but I think you should use a switch in your code instead of all those if else statements.

A switch would look like,

1
2
3
4
5
6
7
switch ( a ){
case 1:
/*do something*/
break;
case 2:
/*do something*/
break;

and so on.
Hi.
After these things, you can call your main function to restart your program.
i cant understand italian(already my orginal language is persian!!) so i wrote example and didnt make changes in your code.
you can use it in your program.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main()
{
    int a;
    cin>>a;
    if(a == 1)
        main();
    return 0;
}


and for quit from program, in simplest way, use return 0; where you want to Quit

Have nice Time
amirtork wrote:
After these things, you can call your main function to restart your program.

No. It's not legal to make a recursive call to main.
Okay!
so you can use goto command too
but i did not here anywhere about its not legal to call main recursive...
you can use goto like that:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
    restart:
    cout<<"Amirtork";
    goto restart;
    return 0;
}


this program will show Amirtork in a loop without end.
thanks to AbstractionAnon for let me know my mistake.(thank you bro.)
Have nice day.
You should never use goto statements in C++ either. If you want to loop main forever, use a loop.
1
2
3
4
5
6
7
int main ( )
{
    while ( true )
    {
        /*do something*/
    }
}
Last edited on
Topic archived. No new replies allowed.