C++ program that inputs two integers then calls a function subtract.

"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 find this assignment written very sloppily, and I'm having a very hard time understanding it. Not only am I not entirely sure what it's asking for, I can't make the code I've come up with work. I've done my research and this is as close as I could get. I feel like I'm going around in circles. Any help would be greatly 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
 #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;
       }
By "allow the user to change their input values" I would assume the instructor just doesn't want you to use std::cin.get(). You should be fine because you're using the input operator. Should you be checking user input for validity? Or are you not that far yet?
I don't think I'm supposed to check for validity. I think this code is sufficient. I hope so anyway. My only problem now is that I can't get it to work. It's probably a simple error somewhere. I'll have to keep messing around with it..
After messing around with the code some more, I still can't get it to work. Anyone know what I'm doing wrong?
It won't affect the way your program works but "change()" should return an integer. Also you never prompt the user to enter different numbers in the change() function or anytime after the initial input.
Ok. I guess I'll just keep messing around with it and see if I can get it to work. Thanks for the help. I appreciate it..
If you care to specify the issue you're having we can help you more.
Interesting, I didn't think it would compile without change() returning a value (even though we can "return" via reference)??
It is in fact just a warning, but it really ought to return a value in this case. I suppose this is to facilitate inline assembly or something where a return might simply be populating a register before the function ends.
Your IDE should throw a warning from something like this. If you have strict adherence set I think it will "fail".
Yes you're right, it's just in Code::Blocks I only see the warnings in the log real-time as it is compiling, once it's finished compiling the build log shows no warnings, unless of-course there was an error. How do I see a log of just warnings if it was a successful compile? (EDIT: that's if I build and run, I have to rebuild just to see the warnings!)

Btw OP, I think they mean something like this:

* main: cin 2 ints into x and y, then call subtract(x, y) - as you did.
* subtract: let user cin 2 ints again, this time into those ref's (essentially replacing x and y's orig. value - I assume this is what they meant, so they can let you see how ref's work)
* subtract: now do x - y and return the difference of that subtraction.

You'll probably use a variable to store the result of the call so the difference isn't lost, i.e. int result = subtract(x, y);, though they don't specifically ask for this.

I'm not exactly sure how to get the difference of a subtraction though, it's like a subtract/modulo hybrid?!
Last edited on
Topic archived. No new replies allowed.