Help With C++ Homework (Loops)

Hello, I am having trouble getting my program to output the correct information, it runs but not with the correct information. These are the directions, I have also attached my code.

A program is required which will act as a calculator. The program should prompt the user to enter 2 integers and a code. The program should perform the following actions based on the code entered.
-A code of 1 indicates that the two numbers should be added
-A code of 2 indicates that the two numbers should be multiplied
-A code of 3 indicates that the two numbers should be divided by the second number
-A code of 4 indicates that the two numbers should be subtracted from the second number

The Program should then print both numbers, with appropriate labels as well as the sum, difference, product or quotient with an appropriate label indicating what is being printed. This process should be repeated until 2 zeros are entered. An Appropriate message should then be printed to notify the user of the end of the program.

Sample Output:
Please enter a number: 3
Please enter another number: 6
Please enter the code: 1
The first number is 3 and the second number is 6. The sum of these numbers is 9.

Please enter a number: 0
Please enter another number: 0
Goodbye!


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

double calculateAddition(double&num1,double&num2,double&output);
double calculateSubtraction(double&num1,double&num2,double&output);
double calculateProduct(double&num1,double&num2,double&output);
double calculateDivision(double&num1,double&num2,double&output);
double printOutput(double &output);
double getCode(double &num1, double &num2, double &codeee);
double printOutput(double &output);
void getData(double &num1, double &num2);
void endProgram(double &num1, double &num2);

int main()
{
	//Declaring my variables
	double output, num1, num2, codeee;
	int code;
	//Function call
	getData(num1,num2);
	
	
	code = getCode(num1,num2,codeee);
	
	//If user enters two zeros, the program will end	
	if ((num1==0) && (num2==0))
	{
	int end=0;
	endProgram(num1, num2);
	}
	else getData(num1, num2);
	
	printOutput(output);
}

//Determines the calculation
void getData(double &num1, double &num2)
{
	double codeee;
	cout<<"Please enter a number"<<endl;
	cin>>num1;
	cout<<"Please enter another number"<<endl;
	cin>>num2;
	
	getCode(num1, num2, codeee);
	
	return;
}

double getCode(double &num1, double &num2, double&codeee)
{
	int code;
	
	double output;
	cout<<"Please enter the code"<<endl;
	cin>>codeee;
	
	if (code == 1)
	{
		calculateAddition(num1,num2,output);	
	}
	else if (code == 2)
	{
		calculateProduct(num1,num2,output);	
	}
	else if (code == 3)
	{
		calculateDivision(num1,num2,output);	
	}
	else if (code == 4)
	{
		calculateSubtraction(num1,num2,output);	
	}
	
	return codeee;
}
//Each calculation's function
double calculateAddition(double&num1,double&num2,double&output)
{
	output=num1+num2;
	cout << "The first number is." << " " << num1 << " " << "The second number is." << " " << num2 << " " << "The sum of these numbers is." << " " << output << endl;

	return output;
}
double calculateSubtraction(double&num1,double&num2,double&output)
{
	output=num1-num2;
	cout << "The first number is." << " " << num1 << " " << "The second number is." << " " << num2 << " " << "The difference of these numbers is." << " " << output << endl;

	return output;
}
double calculateProduct(double&num1,double&num2,double&output)
{
	output= num1 * num2;
	cout << "The first number is." << " " << num1 << " " << "The second number is." << " " << num2 << " " << "The product of these numbers is." << " " << output << endl;

	return output;
}
double calculateDivision(double&num1,double&num2,double&output)
{
	output=num1/num2;
	cout << "The first number is." << " " << num1 << " " << "The second number is." << " " << num2 << " " << "The quotient of these numbers is." << " " << output << endl;

	return output;
}
double printOutput(double &output)
{
	cout<<"Your output is: "<<output<<endl;
	return output;
}
void endProgram (double &num1, double &num2)
{
	cout<<"Goodbye!"<<endl;
	return;
}
52
53
54
55
56
57
58
	int code;
	
	double output;
	cout<<"Please enter the code"<<endl;
	cin>>codeee;
	
	if (code == 1)


What happens betweens line 52 and 58 that makes you think code takes on some predictable value?
This process should be repeated until 2 zeros are entered.


your program is only going to run once. you need to use a while loop.

The program should prompt the user to enter 2 integers


Your not getting integers from the user and your not checking to see if it's a valid value.

After that your program is kinda complicated, calling one function from another and another, and maybe not returning what you want when you want. Do you have a flowchart ?

Finally, you declare code, then you ask for codeee. something isn't right. Again a flowchart here would help, assuming it's not just a typo.

1
2
3
4
5
6
7
int code;
	
	double output;
	cout<<"Please enter the code"<<endl;
	cin>>codeee;
	
	if (code == 1)
Last edited on
closed account (48T7M4Gy)
Most of what you have is pretty good but this might be a little closer to what you want. I changed the way calculate addition works, deleted the goodbye part and replaced it in main and made the operator code a bit simpler. All you need do is complete the selection else if's in main and modify the reamining three calculate functions.

The principle involved is 1) you select the operation( this is where you can enter a quit) by getting the operation code, 2) get the two numbers, 3) perform the operation. You don't have to keep sending all the information to the function, just enough to get what you want. In fact to get the operation code you don't send anything, the parameter list in brackets is empty.

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

using namespace std;

double calculateAddition(double &, double &);

double calculateSubtraction(double&num1,double&num2,double&output);
double calculateProduct(double&num1,double&num2,double&output);
double calculateDivision(double&num1,double&num2,double&output);
double printOutput(double &output);
int getCode();
double printOutput(double &output);
void getData(double &num1, double &num2);
void endProgram(double &num1, double &num2);

int main()
{
    //Declaring my variables
    double output = 0, num1 = 0, num2 = 0;
    int operation = 0;
    
    //Function call
    
    operation = getCode();
    
    //If user enters two zeros, the program will end
    getData(num1,num2);
    if ((num1==0) && (num2==0))
    {
        cout << "Goodbye\n";
        return 0;
    }
    else if( operation == 1)
    {
        output = calculateAddition(num1, num2);
    }
    else{};
        
    
    printOutput(output);
    
    return 0;
}

//Determines the calculation
void getData(double &num1, double &num2)
{
    cout<<"Please enter a number"<<endl;
    cin>>num1;
    cout<<"Please enter another number"<<endl;
    cin>>num2;
    return;
}

int getCode()
{
    int op_code;
    cout<<"Please enter the code (1 +,2 -,3 *,4 /)"<<endl;
    cin>>op_code;
    
    return op_code;
}

//Each calculation's function
double calculateAddition(double &num1,double &num2)
{
    double output=num1+num2;
    cout << "The first number is." << " " << num1 << " " << "The second number is." << " " << num2 << " " << "The sum of these numbers is." << " " << output << endl;
    
    return output;
}

double calculateSubtraction(double&num1,double&num2,double&output)
{
    output=num1-num2;
    cout << "The first number is." << " " << num1 << " " << "The second number is." << " " << num2 << " " << "The difference of these numbers is." << " " << output << endl;
    
    return output;
}

double calculateProduct(double&num1,double&num2,double&output)
{
    output= num1 * num2;
    cout << "The first number is." << " " << num1 << " " << "The second number is." << " " << num2 << " " << "The product of these numbers is." << " " << output << endl;
    
    return output;
}

double calculateDivision(double&num1,double&num2,double&output)
{
    output=num1/num2;
    cout << "The first number is." << " " << num1 << " " << "The second number is." << " " << num2 << " " << "The quotient of these numbers is." << " " << output << endl;
    
    return output;
}

double printOutput(double &output)
{
    cout<<"Your output is: "<<output<<endl;
    return output;
}


Please enter the code (1 +,2 -,3 *,4 /)
1
Please enter a number
2
Please enter another number
3
The first number is. 2 The second number is. 3 The sum of these numbers is. 5
Your output is: 5
Program ended with exit code: 0
Last edited on
closed account (48T7M4Gy)
Maybe this instead of num1 and num2 = 0. Saves typing in numbers when all you want to do is quit.

1
2
3
4
5
6
7
8
int getCode()
{
    int op_code;
    cout<<"Please enter the code (0 quit, 1 +, 2 -, 3 *, 4 /)"<<endl;
    cin>>op_code;
    
    return op_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
#include <iostream>

using namespace std;

double calculateAddition(double &, double &);

double calculateSubtraction(double &, double &);
double calculateProduct(double &, double &);
double calculateDivision(double &,double &);
double printOutput(double &);

int getCode();
void getData(double &, double &);

int main()
{
    //Declaring my variables
    double output = 0, num1 = 0, num2 = 0;
    int operation = 0;
    
    //Function call
    
    operation = getCode();
    if (operation == 0)
    {
        cout << "Goodbye\n";
        return 0;
    }
    else if( operation == 1)
    {
        getData(num1, num2);
        output = calculateAddition(num1, num2);
    }
    else{/* blah blah blah*/};
    
    printOutput(output);
    
    return 0;
}

//Determines the calculation
void getData(double &num1, double &num2)
{
    cout<<"Please enter a number"<<endl;
    cin>>num1;
    cout<<"Please enter another number"<<endl;
    cin>>num2;
    
    cout << "The first number is." << " " << num1 << " " << "The second number is." << " " << num2 << '\n';
    return;
}

int getCode()
{
    int op_code;
    cout<<"Please enter the code (0 quit, 1 +, 2 -, 3 *, 4 /)"<<endl;
    cin>>op_code;
    
    return op_code;
}

//Each calculation's function
double calculateAddition(double &num1,double &num2)
{
    double output=num1+num2;
    cout << "The sum of these numbers is." << " " << output << endl;
    
    return output;
}

double calculateSubtraction(double &num1, double &num2)
{
    double output = num1 - num2;
    cout << "The difference of these numbers is." << " " << output << endl;
    
    return output;
}

double calculateProduct(double& num1,double& num2)
{
    double output= num1 * num2;
    cout << "The product of these numbers is." << " " << output << endl;
    
    return output;
}

double calculateDivision(double&num1,double&num2)
{
    double output=num1/num2;
    cout << "The quotient of these numbers is." << " " << output << endl;
    
    return output;
}

double printOutput(double &output)
{
    cout<<"Your output is: "<<output<<endl;
    return output;
}
Last edited on
I appreciate the help, another question. What kind of loop should I use (for, while, do) to cause my program to loop endlessly until two zeros are entered?
closed account (48T7M4Gy)
Best loop is a while loop I reckon.

That's why input of two zero's to quit isn't the best. Your program, your call though :)

pseudocode

loop: while (ask for operator code, get operator code and operator code != 0)
do stuff
continue to loop

now out of loop: cya later

end of program


And you could use a switch control to select the operation instead of a cascade of if's.
> What kind of loop should I use (for, while, do) to cause my program to loop endlessly until two zeros are entered?

A while loop is usually used for this. Something like this, perhaps:

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

bool get_input( double& first, double& second )
{
    std::cout << "enter first number: " ;
    std::cin >> first ;
    // note: if input fails (for instance the user enters nonsense like 'xyz')
    //       the value (first here) is set to zero (C++11)

    second = 0 ; // if the input of first failed, std::cin >> second would be a no op.
                 // assign zero to second before hand (we want both to be zeroes if input failed)
    std::cout << "enter second number: " ;
    std::cin >> second ;

    return first != 0 || second != 0 ; // true if at least one is non-zero
}

int main()
{
    double first ;
    double second ;

    // get_input returns false if both first and second are zero
    while( get_input( first, second ) ) // loop endlessly until two zeros are entered
    {
        // do something with first and second
        // ...
    }
}
Topic archived. No new replies allowed.