Variable is being used without being intialized

variables F,M,A,I are not being initialized near the if statements but I thought I internalized them in the beginning of the code? Any idea where I am going wrong?

#include <iostream>
using namespace std;

int main ()

{

int weight= 0;
int calneed=0;
int F;
int M;
int I;
int A;
int otherresponse;
int response;



cout << " Type F or M for Gender:" << endl;
cin >> otherresponse;
switch (otherresponse)
{
case 1: F;
case 2: M;
}



{
cout << " Type A if your active and type I if your inactive:" << endl;
cin >> response;
switch (response)
{
case 1: A;
case 2: I;
}

}

cout << "Enter your current weight:" << endl;
cin >> weight;

switch (weight)
{
case 1: F;
case 2: A;
}

{

if ( otherresponse==F && response==A)
calneed= weight*12;
else

if ( otherresponse==F && response==I)
calneed=weight*10;
else

if (otherresponse==M && response==A)

calneed=weight*15;
else

if (otherresponse==M && I)
calneed= weight*13;

}

return 0;

}
int is for integers.
Use char if you want to store characters.

I don't know what you're trying to do with your switch statements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    std::cout << "What is your gender? (F/M): ";
    char gender;
    std::cin >> gender;
    if (gender == 'F')
        std::cout << "You responded that you are female.";
    else if (gender == 'M')
        std::cout << "You responded that you are male.";
    else
        std::cout << "You responded that you are a '" << gender << "'. What's that?";
}
Ugh you are trying to use variables as characters. They are also of type int instead of char

Try replacing your switches with the letters and do what you want.

1
2
3
4
5
6
7
8
9
10
11
switch(response)
{
    case 'A':
        std::cout << "You entered 'A'" << std::endl;
        break;
    case 'I':
        std::cout << "You did not enter 'A' but instead 'I'" << std::endl;
        break;
    default:
        std::cout << "Invalid response." << std::endl;
}
Thanks guys just started coding yesterday
Hi & welcome to the forum ! :+D

No one has pointed out to you what initialisation is.

Initialisation means assigning a value to a variable:

int a = 0;

Ideally this should happen when the variable is declared. Use of un-initialised variables is one of the biggest source of errors. So this is a golden rule, to help you out :+D Make sure you initialise your variables with something. Happy days!

I think it will be handy for you to realise the difference between various types: int char double bool , and how they are stored, plus what one can do (and not do) with them.

While I am at it, and seen as it is day 2 for you, it is a great time to get you out of the habit of having using namespace std; Have a go at Googling why that is, or look at some of my recent posts. Just put std:: like giblit and long double main have done.

Just as important, please use code tags from now on: select the code, then press the <> button on the format menu.

http://www.cplusplus.com/articles/z13hAqkS/


Finally, at the top right of this page there is a great tutorial, untold reference material, and articles.

Cheers

Last edited on
Topic archived. No new replies allowed.