Hello all, i'm trying to create a program using fstream that calculates the sum, and average in the file. I'm kinda lost and can't really ask questions since stupid corona cancelled classes. Here is the exact prompt:
You will create a C++ program that opens a file, numbers.txt, and reads all numbers in the file using a while() loop. Refer to the Files Notes.
While reading the numbers the program will keep track of the count of numbers read while also computing a running sum of all the numbers.
When the while() loop is done, the program will close the input file and compute the average of all numbers.
The numbers in the file numbers.txt are all integers. The sum should be an unsigned integer. However, the average must be a double.
My program: (Is it already reading the file I created with the numbers I have? when I hit run It just says press enter to close program?
Sorry If it's a lot of reading or sloppy the website won't let me bold stuff, any help is appreciated. My teacher preset the program like this except for a few things I added but i'm pretty lost on what to do.
Also, the website works just fine. There are plenty of little buttons on the right to help you format stuff, like bolding text and pretty-printing your source code. You actually had to delete some of it to post your question without it!
What you have so far is very, very basic, but more or less correct — except you are not being exact. For example, your prompt says the input file will be named “numbers.txt”, yet your program opens a file named “ListOfNumbers.txt”.
#include <fstream>
#include <iostream>
usingnamespace std;
int main()
{
unsigned sum; // "The sum should be an unsigned integer."
double average; // "However, the average must be a double."
// what else do you need?
// "...opens a file, numbers.txt, and reads all numbers in the file using a while() loop"
ifstream inputFile( "numbers.txt" );
while (inputFile >> n)
{
// do something here
}
// "When the while() loop is done, the program will close the input file..."
inputFile.close();
// ...and do something here
// Your instructions didn't say anything about printing the average,
// but you probably ought to do that anyway.
cout << "The average is " <<
}
You need two pieces of information to compute an average. So far you are missing one of them. (Your instructions mention it!)
Something to remember is that in C and C++ at least one operand must be a floating point value to get a floating point result during division. You can guarantee this by casting:
I edited my code a bit with your idea but I can't figure out what mathematical operation to use to add up the numbers in the file? The output of the program says read 20 numbers from the file while they're are actually like 50 in the file soo that means I still add up all 50 or however many there are right? This is what I mean if im confusing you, Teachers prompt: The sum should be an unsigned integer. However, the average must be a double.
Your output will look like this (your numbers will be different):
Read 20 numbers from file.
Sum of numbers is 97365.
Average of numbers is 4868.25.
#include <iostream>
#include <cmath>
#include <fstream>
usingnamespace std;
int main()
{
int number = 0; // numbers being used
unsigned sum = 0; // sum is unsigned
double average = 0.0; // average is double
ifstream inputFile("numbers.txt"); // Document containing numbers
while (inputFile >> number)
{
// dont know what mathmatical operation to do? But it calculates the running sum
}
inputFile.close(); // Closes File
// calc average here but how?
cout << "The average is " << endl;
return 0;
}
obv its addition but I don't know how to incorporate it into the program, im not gonna write 20 different numbers out is there not a simpler way to write it?
The while (inputFile >> number) part will read the next number every time it's executed. So to just print out the numbers:
1 2 3 4
while (inputFile >> number)
{
cout << number << '\n';
}
So the ++count is not necessary to skip to the next number.
It's just in the body of the loop to count how many numbers are read.
And of course sum += number is there to accumulate the sum.
so when I run my program to test the sum, why does it display the sum as zero, I know I declared it in the beginning but shouldnt your recommendation now make my cout statement add up all the numbers?
int count = 0; // numbers being used
unsigned sum = 0; // sum is unsigned
double average = 0.0; // average is double
int number;
ifstream inputFile("numbers.txt"); // Document containing numbers
while (inputFile >> number) // read to next number, loop
{
++count; // counts how many #'s are executed
sum += number;// accumulates the sum
}
inputFile.close(); // Closes File
// something that calculates average
cout << "The sum is " << sum << endl;
cout << "The average is " << average << endl;
return 0;
Probably the file isn't opening. Try putting a test in:
1 2 3 4 5 6
ifstream inputFile("numbers.txt");
if (!inputFile)
{
cout << "Cannot open file.\n";
return 1;
}
And to calculate the average:
average = double(sum) / count;
Note that you need to cast at least one of the integers on the right-hand-side to floating point in order for the division to be a floating point division instead of integer division.
yea your right my program displayed could not open file, I have my text document labeled as "number" so when entering it into the program I should just have to enter "number.txt". I'm not sure why it won't read it.
Is it "number" or "numbers"?
You need to get that right.
You could try it with and without the .txt part.
You could try moving the file to a different location.
I don't use Windows so I'm not sure where it should go.
Probably either in the directory with the program or in the directory above that.
I'm on windows I tried saving it in like 5 different folders and compiling but my program just can't find the file, and it is numbers, I also saved it in the file where my program is located.