Program executing although answer is NO?
Oct 31, 2009 at 12:50pm UTC
Hello I`m trying to make a simple program to enter info about TVSeries (doesn`t save anything anywhere, just did it for fun)
So, I have the user confirm at a certain point if the info is correct where he inputs y or n. When the answer is y everything goes according to plan. But when the answer is anything different than that, the program keeps executing as if the answer was y.
Here is my code:
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
#include <iostream>
char SeriesName[128];
int Season;
int SeasonsInSeries;
int Episode;
int EpisodesInCSeason;
char answer[1];
bool Seasons()
{
std::cout << "How many seasons are in " << ::SeriesName << "?" << std::endl;
std::cin >> ::SeasonsInSeries;
std::cout << "Enter the season of the series you are currently in: " << std::endl;
std::cin >> ::Season;
if (::Season > ::SeasonsInSeries)
{
std::cout << "Invalid argument" << std::endl;
Seasons();
}
else
{ return true ; }
}
bool Episodes()
{
std::cout << "How many episodes are in season " << ::SeasonsInSeries << " in total?" << std::endl;
std::cin >> ::EpisodesInCSeason;
std::cout << "Enter the episode of the series you are currently in: " << std::endl;
std::cin >> ::Episode;
if (::Episode > ::EpisodesInCSeason)
{
std::cout << "Invalid argument" << std::endl;
Episodes();
}
else
{ return true ; }
}
void UInput()
{
std::cout << "Enter the name of the series you want to check: " << std::endl;
std::cin >> ::SeriesName;
Seasons();
Episodes();
std::cout << "So you are watching " << ::SeriesName << " and currently are in episode " << ::Episode << " of season " << ::Season << "." << std::endl;
std::cout << "Is this information correct? (y/n)? " << std::endl;
std::cin >> ::answer;
if (::answer[0] = 'y' )
{
if (::Episode < ::EpisodesInCSeason)
{
std::cout << "Season " << ::Season << " of " << ::SeriesName << " is not yet completed" << std::endl;
}
else if (::Episode == ::EpisodesInCSeason)
{
std::cout << "You have completed season " << ::Season << " of " << ::SeriesName << std::endl;
if (::Season == ::SeasonsInSeries)
{
std::cout << "You have completed the series '" << ::SeriesName << "'." << std::endl;
}
}
}
else
{
std::cout << "Re-enter information" << std::endl;
std::cout << " " << std::endl;
UInput();
}
}
void main()
{
std::cout << "Welcome to the TVSeries Database Check" << std::endl;
UInput();
}
Last edited on Oct 31, 2009 at 1:24pm UTC
Oct 31, 2009 at 2:01pm UTC
if (::answer[0] = 'y' )
You have to use the == operator
Oct 31, 2009 at 2:05pm UTC
Thank you :)
Topic archived. No new replies allowed.