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.
#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!
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.
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?