English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

we're learning functions in my C++ class, he's given us function main, we're to write sub-functions that'll complete the task libraries are just iostream. I need to return a value to be stored in the variable userInput, but i can't think of a way to do that without modifying the function main (which i'm not allowed to do) or adding a global variable(which i'm not allowed to do) I don't need the code, just a hint (sorry yahoo answers removes my spacing before my code)

// function get Input has already been declared

int main (void)
{
int userInput;
char letter grade;
char gradeSign;

cout << "input number between 1 and 100\n (an invalid value terminates the program)\n";

while (getInput (userInput))
{
//unrelavant code that uses the variable userInput
}
}

//function i've written so far
int getInput (int getInputVar)
{
cout << "Numeric grade: ";
cin >> getInputVar;
if (cin.fail() || getInputVar <0 || getInputVar > 100)
return 0;
else
return getInputVar;

2006-10-24 18:10:12 · 4 answers · asked by duffusd 3 in Computers & Internet Programming & Design

when i normally run the compiler, it ouputs a random number when i tell it to output userInput

2006-10-24 18:12:58 · update #1

i'm trying to assign the variable userInput in function main() the getInputVar, and i'm not allowed to modify function main() at all

2006-10-24 18:19:35 · update #2

the problem with all of your suggestions (thank you for them btw) is that they all revolve around using userInput as a global variable, its only available in function Main, and i can't change function main at all... and userInput isn't a global variable (i can't change that either) i think its impossible to do it, but my teacher insists that it is possible

2006-10-24 19:25:41 · update #3

4 answers

I am not positive what you are trying to do, are you asking how to let the "return getInputVar;" return to userInput? If so, there's a simple way to do it. Remember that you want the return value to return to something (i.e. using a =...). Hopefully that's enough of a hint.

2006-10-24 18:17:24 · answer #1 · answered by asleep 2 · 0 0

Can you access userInput from the getInput function you've wrote? Try adding userInput = getInputVar; just after the else and before the return statement at the end, and see if it lets you compile. The reason the program outputs random numbers for userInput right now is because you've declared the variable but haven't assigned any value to it yet, so the value stored at that spot in memory is just whatever happened to be stored there last time that memory spot was used by some program. This is called a garbage value because it means absolutely nothing to you or your program.

2006-10-24 18:29:51 · answer #2 · answered by Anonymous · 0 0

1) how is the function "getinput()" declared? Is it declared with a return value, or no return value? example: int getinput() return value, getinput() no return value.

2) how is getinputVar declared in the arguments list for getinput() function? is it "int getinputVar" or "int* getinputVar"?

3)userinput is never assigned a value before being passed to the getinput() function (just poor programming practice, unless it is a pointer to userinput).

2006-10-25 02:20:04 · answer #3 · answered by justme 7 · 0 0

u can use main function to call that function and return its value in a variable as::


main()
{

var = fun( parameter list );

}

return type fun( parameter list );
{

return( what u want );
}

var and return type of function should be compatible

2006-10-24 18:30:56 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers