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
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 &&
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";
}
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.