Is there a better way to do this? The following are my instructions. The problem I am having is after I instruct 'y' its not showing my enter goals scored in the program but its allowing me to enter the number. the calculations are correct but the program has to be done to where the user has to hit yes for 5 games where game 2 totals add to game 1 and game 3 totals add to game 2 and so forth. Hopefully someone understands what I am trying to ask. Thank you for your help. Do I have at least the right idea?
#include <iostream>
#include <iostream>
#include <iomanip>
usingnamespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main ()
{
int goals, num2; //Processing data requirements should be integer
int shotsAttempted, shot2; //Processing data requirements should be integer
float percent = 100.0 * goals / shotsAttempted; //Calculations should be float or double
char answer,
y, n; // is there more data? Y or N
cout << " Enter goals scored: ";
cin >> goals;
if (goals < 0)
{
cout << "Goals entered must be greater than 0.....Please re-enter goals: "<<endl;
cin >> goals;
}
cout << " Enter shots attempted: ";
cin >> shotsAttempted;
if (shotsAttempted < 0)
{
cout << " Shots attempted must be greater than 0......Please re-enter shots attempted: "<<endl;
cin >> shotsAttempted;
}
percent = 100.0 * goals / shotsAttempted;
cout << setprecision (1) << fixed << percent;
cout << "\n The shooting percentage is: " << percent;
// Ask the user if more data is needed to be entered after the first calculation.
cout << "\n Is there more data? y or n? \n";
cin >> answer;
if( answer == y)
cout << " \n Enter goals scored: ";
cin >> num2;
cout << " Enter shots attempted: ";
cin >> shot2;
percent = 100.0 * (goals + num2) / (shotsAttempted + shot2);
cout << setprecision (1) << fixed << percent;
cout << " \n The shooting percentage after two games is: " << percent;
return 0;
}
using y instead of 'y' is like comparing your answer to an unknow variable, note that you dont use "y" , you use "" for multiple characters and '' for only one character
What's to stop a user from entering zero when prompted on lines 23 and 32? Use loops which only terminate when a user enters valid input.
Additionally:
1.) Place lines 11 - 42 in a loop, which terminates when there's no more data.
2.) Line 13 is not an equation, it's an assignment (and a bad one at that, too. You're using unintialized variables. The assignment is pointless to begin with since you effectively discard any garbage value percent holds later on when you perform the actual calculation).
3.) goals / shotsAttempted is an integer division in case it matters.
4.) There's a discrepancy on lines 20 and 22. Zero is not greater than zero.
int main() {
bool do_it_again = false;
float percent = 0.0f;
do {
int num_goals = 0;
while (/*input is invalid*/) {
//ask for user input
//num_goals might be invalid if it's less than zero.
}
int num_shots = 0;
while (/*input is invalid*/) {
//ask for user input
//num_shots might be invalid if it's zero or less than num_goals.
}
//calculate percentage
//print results
//ask user if there is more data
//if there is more data, set do_it_again to true.
} while (do_it_again);
return 0;
}
What are you talking about for number 4 on line 20 and 22. It's how its supposed to be if a user enters a negative number. I don't think you read that part of the program right.
if (goals < 0)
{
cout << "Goals entered must be greater than 0.....Please re-enter goals: "<<endl;
In other words:
If goals is less than zero, tell the user the number of goals must be greater than zero.
What if the number of goals is zero? Zero is neither greater nor less than zero.
ah ok thanks. This is what I have now. I totally had to redo everything just have to figure out my errors. in the calculations department. and i know there is a couple more errors. I 'm learning.
cout << "Enter the number of goals attempted: ";
cin >> numGoal;
while (numGoal < 0)
{
cout << "Error: The number of goals must be greater than 0. Try again: ";
cin >> numGoal;
}
cout << "\nEnter the number of shots scored: ";
cin >> numShots;
while (numShots < 0 )
{
cout << "Error: The number of shots must be greater than 0. Try again: ";
cin >> numShots;
}
totalGoals += numGoal;
totalShots += numShots;
ShootingPercentage = numGoal / (double) numShots * 100.0;
cout << setprecision(1) << fixed << "\nThe shooting percentage of the after after " << numGames << " game(s) is " <<ShootingPercentage <<endl;
numGames++;
cout << "\nIs there more data? (y or n): ";
cin >> Continue;
}
return 0;