Completely Lost....

I have an assignment due tonight at 12am est, I am completely lost here..... Can anyone point me to the right direction?

"Write a C++ program that inputs two integers then calls a function subtract, use pass by reference and allow the user to change his/her input values, then subtract the two numbers and return the difference."

I do not understand if I am supposed to input the two integers into the actual code, or as for two integers from the user. Also how am I supposed to give the user the opportunity to change his/her values, I was under the assumptions that when you pass by reference, you have the opportunity to swap the two numbers, not completely enter two new numbers
Ask for input->call function: int substract(int&, int&)->ask user if (s)he want to change numbers->update if necessary->cout the answer.

I think that is the way.
I have came up with this so far.....I dont know how to actually change the values besides swap them....Does this look anything like it is supposed to??

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

using namespace std;

int change(int& x, int& y)         
{
     int temp=0;                
     cout << "Function(before change): " << x << " " << y << endl;
     temp = x;                         
     x = y;                                 
     y = temp;                         
     cout << "Function(after change): " << x << " " << y << endl;
}
int main()
{
int x,y;

    cout << "Enter your first number: ";
    cin >> x;
    
    cout << "Enter your second number: ";
    cin >> y;
    
       
       cout << "Main (before change): " << x - y << endl;
       change(x, y);
       
       cout << "Main (after change): " << x - y << endl;
       

       
       system("Pause");
       return 0;
       }
Ok so i kind of tweaked it and now it looks like

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

using namespace std;

int change(int& x, int& y)         
{
     int temp=0;                
     cout << "Function(before change) before subtraction: " << x << " " << y << endl;
     temp = x;                         
     x = y;                                 
     y = temp;                         
     cout << "Function(after change) before subtraction: " << x << " " << y << endl;
}
int main()
{
int x,y;

    cout << "Enter two numbers which you would like to subtract. Enter your first number: ";
    cin >> x;
    
    cout << "Enter your second number: ";
    cin >> y;
    
       
       cout << "Main (before change) after subtraction: " << x - y << endl;
       change(x, y);
       
       cout << "Main (after change) after subtraction: " << x - y << endl;
       

       
       system("Pause");
       return 0;
       }



Im still not sure if these is even what i am supposed to be doing, I only swapped out the two values with each other, I am not sure how to completeley allow the user to input two new numbers (this does not make any sense because they already are given the option to input their own numbers, why would they want to change them?)

Can you change your profile so you accept PMs?
Same issue. It is not clear as to if the user is inputting twice...
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
#include <iostream>
using namespace std;

// function prototypes go here

int main()
{
	// declare integers, may as well initialize them to 1, can't hurt
	// need to declare a variable for user input
	char cont = 'y';

	while (cont == 'y')
	{

		// display current values of x, y to the user
		// ask them if they want to change the values
		// get input from the user

		while (input != 'y' && input != 'n') // your choice if you want to include this
		{ // this basically clears incorrect input
			cin.clear();
			cin.ignore(1000, '\n');
			cout << "Incorrect input.  Would you like to change the values of x and y? (y/n): ";
			cin >> input;
		}

		if (input == 'y')
		{
			// user wants to change the values, prompt them to do so
			// call the function to change the values here

			while (cin.fail())
			{
			cin.clear();
			cin.ignore(1000, '\n');
			cout << "Incorrect input.  Enter value of x and y: ";
			// call the function to change the values here
			}
		}

		cout << x << " - " << y << " = " << subtract(x, y) << endl;
		
		// ask the user if he wants to continue processing numbers
		// get user's answer via console input

		while (cont != 'y' && cont != 'n') // another error checking loop
		{
			cin.clear();
			cin.ignore(1000, '\n');
			cout << "Incorrect input.  Would you like to continue? (y/n): ";
			cin >> cont;
		}
	}

	cin.clear();
	cin.ignore(1000, '\n');
	cin.get(); // so you can see the end result
	return 0;
}

int subtract (const int & x, const int & y) // you'll want const integers for this
{
	// you want to return the result of x - y
}

void change_values (int & x, int & y) // const integers would be very bad for this of course
{
	// here you want to get console input from the user twice, once for x, once for y
}


Here's a program skeleton, fill in the blanks.
Last edited on
Topic archived. No new replies allowed.