Compiles yet does not execute.

Hey guys i get an error that says "variable "total" cannot be initialized because it's not defined." or something along those lines. here is my code:

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
40
41
42
43
44
45
46
47
48
49
50
// Assigment 4

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

int main()

{
	string air;
	string water;
	string steel;
	string material;
	int total;
	int seconds;

	// User input


	cout << "Are you interested in air, water, or steel's amount of time traveled? ";
	cin >> material;

	cout << "How many seconds has the sound wave been traveling?";
	cin  >> seconds;

	//Calculations

	air = 1100 * seconds,
	
	water = 4900 * seconds,

	steel = 16400 * seconds;

	
        //determine which variable is used.

	if (material == air)
		total = 1100 * seconds;
	else if (material == water)
		total = 4900 * seconds;
	else if (material == steel)
		total = 16400 * seconds;

	cout << "Your material has traveled " << setprecision(2) << total << ".\n";
	
	return 0;
}


I'm getting a debug error after it compiles and I choose "Start without debugging" that says:

"The variable total is being used without being initialized"

any help would be grand.
Last edited on
line 36 is your problem.

material is a string, and seconds is an integer. You're trying to multiply a string and an int together (which, or course, makes no sense).

What are you trying to accomplish with that line?

EDIT: (also it's missing a semicolon)

EDIT 2:

wait a moment... you're doing that several times. air, water, steel, are all strings.

Last edited on
Perhaps no semicolon on line 36. What line is the error bound to.
EDIT: Wow, I missed the string * int multiplication... I seriously need to get my vision checked.
Last edited on
updated. the original post was a mistake.
You're still doing it with air, steel, water kind of:

1
2
3
4
5
6
	air = 1100 * seconds,  // 1100 * seconds is an integer.  air is a string
   // you can't assign an integer to a string
	
	water = 4900 * seconds,  // same

	steel = 16400 * seconds;  // same 



You also probably mean to be comparing material to string literals below that.

if (material == air)
This checks to see if the string contained in the variable material is the same as the string contained in the variable air.

if (material == "air")
This checks to see if the string contained in the variable material is the same as the string "air".
I'm pretty sure that you can construct a string from an integer so the OP could do this:
string air(1100 * seconds);
But if they are all going to be integers then what's the point of making the variables strings? I kind of see what you are trying to do but that's not really the best solution... strings are supposed to be strings, not numbers...
Last edited on
Ahh i see what you're saying disch. so i need to come up with different variables for air, steel, water. Problem is I'm going crazy deciding how to go about it without conflicting.
airnum waternum steelnum
I don't see why you need strings for air, water, steel at all. Why not just change them to ints?
@Disch

I'm using them as strings so that the user can input them as either air, water or steel as each of these have different unique equations.

My goal is to have the user type in Air then 25 seconds and have the compiler multiply 1100 * 25 as defined by my calculations area.

@tummychow.

i did as you suggested and got this as a result
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Assigment 4

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

int main()

{
	string air;
	string water;
	string steel;
	string material;

	int airnum;
	int waternum;
	int steelnum;
	int total;
	int seconds;

	// User input


	cout << "Are you interested in air, water, or steel's amount of time traveled? ";
	cin >> material;

	cout << "How many seconds has the sound wave been traveling?";
	cin  >> seconds;

	//Calculations

	airnum = 1100 * seconds,
	
	waternum = 4900 * seconds,

	steelnum = 16400 * seconds;

	
        //determine which variable is used.

	if (material == air)
		total = airnum;
	else if (material == water)
		total = waternum;
	else if (material == steel)
		total = steelnum;

	cout << "Your material has traveled " << setprecision(2) << total << ".\n";
	
	return 0;
}


however it is still doing the same thing.
Try changing the ifs and else ifs to
material == "steel" with quotes rather than without. Without quotes, you refer to the variable. With, you refer to the string literal. Steel, water and air are all null strings because you never give them any input.
@tummychow

Bravo! it did it. I think I understand what you are saying. When I did not include the quotes, it was looking for the variable Steel's definition as in "int steel = 25" but adding the quotes tells the compiler to look at what the word, or string, steel is defined as which was "steelnum" which equaled "16400 * seconds".
You got the idea.
I think this is also the way !!!!

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <sstream>
using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{
string material;
string input;
int total = 0;
int seconds;

// User input


cout << "Are you interested in air, water, or steel's amount of time traveled? ";
getline(cin, material);


cout << "How many seconds has the sound wave been traveling?";
while (true)
{

getline(cin, input);

// This code converts from string to number safely.
stringstream myStream(input);
if (myStream >> seconds)
break;
cout << "Invalid number, please try again" << endl;
}



//Calculations

/*air = 1100 * seconds,

water = 4900 * seconds,

steel = 16400 * seconds;*/


//determine which variable is used.

if (material == "air")
{
total = 1100 * seconds;
}
else if (material == "water")
{
total = 4900 * seconds;
}
else if (material == "steel")
{
total = 16400 * seconds;
}

cout << "Your material has traveled " << setprecision(2) << total << ".\n";


return 0;
}
Topic archived. No new replies allowed.