Basic Console Application


Here's the code of a very basic console calculator I made using DEVCPP.

My question is how do I loop the program to go back to the start so I can use input more calculations. Because what happens is the program let's me do one calculation then it gets to the end "Press any key to continue" and closes, whereas I want the program to loop back to the beginning and let me choose to do another calculation.

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
  #include <cstdlib>
#include <iostream>

using namespace std;

int addFunc()
{
    int num1; 
    int num2;
    int num3; 
    cout << "Please type 2 numbers to be added together \n";
    cin >> num1; cout << "\n";
    cin >> num2; cout << "\n";
    
    num3 = num1 + num2;
    
    cout << num1 << " + " << num2 << " = " <<  num3 << "\n";
}

int subFunc()
{
    int num1; 
    int num2;
    int num3; 
    cout << "Please type 2 numbers to be subtracted \n";
    cin >> num1; cout << "\n";
    cin >> num2; cout << "\n";
    
    num3 = num1 - num2;
    
    cout << num1 << " - " << num2 << " = " <<  num3 << "\n";
}

int multFunc()
{
    int num1; 
    int num2;
    int num3; 
    cout << "Please type 2 numbers to be multiplied \n";
    cin >> num1; cout << "\n";
    cin >> num2; cout << "\n";
    
    num3 = num1 * num2;
    
    cout << num1 << " * " << num2 << " = " <<  num3 << "\n";   
}

int divFunc()
{
     int num1; 
    int num2;
    int num3; 
    cout << "Please type 2 numbers to be divided \n";
    cin >> num1; cout << "\n";
    cin >> num2; cout << "\n";
    
    num3 = num1 / num2;
    
    cout << num1 << " / " << num2 << " = " <<  num3 << "\n";  
}


int main(int argc, char *argv[])
{
    int operation;
    cout << "Please type 1 for add, 2 for sub, 3 for mult, or 4 for div \n";
    cin >> operation;
    
    if ( operation == 1)
    {
         addFunc();
    }
    else if (operation == 2)
    {
         subFunc();      
    }
    else if  (operation == 3)
    {    
         multFunc();
    }
    else if (operation == 4)
    {
         divFunc();
    }
    else 
   {
     cout <<"You have not chosen a correct operation \n";
   }

              
    system("PAUSE");
    return 1;
}
Last edited on
Put the contents of main into a loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ...
int main(int argc, char **argv) {
    int operation;
    do {
        std::cout << "Please type 1 for add, ... 4 for div, or 5 to quit."
        std::cin >> operation;

        if (operation == 1) {
            addFunc();
        }
        // ...
        else if (operation != 5) {
            std::cout << "You have not chosen a correct operation\n";
        }
    } while (operation != 5);
    
    return 0;
}
Last edited on
Thank you very much I got it.
Helpful.

Edit: I don't know if I should start a new topic for this second code I'm working on.

It converts C to F temperatures
and Miles to KM temperatures.

It works perfectly I used a loop within a loop to have a selection between Temp and Distance. Then whether to convert F to C, or C to F. Km to M or M to KM.

Although it works functionally here's what it's doing that's a tad bit annoying. If you press 3, it will exit the screen and return to the previous one but it'll output the message "You have not enterered 1 2 or 3." so although it works properly, it outputs that message for some reason.


Try it for yourself, press 1 to enter Temperatures, then press 3 to return to previous screen. You'll see it works, but with the message showing up.





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
#include <cstdlib>
#include <iostream>

using namespace std;
int CtoF()
{
    float temp;
    cout << "\n Please enter the temperature in Celcius \n";
    cin >> temp;
    cout << temp << " Degress Celcius is " << (temp * 1.8) +32 << " Degress Fahrenheit. \n \n";
    
    return 0;
}

int FtoC()
{
    float temp;
    cout << "\n Please enter the temperature in Fahrenheit \n";
    cin >> temp;
    cout << temp << " Degress Fahrenheit is " << (temp - 32) /1.8 << " Degress Celcius. \n \n";
    
    return 0;
}

int MtoKM()
{
    float distance;
    cout << "\n Please enter the distance in Miles \n";
    cin >> distance;
    cout << distance << " Miles is " << (distance / 0.621371) << " KM. \n \n";
    
    return 0;
}

int KMtoM()
{
    float distance;
    cout << "\n Please enter the distance in KM \n";
    cin >> distance;
    cout << distance << " KM is " << (distance * 0.621371) << " Miles. \n \n";
    
    return 0;
}


int main(int argc, char *argv[])
{
    int option;
    int option2;
    int option3;
    do 
    {
        cout << "[Feel Goods Unit Converter.] \n";
        cout << "Please Type 1 to convert Temperatures, Please Type 2 to convert Distances. \n";
        cout << "Type 3 when you are finished to exit. \n \n";
        cin >> option;
        
        
        if (option == 1)
        {
        
        
        do 
        {
        
        cout << "Please type 1 to convert C to F, and type 2 to convert F to C. \n";
        cout << "Please type 3 to go back to main menu. \n";
        cin >> option2;
            
            if (option2 == 1)
            {                
                 CtoF();
            }
            
            else if (option2 == 2)
            {
                 FtoC();
            }
           
            else if (option !=3)
            {
                 cout << "\n You have not chosen 1, 2, or 3, please enter a correct number. \n \n";
            } }while(option2 !=3);       
        }
        
        
        else if ( option == 2 )
        do
        {
             cout << " Please enter 1 to convert Miles to KM. Please enter 2 to convert KM to Miles. \n";
             cout << " Please enter 3 to return to the main menu. \n";
             cin >> option3;
             
             if( option3 == 1)
             {
                 MtoKM();
             }
             else if (option3 == 2)
             {
                  KMtoM();
             }
             else if (option !=3)
             {
                  cout << "\n You have not chosen 1, 2, or 3, please enter a correct number. \n \n";
             }} while(option3 !=3);
               
             
        
        else if (option !=3)
        {
              cout << "\n You have not chosen 1, 2, or 3, please enter a correct number. \n \n";
        }
    } while(option != 3);
        
    return 0;
}
Last edited on
Make sure you use the correct variables at the correct positions in the code.
Topic archived. No new replies allowed.