Edit:
I managed to fix this problem using a do while loop. :)
(I assumed that C++ code re-loops (globally, as in Java) unless you explicitly tell it to stop (with {return 0;} ).
Original Question:
Hey guys, my char variable "answer" won't change after being defined. Is this right? The problem happens when
// FUEL STATION EXITED (line 59):
/*
* This is my version of a "Hello World!" program in C++.
* For now, ints followed by a ".00" string were used instead of doubles. (no real reason)
* "conit", "unit" and other typedefs may become a standard of my own mini-library. (Feel free to voice your opinion on that)
*/
#include <iostream>
usingnamespace std;
int main()
{
typedefconstunsignedint conit;
typedefunsignedint unit;
conit PRICE_FUEL = 50;
bool fuelStationVisit = true;
char answer;
unit balance = 500;
unit costInput;
unit fuel = 0;
unit fuelInput;
// FUEL STATION ENTERED
cout << "Balance: " << balance << ".00; " << "Fuel: " << fuel << ".00\n\n";
cout << "Welcome to our fueling station. \nOur fuel costs: " << PRICE_FUEL << ".00 credits.\n\n";
cout << "How much fuel would you like to buy?\n";
cin >> fuelInput;
while (fuelStationVisit) {
if (fuelInput * PRICE_FUEL > balance)
{
cout << fuelInput << ".00 fuel will cost " << fuelInput * PRICE_FUEL << ".00 credits.\n";
cout << "Transaction FAILED. Please type another amount.\n";
cin >> fuelInput;
}
else
{
fuel += fuelInput;
balance -= fuelInput * PRICE_FUEL;
cout << fuelInput << ".00 fuel will cost " << fuelInput * PRICE_FUEL << ".00 credits.\n";
cout << "Transaction SUCCESSFUL. Have a great day.\n\n";
cout << "Balance: " << balance << "; " << "Fuel: " << fuel << "\n\n";
fuelStationVisit = false;
}
} // FUEL STATION EXITED
cout << "GAME OVER!\n";
cout << "Would you like to quit the game (y or n)?\n";
cin >> answer;
if (answer == 'n')
{
cout << "Well, this is all she wrote really.\n";
cout << "Sorry, I guess you'll have to quit either way.\n";
system("PAUSE");
return 0;
// ^ ___REFACTOR LATER!___
}
elseif (answer == 'y')
{
return 0;
}
// BUGGY CODE:
else
{
cout << "Please answer either 'y' (yes) or 'n' (no).\n";
cout << "Doing otherwise will not grant you absolution.\n";
cin >> answer;
}
}
The idea is to terminate the program ONLY when you type 'y' (default) or 'n' (temporarily) when prompted. But even if I type a different char, two times in a row, the program also terminates.
Only thing I can think of is that I'm using { cin = answer; } wrong. I'll try making a do while loop or something, unless you guys have a better idea.
*snip*
} // FUEL STATION EXITED
cout << "GAME OVER!\n";
cout << "Would you like to quit the game (y or n)?\n";
do
{
cin >> answer;
if (answer == 'n')
{
cout << "Well, this is all she wrote really.\n";
cout << "Sorry, I guess you'll have to quit either way.\n";
system("PAUSE");
return 0;
}
elseif (answer != 'y' && answer != 'n')
{
cout << "Please answer either 'y' (yes) or 'n' (no).\n";
cout << "Doing otherwise will not grant you absolution.\n";
}
else {}
} while (answer != 'y');
return 0;
}