char variable not changing? :(

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):

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
86
87
/*

 * 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>

using namespace std;

int main()
{
	typedef const unsigned int conit;
	typedef unsigned int 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!___
	}
	else if (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.
Last edited on
If someone enters f.e. 'q' at line 64, we'll reach line 79 next. Think about what does happen after taking some input at line 81.

A solution might be a loop around your game. F.e.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cctype>

int main(int argc, char *argv[])
{
    //...
    bool finished = false;

    //...

    while (! finished)
    {
        char answer;
        //...
        cin >> answer;
        //...
        finished = std::tolower(answer) == 'y';
    }

    return EXIT_SUCCESS;
}
FIX:

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
*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;
		}

		else if (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;
}


Oh man, I feel like a God right now. :D
Last edited on
Topic archived. No new replies allowed.