Logic Error

I'm trying to run a code that does a simple calculation of (x/13.772)* 30 with x being any value but the problem is the code stops prematurely at the point where it asks to rerun the code again. (Calculation works perfectly fine, so it doesn't seem like it's the problem)

ps... I know the names suck :P.

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
#include <iostream>
#include <string>

void Welcome();
float GeoValue();
void AnswerValue();
bool FightMe();


int main()
{
	bool bPlayAgain = true;

	do
	{
		Welcome();
		AnswerValue();
	} 
	while (bPlayAgain == FightMe());

	return 0;
}


void Welcome()
{
	std::cout << "TO CALCULATE THE TIMESCALE! WELCOME!\n";

	return;
}

float GeoValue()
{
	float value = 0;
	std::cout << "Enter your value." << std::endl;
	std::cin >> value;

	return value;
}

void AnswerValue()
{
	const float TimeScale = 13.772;
	const int LineMeasure = 30;

	float ValueOne = GeoValue();
	float Answer = (ValueOne / TimeScale) * LineMeasure;
	std::cout << "Your answer is " << Answer << "." << std::endl << std::endl;

	return;
}

bool FightMe()
{
	std::cout << "Do you want to do it again?\n";
	std::string PlayAgainPls = "hi";
	getline(std::cin, PlayAgainPls);
	return (PlayAgainPls[0] == 'y') || (PlayAgainPls[0] == 'Y');
}
Last edited on
closed account (48T7M4Gy)
An innovative re-use of somebody's game-code.

PlayAgainPls has a number of problems and the easiest is to make it a char instead of string with the need for additional lines to ignore() stuff etc

1
2
3
4
5
6
7
bool FightMe()
{
	std::cout << "Do you want to do it again?\n";
	char PlayAgainPls = 'y';
	std::cin >> PlayAgainPls;
	return tolower(PlayAgainPls) == 'y';
}
getline(std::cin, PlayAgainPls); is unformatted input; it won't skip leading white space.

Change that to formatted input (which skips leading white space) with:
1
2
3
4
5
6
7
8
9
10
bool FightMe()
{
	std::cout << "Do you want to do it again?\n";
	std::string PlayAgainPls = "hi";

	// getline(std::cin, PlayAgainPls);
        std::cin >> PlayAgainPls ;

	return (PlayAgainPls[0] == 'y') || (PlayAgainPls[0] == 'Y');
}

and things would work as expected (we ignore input failure for now).

More information: http://www.cplusplus.com/forum/general/113238/#msg618762
The code works for the tutorial that I'm going through, so I honestly don't know why it runs entirely differently on mine. Is there some kind of hidden interactions that I'm not aware of?

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
#include<iostream>
#include<string>


void PrintIntro();
void PlayGame();
std::string GetGuess();
bool AskToPlayAgain();

//entry point
int main()
{ 
	bool bPlayAgain = true;

	do 
	{
		PrintIntro();
		PlayGame();
	}
	while (bPlayAgain == AskToPlayAgain());

	return 0; // exit app
}

//introduce game
void PrintIntro()
{
	constexpr int WordLength = 5;
	std::cout << "Welcome to Bulls and Cows!\n";
	std::cout << "Can you guess the " << WordLength << " letter isogram that I'm thinking of?\n";
	std::cout << std::endl;
	return;
}

//iterate 5 times for GetGuess();
void PlayGame()
{
	constexpr int NumberOfTurns = 5;
	for (int GuessIteration = 1; GuessIteration <= NumberOfTurns; GuessIteration++)
	{
		std::string Guess = GetGuess();
		std::cout << "Your guess was " << Guess << "." << std::endl << std::endl;
	}
	return;
}

//get guess from player
std::string GetGuess()
{
	std::string Guess = "";
	std::cout << "Enter your guess: ";
	getline(std::cin, Guess);
	return Guess;
}

bool AskToPlayAgain()
{
	std::cout << "Do you want to play again? (y/n)";
	std::string Response = "";
	getline(std::cin, Response);

	return (Response[0] == 'y') || (Response[0] == 'Y');
}

Last edited on
closed account (48T7M4Gy)
What is the program doing? What do you expect it to do?
Well, it's an incomplete game because I haven't done the tutorial completely yet but it's supposed to be Bulls and Cows and I'm not good at explaining but it basically lets you say stuff five times then at the end it asks you to whether you want to play it again as of now...

It's the last function that bothers me, it's exactly the same as the one in my previous code but the previous code just halts at the point where it asks you whether you want to play again while this one works perfectly fine.
Topic archived. No new replies allowed.