function question from a noob

Hello

I'm a C++ noob. I've been learning functions and ran into something that I would like my program to do but do not really see how. I'm sure it can be done, just perhaps not in the way that I have in mind.

This is a program that lets the user add or subtract integers (or end the program). As you will note from the code, I provide an error message, but the error message appears in every sub-block of code -- which seems redundant to me and therefore ripe for a function. What I am not seeing is how to put the error message in a function but then to get the program back on track once the user input is acceptable.

Please note also, like I said, I am noob, so any general helpful comments on my coding style is also appreciated.

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

// Program gives the user an option to 
// add or subtract two integers
// Program runs until user enters "0"

#include <iostream>
using namespace std;

int nAddition (int nNum1, int nNum2);
int nSubtraction (int nNum1, int nNum2);

int main()
{
    int nValue1, nValue2, nOption;
    
    // initial user choice interface
    
    cout << "Press 1 to add two integers or \n";
    cout << "Press 2 to subtract two integers: \n";
    cout << "<Press 0 to end program> \n";
    cin >> nOption;
    cout << " \n";
    
    while (nOption < 0 || nOption > 3) // error message    
    {
        cout << "<error> \n";
        cout << "Press 1 to add two integers or \n";
        cout << "Press 2 to subtract two integers: \n";
        cout << "<Press 0 to end program> \n";
        cin >> nOption;
        cout << " \n";
    }        
    while (nOption != 0) // user does not chose to exit
    {
        if (nOption == 1) // add two integers
        { 
            cout << "You have chosen to add two integers. \n";
            cout << " \n";

            cout << "Enter first integer: ";
            cin >> nValue1;
            cout << " \n";
    
            cout << "Enter second integer: ";
            cin >> nValue2;                
            cout << "\n";
    
            cout << "The sum of " << nValue1 << " and ";
            cout << nValue2 << " is: ";
            cout << nAddition(nValue1, nValue2) << "\n";
            cout << " \n";
            
            cout << "Press 1 to add two integers or \n";
            cout << "Press 2 to subtract two integers: \n";
            cout << "<Press 0 to end program> \n";
            cin >> nOption;
            cout << " \n";
                
            while (nOption > 3 || nOption < 0) // error message
            {
                cout << "<error> \n";
                cout << "Press 1 to add two integers or \n";
                cout << "Press 2 to subtract two integers: \n";
                cout << "<Press 0 to end program> \n";
                cin >> nOption;
                cout << " \n";
            }
        }             
        else // subtract two integers
        {
            cout << "You have chosen to subtract two integer. \n";
            cout << "\n";

            cout << "Enter first integer: ";
            cin >> nValue1;
            cout << " \n";
                        
            cout << "Enter second integer: ";
            cin >> nValue2;
            cout << " \n";
    
            cout << "The difference of " << nValue1 << " and \n";
            cout << nValue2 << " is: " << nSubtraction(nValue1, nValue2);
            cout << " \n";
            
            cout << "Press 1 to add two integers or \n";
            cout << "Press 2 to subtract two integers: \n";
            cout << "<Press 0 to end program> \n";
            cin >> nOption;
            cout << " \n";
                    
            while(nOption < 0 || nOption > 3) // error message
            {
                cout << "error: \n";
                cout << "Press 1 to add two integers or \n";
                cout << "Press 2 to subtract two integers: \n";
                cout << "<Press 0 to end program> \n";
                cin >> nOption;
                cout << "\n";
            }
        }       
    }    
    system("PAUSE");
    return 0;
}

// addition function

int nAddition(int nNum1, int nNum2)
{
    int nValue;
    nValue = nNum1 + nNum2;
    return(nValue);
}

 // subtraction function
 
int nSubtraction(int nNum1, int nNum2)
{
    int nValue;
    nValue = nNum1 - nNum2;
    return(nValue);
}


Thanks in advance!
What i would do is enclose everything after your first integer definitions in a while loop. Loop that until the user enters 0. Then use switch states for the add/subtract. Actualy, let me just show you.
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
// Program gives the user an option to 
// add or subtract two integers
// Program runs until user enters "0"

#include <iostream>
using namespace std;

int nAddition (int nNum1, int nNum2);
int nSubtraction (int nNum1, int nNum2);

int main()
{
    int nValue1, nValue2; 
	int	nOption = 1;
    
	while(nOption != 0)
	{
		// initial user choice interface
    
		cout << endl;
		cout << "Press 1 to add two integers or \n";
		cout << "Press 2 to subtract two integers: \n";
		cout << "<Press 0 to end program> \n";
		cin >> nOption;
		cout << " \n";
    
		if(nOption < 0 || nOption > 3) // error message    
		{
			cout << "<error> \n";
			cout << "You did not enter a valid choice";
			continue; //Bring the program back to the 
					  //top of the while loop
		}  
		switch(nOption)
		{
		case 1: // Add 
			cout << "You have chosen to add two integers. \n";
			cout << " \n";

			cout << "Enter first integer: ";
			cin >> nValue1;
			cout << " \n";
    
			cout << "Enter second integer: ";
			cin >> nValue2;                
			cout << "\n";
    
			cout << "The sum of " << nValue1 << " and ";
			cout << nValue2 << " is: ";
			cout << nAddition(nValue1, nValue2) << "\n";
			cout << " \n";
			break;
		case 2: // Subtract  
			cout << "You have chosen to subtract two integer. \n";
			cout << "\n";

			cout << "Enter first integer: ";
			cin >> nValue1;
			cout << " \n";
                        
			cout << "Enter second integer: ";
			cin >> nValue2;
			cout << " \n";
    
			cout << "The difference of " << nValue1 << " and \n";
			cout << nValue2 << " is: " << nSubtraction(nValue1, nValue2);
			cout << " \n";
			break;
		case 0:
			cout << "Program now exiting...";
			nOption = 0;
		}
	}       
    
    system("PAUSE");
    return 0;
}


// addition function
int nAddition(int nNum1, int nNum2)
{
    int nValue;
    nValue = nNum1 + nNum2;
    return(nValue);
}


 // subtraction function
int nSubtraction(int nNum1, int nNum2)
{
    int nValue;
    nValue = nNum1 - nNum2;
    return(nValue);
}





Thank you. I was not familiar with "continue". Using "switch" also seems to me to better capture the logic of the program, as well as make the program more concise.
nano511, if you don't mind let me ask you a follow up question. Why do you initialize 'nOption' to 1 in this case?

I initalize nOption to 1 because of
while(nOption != 0)

If i dont initilize nOption to anything then the while loop doesnt work and i get errors. And if i put it to zero then the while loop will never loop. I guess i could have initialized it to zero and made it a Do While loop but i didn't think about that.
Ok, I see that now. Thanks!
I dont know if its considered bad form, but usually on something really basic like adding or subtracting and the operation is obvious i eliminate the extra copy to the variable like so.

1
2
3
4
int nAddition(int nNum1, int nNum2)
{
   return (nNum1 + nNum2);
}
acorn is correct, his is more efficient, though perhaps the compiler will optimise out the redundant variables anyhow...

If we're really talking about efficiency (and indeed just concise code), then there is no real point having addition and subtraction functions. It is easier, more efficient and more versatile to simply write the operator.

However, I guess you were just using these as examples to practise with functions. Just remember that functions that simply wrap existing operators are not worthwhile in full programs if they don't add any new functionality.
Topic archived. No new replies allowed.