Trouble with my assignment again..

Apr 5, 2013 at 1:34am
Whenever I try to start debugging it, it says 'M' is being used without being initialized...I want to rip my hair out -_______-

What I have to do:
A void function named mostPowerful

o That accepts three Jetpack power output measurements in MW as double values.

o Returns the largest power output and the number of the Jetpack as parameters.

o Return a 1 for the number if the first Jetpack’s power output is the largest, a 2 if the second Jetpack’s power output is the largest, and a 3 if the third Jetpack’s power output is the largest.

What I have:
#include <iostream>
using namespace std;

char menu();
void mostPowerful(double, double, double);

int main ()
{
char M, D, H;
double num1, num2, num3;
char option = menu();


if (option = M)
{
mostPowerful(num1, num2, num3);
}

system("PAUSE");
return 0;
}

char menu()
{
char option;
cout << "Welcome to Jim’s Space Travel Company!" <<endl;
cout << endl;
cout << "(M)ost Powerful Calculation" << endl;
cout << "(D)iscount Calculation" << endl;
cout << "(H)ow Many Calculation" << endl;
cout << "(Q)uit" << endl;
cout << "Please enter the option (M,D,H, or Q): ";
cin >> option;
return option;
}

void mostPowerful(double, double, double)
{
double numb1, numb2, numb3;
cout << "Please enter 3 power output measurements in MW: ";
cin >> numb1, numb2, numb3;

if (numb1 > numb2, numb3)
{
cout << "The largest power output is" << numb1 << " and is Jetpack number 1" << endl;
}
}
Apr 5, 2013 at 1:41am
closed account (3CXz8vqX)
Why not just initialise them with '\0' if it's throwing that it hasn't been initialised.

It's probably because you're calling it in if(option = M) without it actually having anything contained in the variable.

Please please use code tags...

Edit. You should always initialise everything anyway, it's good practice =D.
Last edited on Apr 5, 2013 at 1:42am
Apr 5, 2013 at 1:46am
Take a look at line 14, counting your first include directive as line 1.

You have "option = M," which is trying to assign option with the value stored in M.

Use option == M.
Apr 5, 2013 at 1:47am
if (option = M)

Should be

if (option == M)

And while I'd harp on you for using namespace STD, as well as system("PAUSE"), It will be fine for a program of this simplicity... just try to not use either of those on future programs. There's a forum thread on the first page of this subforum explaining why not to use system("PAUSE"). As for using namespace STD... well, the example I recall reading is that say you have two namespaces, foo and bar. Say that foo has the function quut(int a), which you use in a program. However, if bar were to update and suddenly have a function quut(int a) with an entirely different function, there would be quite the issue in your code. Hence why it is poor coding practice to use namespace STD. What you can do instead is state STD:: in front of every instance of a function that is namespace STD, such as cout, cin, and string. Also, \n is your friend.
Apr 5, 2013 at 2:13am
I have no idea what you guys are saying. I tried the if (option == M) but it said the same thing. My teacher is being difficult, he never really told us about using namespace std and system("PAUSE")...he just says we need it in order for the program to work.
Apr 5, 2013 at 2:17am
Try

if(option == 'M')
Apr 5, 2013 at 2:21am
Nataleilani, you never initialized M and provided no way to assign it a value before the if statement.

What you are looking for is:
 
char M = 'M'; //and initialize other variables 


Or you could do as Ispil suggested and not use variables, placing the characters in the tests themselves.
Last edited on Apr 5, 2013 at 2:24am
Apr 5, 2013 at 2:24am
Yes, M is a variable- and it doesn't need to be. He could instead just not make it a variable due to it being otherwise pointless to have as a variable. He's checking whether option is equal to M, which in this case based on what his code is, should hold the letter M. I think that is the confusion.
Apr 5, 2013 at 2:27am
Sorry about that Ispil, I only realized what you meant seconds after I submitted my post. I edited it while you were replying coincidentally.
Apr 5, 2013 at 2:44am
Can you please explain that. I'm sorry I'm not comprehending..english is not my first language. You are saying that I shouldn't use variables? I'm sorry I'm irritating and not understanding..
Apr 5, 2013 at 2:49am
Instead of:
1
2
3
    char M = 'M';
//...
    if(option == M)


Ispil is stating that you can simply use:
1
2
3
   //char M = 'M';  //Variable commented out so it is not used
//...
   if(option == 'M')


Of course, the choice is up to you.
Last edited on Apr 5, 2013 at 2:49am
Topic archived. No new replies allowed.