cant get this coding to work properly

So first things first, I am taking a C++ course in college and the assignment is the whole Haverly's calculator problem that some of you here may know very well already. I've written the code for it but i get 3 error when i attempt to compile it, and I seriously cant help to fix the issues. the 3 errors are the same one error C4700 on lines 20, 36, and 55 in regards to having an uninitialized local variable used. line 20 is "width_of_room1" line 36 is "length_of_room_2" and line 55 is "radius_of_room". I've noticed that on line 36 "width_of_room_2" isnt giving any error code though.

What I am trying to do is make it so that if a value lower than the number zero the program will let the user know that the input for the length and area and radius is invalid and to restart. I was success getting the first menu selection to do that for me but when it comes to the the area, length, radius, Im at a complete loss. I need help troubleshooting for sure
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  
#include <iostream>
using namespace std;

int main()
{
	double type_of_room, area_of_room, width_of_room_1, width_of_room_2, length_of_room_2, radius_of_room;
	const double pi = 3.14;
	const int room1 = 1, room2 = 2, room3 = 3, room4 = 4, zero = 0;

	cout << "Haverly's Room Calculator: \n***********************************\n";
	cout << "1. Square Room \n2. Rectangular Room \n3. Round Room \n4. Quit \n";
	cout << "\nType of Room: ";
	cin >> type_of_room;


	if (type_of_room == room1)

	{
		if (width_of_room_1 >= zero)
		{
			//square room
			cout << "Please enter the width of the room. ";
			cin >> width_of_room_1;
			area_of_room = width_of_room_1 * width_of_room_1;
			cout << "\nSquare Room \nArea of Room = " << area_of_room << " sq/ft.\n" << endl;
		}
		else cout << "Please restart program and input a valid choice\n";
	}

	else if (type_of_room == room2)

	{

		//rectangular room
		if (width_of_room_2 >= zero, length_of_room_2 >= zero) //come back to work on this invalid input area

		{

			cout << "Please enter the width.\n ";
			cin >> width_of_room_2;
			cout << "Please enter the length.";
			cin >> length_of_room_2;
			area_of_room = width_of_room_2 * length_of_room_2;
			cout << "\nRectangle Room \nArea of Room = " << area_of_room << " sq/ft.\n" << endl;
		}
		else cout << "Please restart program and input a valid choice\n";

	}

	else if (type_of_room == room3)

	{

		if (radius_of_room >= zero)
		{
			//round room
			cout << "Please enter the radius. ";
			cin >> radius_of_room;
			area_of_room = pi * (radius_of_room * radius_of_room);
			cout << "\nRound Room \nArea of Room = " << area_of_room << " sq/ft.\n";
		}

		else cout << "Please restart program and input a valid choice\n";
	}
		

	else if (type_of_room == room4)
	{
		//quit the program
		cout << "\nNow exiting the program.\n";
	}

	else
	{
		cout << "Please restart program and input a valid choice\n";

	}
	cout << "Thank you for using Haverly's Room Calculator.\n";


	system("pause");

	return 0;
}
Hi,

One Golden Rule is to always initialise all of your variables. In C++ the declaration doesn't have to be at the top of the program, it can be anywhere. So, ideally wait until you have a sensible value then declare and initialise all in one statement. Do one of these per line, not multiple variables in one line.

In this case, the values need to be positive, so you could initialise to a negative value. Also validate the input so the values are positive. This will simplify the program.

On line 36 the comma doesn't do what you think. Even constructed properly it doesn't test for rectangular. How about if the are unequal and above zero? The AND operator is &&. If you have already validated the input to be positive, then you don't need the &&

Good Luck !!

Hello,

One additional suggestion would be, if you are allowed to and familiar with it, to use an enum and switch statement.

// It is not strictly necessary to assign numbers besides the first which is Room1 = 1, because everything that follows is assigned 2, 3, 4 ... n but I find this is good practice ...
enum RoomTypes { Room1 = 1, Room2 = 2, Room3 = 3, Room4 = 4, Quit = 5 };

You could then do:

switch(type_of_room)
{
case Room1:
{
....
} break;

case Room2:
{
....
} break;

case Quit:
{
....
} break;

default: std::cout << "Please restart program and input a valid choice\n";
}
Last edited on
Thanks for the tips, I thought about doing the switch first but then i couldnt figure out how to go about making error statements within the program for any invalid input. Closer look into the coding, I see what you mean The IdeasMan, I'm going to to see about fixing the code when i get a chance within the week. This assignment isnt due til the 24th I believe so i got time to mess around and figure out the best way to do the code, as I do find the way I wrote it to be a bit messy.
Registered users can post here. Sign in or register to post.