Hello kerbealbor,
Just looking at the program.
while (moreData == "y")
, but what if "yes", "YES", "yyyyy" or even just "Y" is entered into "moreData". Will it ever be equal to "y"?
With "moreData" as a string more than 1 character can be entered and it would be acceptable.
Try this
while (std::tolower(moreData[0] == 'y'
. Notice the single quotes. "std::tolower()" and "std::toupper()" need the header file "cctype".
Any variable used for a total should be initialized to 0 before it is used. And it would best to initialize all variables. From C++11 on this is very easy.
int goals{}, shots{}, totalGoals{}, totalShots{}, games{};
. The empty {}s will set the variables to (0).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
while (moreData == "y")
{
games++;
cout << "Enter the number of goals that were scored: "; //Defining goals through user input
cin >> goals;
totalGoals = goals + totalGoals;
while (goals <= 0) //if goal input is less than 0, error message. Or else, nothing added.
{
cout << "\nThe number of goals must be greater than 0. Please try again: ";
cin >> goals;
}
|
Line 6 enters the number of "goals", but this could be (0) or less at this point.
Line 8 could look something like this to start with
-858993460 = 0 + -858993460;
. Not what you are wanting. Even after the rhs of = is added nothing will change.
Line 10 is checking the value of "goals" to late.
What you could do is:
1 2 3 4 5 6
|
while (cout << "Enter the number of goals that were scored: " && (std::cin >> goals) < 1)
{
cout << "\nThe number of goals must be greater than 0. Please try again: ";
}
totalGoals = goals + totalGoals;
|
My thoughts for now until I have a chance to test it.
Andy