0

Trying to make a simple function to return a value stored in a secondary function to the main function in DEV-C++ and I'm not sure why it's not working :/

I really feel like it should be running correctly but when I compile and run the program it does not ask to enter a value and displays 0 and the sentence that goes with the statement when 0 is the entered value.

// This program asks user to enter IQ score and then displays a message

#include<iostream>
using namespace std;

double getIQ();

int main()
{
    double iq = 0.0;

    getIQ();

    if (iq > 120)
    {
        cout << "You are a genius" << endl;
    }
    else
    {
        cout << "You don't need a high IQ to be a successful person" << endl;
    }

    system("pause");
    return 0;
}

double getIQ()
{
    double iq = 0.0;

    cout << "Enter IQ score: " << iq << endl;
    return iq;
}
user2040308
  • 53
  • 1
  • 8
  • `cout` only outputs things. Use `cin` to input something. You also don't use the return value from the function. You simply execute it and throw away the IQ. – chris Mar 20 '13 at 02:21
  • `cout` does not read input: http://www.cplusplus.com/reference/iostream/cin/ – Mike D Mar 20 '13 at 02:21

3 Answers3

3

There are two issues you are not assigning the return value of getIQ():

 double iq = getIQ() ;

and you need a cin in getIQ():

double getIQ()
{
    double iq = 0.0;

    cout << "Enter IQ score: " ;
    cin >> iq ;
    return iq;
}

Also, I would advise against' using system("pause"), please see System(“pause”); - Why is it wrong? and against using using namespace std;, please see Why is 'using namespace std;' considered a bad practice in C++?.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • I made the changes and that helps, but now when it prompts me to enter in IQ it already has a 0 but it still lets me enter in a value, but then it repeats asking me to enter a value and after entering the value again it runs the rest of the program – user2040308 Mar 20 '13 at 02:30
  • ok I figured it out. I had both: double iq = getIQ(); getIQ(); – user2040308 Mar 20 '13 at 02:33
2

You're very close,

you just need to store the value returned from the getIQ function into iq:

iq = getIQ();

this will store the double returned by getIQ into the iq variable that you set up earlier.

you can also do:

double iq = getIQ();

To display the stuff in getIQ you should change your input getting line to:

cout << "Enter IQ Score";
cin >> iq;
cout << endl;

Don't forget to check if they input a number or not though.

Serdalis
  • 10,296
  • 2
  • 38
  • 58
1

The problem is that you did not assign the returned value of getIQ() to iq and your getIQ() function is not receiving user input. Actually you can get rid of that iq variable by doing some refactoring: Inline temp (according to Martin Fowler's refactoring book). since iq is a temp variable to hold the return value of the getIQ() function. This way you avoid getting errors. Meanwhile, you need to make your getIQ() function return the user input as indicated in other answers.

int main()
{
    if (getIQ() > 120)
    {
        cout << "You are a genius" << endl;
    }
    else
   {
       cout << "You don't need a high IQ to be a successful person" << endl;
   }

   cin.get();//you can use cin.get() in C++;
   return 0;
}
taocp
  • 23,276
  • 10
  • 49
  • 62