Newbie here, could use some help :)

I am taking an intro to Computer Science and have been doing pretty well so far. Alot of the programming is getting tough as of late and my assignment is to do this:

Write a C++ program that prompts the user to enter an integer x at the keyboard. Your main function should store the user's integer x, then invoke two non-value returning functions as follows:

printVar(x); // Prints the value of x
changeVar(x); // Squares x (x times x), then adds 5 to x
printVar(x); // Prints the new value of x

The printVar function should pass the parameter by value.
The changeVar function should pass the parameter by reference.




Is there any way you guys could help me this? I am a Graphic Designer that needs to take this course as a pre-requisite for an html course which Im pumped up for.

Thanks guys!
In the future, please don't post the same question on multiple forums.
Ok.

Is there anyway you can help me with this though man?
This should work:

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 std::cout;
using std::cin;
using std::endl;


void printVar(int x) {

cout << x << endl;

}

void changeVar(int& x) {

x = x * x + 5;

}



int main() {

int x;
cout << "Please enter and integer: ";
cin >> x;

printVar(x);
changeVar(x);
printVar(x);

return 0;

}
Okay, so i copied this program in the "Dev-C++" Program at my school and when I compile it asks me to enter an integer and after I enter a number and press enter, the screen disappears!

Does anyone know why this may be happening?

Thanks!
Which part of your code do you think should stop it disappearing?

http://www.cplusplus.com/forum/beginner/1988/
boxxer, please don't post full solutions. The reason we don't do homework is for people is because if they just read our solution, they'll never learn anything. And that's how project managers are born.
Thats where they come from!
Boxers solution wouldn't work anyway, since the changeVar function needs to have x passed by reference, not by value.
Sorry for posting the full solution. Also noNotThat, it does work and x is being passed by reference:
void changeVar(int& x) see the reference operator?
closed account (zb0S216C)
see the reference operator?

You've missed the reference operator in the printVar( ) method.
No, Framework, he didn't put it there because you don't need to, and shouldn't be, passing by reference if you're not modifying the variable.
closed account (zb0S216C)
I was falling asleep at the time. Give me a break :/
Programmers don't fall asleep or get tired.... they simply stop coding.
closed account (zb0S216C)
Programmers don't fall asleep or get tired

It's natural for humans to fall asleep. Programmers are humans as well. What you've just said isn't funny.
Topic archived. No new replies allowed.