Problem with uninitialized local variable error

Hey guys I am new to programming and have recently started using C++ for one of my classes. As you can probably tell from the code we are just starting out. The error I keep getting is the "uninitialized local variable" and "unreferenced local variable" for all of my variables I enter. I tried using the "int" and "double" commands but nothing seems to work. Is there any other way to do it? This is an exercise for my class that needs to be completed if anyone wants that actual directions for the problem (if that will help just let me know). I have been stumped on this for about 2 hours now. I am also not sure if I am using the "wholebody" and "deadlift" responses right. They are supposed to be 2 words actually instead of 1 but every time I tried putting them as "whole body' or "dead lift" it kept saying that was wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;
int main()
{
	double exercise;
	double chinup;
	double pushup;
	double press;
	double row;
	double deadLift;
	double biceps;
	double pecs;
	double back;
	double wholeBody;
	double curl;

	cout <<"Please enter an exercise:";
	cin >> exercise;
	if ((curl) || (chinup))
	{
		cout << "This exercise is for your biceps.";
	}
	cout << endl;
	if ((pushup) || (press))
	{
		cout << "This exercise is for your pecs.";
	}
	cout << endl;
	if (row)
	{
		cout << "This exercise is for your back.";
	}
	cout << endl;
	if (deadLift)
	{
		cout << "This exercise is for your whole body.";
	}
	cout << endl;
}
It looks like you are going about this the wrong way. Seeing as I don't know the question you were given or what stuff you are allowed to use...it's hard to tell you exactly how to fix it.

You should only need 1 variable called exercise for the user to work with, which would be a string (as it looks like you want the user to enter in words. Then compare that. For example

1
2
3
4
5
6
7
string exercise;
cin >> exercise;

if(exercise == "curl" || exercise == "chinup")
{ 

}
Well here is the directions for the problem: ( The class has just started and we are only 4 weeks in it so I do not think we have gotten to "string" yet. As of right now the unit is focusing on "if" and "else" commands. Also this unit is focusing on the logical connectives "&& and ||" I would assume the exercise wants me to use these commands to get my program run. Beyond that not much else has been introduced. I replaced the code like you suggested but now I am getting new errors in my compiler and not sure where to go from here James2250.

"Write a program that asks for the user to input an exercise and responds accordingly, based on the answer as shown in the following chart. Be sure to combine the conditions appropriately; do not make a separate "if" statement for each possible answer."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
int main()
{
	string exercise;
	cin >> exercise;

	if ((exercise == "curl") || (exercise == "chinup"))
	{
		cout << "This exercise is for your biceps.";
	}
	cout << endl;
	if ((exercise == "pushup") || (exercise == "press"))
	{
		cout << "This exercise is for your pecs.";
	}
	cout << endl;
	if (exercise == "row")
	{
		cout << "This exercise is for your back.";
	}
	cout << endl;
	if (exercise == deadLift)
	{
		cout << "This exercise is for your whole body.";
	}
	cout << endl;
}

Compiler Errors:
1>c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(6): error C2371: 'exercise' : redefinition; different basic types
1> c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(5) : see declaration of 'exercise'
1>c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(7): error C2088: '>>' : illegal for class
1>c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(9): error C2446: '==' : no conversion from 'const char *' to 'int'
1> There is no context in which this conversion is possible
1>c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(9): error C2040: '==' : 'int' differs in levels of indirection from 'const char [5]'
1>c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(9): error C2446: '==' : no conversion from 'const char *' to 'int'
1> There is no context in which this conversion is possible
1>c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(9): error C2040: '==' : 'int' differs in levels of indirection from 'const char [7]'
1>c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(14): error C2446: '==' : no conversion from 'const char *' to 'int'
1> There is no context in which this conversion is possible
1>c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(14): error C2040: '==' : 'int' differs in levels of indirection from 'const char [7]'
1>c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(14): error C2446: '==' : no conversion from 'const char *' to 'int'
1> There is no context in which this conversion is possible
1>c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(14): error C2040: '==' : 'int' differs in levels of indirection from 'const char [6]'
1>c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(19): error C2446: '==' : no conversion from 'const char *' to 'int'
1> There is no context in which this conversion is possible
1>c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(19): error C2040: '==' : 'int' differs in levels of indirection from 'const char [4]'
1>c:\users\administrator\documents\visual studio 2010\projects\chap05_proj5c\chap05_proj5c\chap05_proj5c.cpp(24): error C2065: 'deadLift' : undeclared identifier
Last edited on
Topic archived. No new replies allowed.