Infinity comes up as an undesired output

Hello, I am supposed to make a program in c++ that takes user inputs of goals and the shots a person in made in soccer, and then use this information to come up with the percentage of shots that actually made a goal. However, when I run the code, it comes out to the percentage being inf, which isn't right. I'll put the code below, any help would be appreciated and thank you!

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
    int goals,shots,totalGoals,totalShots,games;        //Introducing variables
    double result;
    string moreData = "y";
    
    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;
        }
 
        cout << "Enter the number of shots that were attempted: "; //Defining shots through user input
        cin >> shots;
        while (shots <= 0) //if goal input is less than 0, error message. Or else, nothing added.
        {
            cout << "\nThe number of shots must be greater than 0. Please try again: ";
            cin >> shots;
            totalShots = shots + totalShots;
        }
        result = (double)totalGoals/totalShots;       //Defining result through division of goals and shots
           result = result * 100;      //Making percentage
           
           cout << "\nThe team shooting percentage after " <<games<< " game(s) is " <<fixed<<setprecision(1)<< result <<endl; //Displaying the result
        
            cout << "\n Is there more data? y or n: ";  //Asking user if they have more data
              cin >> moreData;
           
    }
   
}
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
Hello kerbealbor,

Upon testing the program I found:
while (cout << "Enter the number of goals that were scored: " && std::cin >> goals && goals < 1). I found the third && was needed although I have not tried anything else yet.

Line 29 should be after the while loop to work correctly. Otherwise you only add to "totalShots" if "shots" only if shots is < 1.

Lines 15, 29 and 32 can be written as:
1
2
3
4
5
totalGoals += goals;

totalShots += shots;

result *= 100; 

And I adjusted the "cout" as:
 
cout << "\nThe team shooting percentage after " << games << " game(s) is " << fixed << setprecision(1) << result << "%\n"; //Displaying the result 


"endl" is not needed at the end of every "cout". The new line, (\n), will usually be enough.

Andy
Thank you much Andy!

I implemented your advice, and the program now works. This has helped tremendously and I have learned more, so hopefully I can avoid such mistakes in the future!

Kerbe
1
2
3
4
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: ";
}


If a non-numeric value is entered, then the stream state will be invalid which needs to be reset before further input can be done. So:

1
2
3
4
5
6
while (cout << "Enter the number of goals that were scored: " && (!(std::cin >> goals) || goals < 1))
{
    cout << "\nThe number of goals must be greater than 0. Please try again\n";
    cin.clear();
    cin.ignore(1000, '\n');
}

Last edited on
Topic archived. No new replies allowed.