finding the average of sums made in a loop in c++ - c++

Hello everyone I'm trying to find the average of a random amount of numbers that are input into a loop. For sum reason after the loop im able to print the right total, but when i try to find the average i get a weird answer. can anyone help me with this or direct to a thread on here that could help? I wasnt able to find anything on here.
here is my code for the program that isnt working.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inData;
string golferFile;
string golfer;
int matches;
int score;
cout << endl;
cout << "Enter the golfer's filename: ";
cin >> golferFile;
inData.open(golferFile.c_str());
getline(inData, golfer);
inData >> matches;
cout << endl;
cout << "The golfer " << golfer << " has " << matches << " matches with scores"
" of" << endl;
cout << endl;
int count;
count = 1;
int matchNumber;
matchNumber = 1;
int sum;
while(count <= matches)
{
inData >> score;
cout << "Match " << matchNumber << ": " << score << endl;
matchNumber++;
count++;
sum = sum + score;
}
}
int mean;
mean = sum / matches;
cout << "The mean score is " << mean << endl;
return 0;
}
the output i receive is this for the mean
The mean score is 1399255

I found several error in your code.
you forget to initialize your sum variable.
in while loop an extra brace found.remove it
you didn't write anything to stop your loop.that why your loop run infinite time.so initialize you loop also.

Related

How do i tell my app "stop running" after set amount rounds?

So i'm making a turn based dice game that's modeled after this game called "underground chinchiro" which was taken from an anime called "Kaiju". I need to set a limit to my program so that it only runs for a set number of rounds,
I'm only a beginner in coding so sorry for anything unusual you see in my code.
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
void roll_3_dice(int &dice1, int &dice2, int &dice3)
{
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
dice3 = rand() % 6 + 1;
return;
}
int main()
{
int cash = 90000;
int wager;
int r;
//dealer's die
int dealer1;
int dealer2;
int dealer3;
// your die
int mdice1;
int mdice2;
int mdice3;
for (int i = 0; i < 10; i++)
{
cout << "Wager up boy!"<< endl;
cin >> wager;
while (wager < 100 || wager > 90000)
{
cout << "Minimum wager is 100; Maximum wager is 90000 ";
cin >> wager;
}
cout << "You wagered: " << wager << endl;
cout << "You have " << cash - wager << " remaining" << endl;
cash = cash - wager;
cout << endl;
cout << "Dealer will now roll the dice" << endl;
roll_3_dice(dealer1, dealer2, dealer3);
cout << "Dealer rolled the following: " << endl;
cout << dealer1 << "-" << dealer2 << "-" << dealer3 << endl;
cout << "It's your turn to roll the dice." << endl;
cout << endl;
cout << "Press any key to roll the dice" << endl;
cin >> r;
roll_3_dice(mdice1, mdice2, mdice3);
cout << "You rolled the following: " << endl;
cout << mdice1 << "-" << mdice2 << "-" << mdice3 << endl;
system ("pause");
}
}
Take a look at for loops. For loops will allow you to run your code for a set number iteration.
e.g. iterate over some code 7 times.
int number_of_iterations = 7;
for(int i = 0; i < number_of_iterations; i++) {
// Your code that you would like to iterate over goes here.
}
EDIT:
As has been specified by the OP (in comments below), the issue is appears to be with the program not stopping to receive input from the user through each iteration of the for loop.
This could be for a number of reasons. My best guess would be that the stdin buffer is not clear and that std::cin continues to read in from the buffer. This could be solver by calling cin.clear() before reading in your input.
is time to learn how to use constants...
define one doing
const int max_round = 5;
and the do a while so long round is <= than max_round
Your problem is pretty unclear. Edit your code, find the section where problems occurring and paste that part only.
like:
while(cin>>wager)
{
if(condition fails)
{
break;
}
}

Xcode won't recognize/read file properly

I built a program in c++ to calculate the amount of gas money I owe to my dad for my summer job at Domino's. What the program does is read two lines from a file in a do while loop, one for the starting mileage that day and one for the ending mileage of that day. However I just can't get the program to read the file properly. I'm positive I opened it correctly and put it in the right spot (same folder as the .cpp file) but it just won't read it properly. Any ideas?
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
inputFile.open("sample.txt");
int days; // number of days Alisha worked
int count = 1;
int totalMileage = 0;
int addMileage;
int startMileage;
int endMileage;
double avgMileage;
double moneyOwed;
double realOwed;
const double PER_MILE = 0.08125;
cout << endl;
cout << "How many days did Alisha work?" << endl;
cin >> days;
days++;
do
{
inputFile >> startMileage;
cout << "The starting mileage for day " << count << " is " << startMileage << endl;
inputFile >> endMileage;
cout << "The ending mileage for day " << count << " is " << endMileage << endl;
addMileage = endMileage - startMileage;
totalMileage = totalMileage + addMileage;
count++;
cout << endl;
}
while (count != days);

Fixing this simple code

I'm a beginner at coding and I can't fix this code and I'm going crazy. It keeps telling me certain variables were not declared and i'm not sure how to fix it.
#include <iostream>
using namespace std;
int main()
{
int (a = 0), sum;{
cout << "This program should read all integer numbers ";
cout << "to sum until a negative number is entered\n";
exit(0);
}
while(a < 0) {
cout << "Enter an integer number: ";
cin >> a;
sum += a;
}
cout << "The sum is sum\n";
return 0;
}
You did not initialize sum, so it could start with any value.
You have extra layers of pointless { } around for no reason.
Your final cout statement does not actually print the varaible.
Change it to: cout << "The sum is " << sum << "\n";
#include <iostream>
using namespace std;
int main()
{
int a = 0, sum = 0;
cout << "This program should read all integer numbers ";
cout << "to sum until a negative number is entered\n";
do {
cout << "Enter an integer number: ";
cin >> a;
if (a > 0)
{
sum += a;
cout << "The sum is currently: " << sum << "; but this is not yet the final value.\n";
}
} while(a > 0) ;
cout << "The sum is " << sum << "\n";
return 0;
}
Lots of gotchas with your code.
Issues I found and corrected:
You have extra parentheses and curly brackets.
You didn't initialize the variable called sum
You initialize the variable called a to 0 but your loop will only execute while a is less than 0
I had to #include "stdafx.h" above where you included iostream
The line that says "The sum is sum" - the second sum should be outside the double quotes so that it will be treated as a variable rather than text.
This code works, please compare it to yours:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int a = 0, sum = 0;
cout << "This program should read all integer numbers ";
cout << "to sum until a negative number is entered\n";
while (a <= 0) {
cout << "Enter an integer number: ";
cin >> a;
sum += a;
}
cout << "The sum is " << sum << "\n";
return 0;
}

C++ Adding Numbers In A Loop Exercise

I am currently learning to code c++ by using the web page program, where I am doing a course. Now recently I got the following exercise:
Using a while or a do-while loop, make a program that asks the user to enter numbers and keeps adding them together until the user enters the number 0.
I wrote the following code in the hope that it would bring the exercise to conclusion:
#include <iostream>
using namespace std;
int main(void){
int sum = 0;
int number;
do
{
cout <<endl;
cin >> number;
sum += number;
cout << "The total so far is: " << sum << endl;
} while (number != 0);
cout << "The total is: " << sum << endl;
}
Yet when I run the code I get the following feedback from the website (there are two links one on the left and the other on the right):
Instructions of the exercise and
Webpage feedback on the exercise
Can you tell me what am I doing wrong, alternatively can you propose an alternative solution then the code I provided? Thank you for any feedback!
The working code is:
#include <iostream>
using namespace std;
int main(){
int sum = 0, numbers;
do{
cout << "The total so far is: " << sum << endl;
cin >> numbers;
cout<< "Number: "<< numbers;
sum += numbers;
} while (numbers != 0);
cout << "The total is: " << sum << endl;
return 0;
}
You have a mistake in the line cout>>endl;. Also, the output should match the instructions. This is why your answer was "wrong".
I think you should design the exact same output as the instructions.
#include <iostream>
using namespace std;
int main(){
int sum = 0, numbers;
do{
cin >> numbers;
sum += numbers;
cout << "The total so far is: " << sum << endl;
} while (numbers != 0);
cout << "The total is: " << sum << endl;
return 0;
}
As for me then I would write the program the following way
#include <iostream>
int main()
{
int sum = 0;
int number;
std::cout << "Enter a sequence of numbers (0-exit): ";
while ( std::cin >> number && number != 0 ) sum += number;
std::cout << "The total is: " << sum << std::endl;
}
If you need to output partial sums then you can add one more output statement
#include <iostream>
int main()
{
int sum = 0;
int number;
std::cout << "Enter a sequence of numbers (0-exit): ";
while ( std::cin >> number && number != 0 )
{
std::cout << "The total so far is: " << ( sum += number ) << std::endl;
}
std::cout << "\nThe total is: " << sum << std::endl;
}
I am guessing the website is doing a simple comparison check.
Could you remove the first cout << endl;
As that would be closer to the expected output.
As to why you are not getting the "total so far" has me stumped.
Do you see the "total so far" text when ran locally?
You should check whether user inputs a number or a character for making sure the adding operation

C++ strange output when adding integers or doubles together

Im having a problem with adding numeric types together in c++ and cant figure out why its happening, I expect when I input 3 bowling scores together I will get 9 or 9.00, instead I get something crazy like 3.31748e+258, what am I doing wrong? any help will go a long way thanks!
#include<iostream>
#include<cmath>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
/*Coordinate variables*/
double bowlTotal;
double bowlScore;
const int FIVE_PLAYERS = 5;
for( int players = 0; players < FIVE_PLAYERS ; players++ )
{
cout << "Player " << players + 1 << endl << endl;
for( int games = 0; games < 3; games++ )
{
double score;
score = 0;
cout << "Please enter score for game #" << games + 1<< ": ";
cin >> score;
cout << endl;
bowlTotal += score;
}
cout << endl << endl <<"The final score for player #"<< players + 1 << " = " << bowlTotal << endl << endl;
bowlScore += bowlTotal;
}
cout << endl << endl <<"The final team score = " << bowlScore << endl << endl;
system("PAUSE");
return 0;
}
You need to initialize your variables to 0 before you use them, as shown below.
double bowlTotal = 0.0;
double bowlScore = 0.0;
Typically compiler will not do this for you, and the variables will be filled with effectively garbage values, to which you add your scores.
As GManNickG succinctly puts it, reading an uninitialized variable is undefined behavior.
You did not initialize bowlTotal or bowlScore, so they contain garbage.