#include <iostream>
#include <fstream>
using namespace std;
int const NUM = 5;
int main()
{
float values[NUM];
ifstream inData;
float average;
float standardDev;
int sum = 0;
int temp;
inData.open("input.dat");
for(int i = 0; i < NUM; i++)
{
temp = values[i];
sum = sum + temp;
}
average = sum/NUM;
cout << " The average is " << average << endl;
return 0;
}
why isnt this computing the average?
[highlight](User was banned for this post ("UTT/missed the help thread/no code tags" - SteveUK))[/highlight]
What error are you getting?
Also is there 5 values in the file you are inputting?
You can change the for loop to:
for(int i = 0; i < NUM; i++)
{
sum += values[i];
}
Doesn't change much, just looks nicer.
In fact try putting a cout in the for loop to make sure that everything is read in correctly.
for(int i = 0; i < NUM; i++)
{
cout << "The current value is: " << values[i] << endl;
cout << "The current total is: " << sum << endl;
sum += values[i];
}
it is saying "The average is -1.07374e+008" and the numbers i am using in the .dat file are
5
10
20
-30
0
[QUOTE=ttrexler99;26652437]why isnt this computing the average?[/QUOTE]
Because you aren't even reading from the file?
Also, for the love of Cthulhu, please use code tags.
how do i make it read from the file
[QUOTE=ttrexler99;26652688]how do i make it read from the file[/QUOTE]
[url]http://www.cplusplus.com/reference/iostream/ifstream/[/url]
#include <iostream>
#include <fstream>
using namespace std;
int const NUM = 6;
int main()
{
float values[NUM];
ifstream inData;
float average;
float standardDev;
int sum = 0;
int temp;
inData.open("input.dat");
for(int i = 0; i < NUM; i++)
{
inData >> values[NUM];
cout << "The current value is: " << values[i] << endl;
cout << "The current total is: " << sum << endl;
sum += values[i];
}
average = sum/NUM;
cout << " The average is " << average << endl;
return 0;
}
so i did that and it is still giving me jiberish as answers
Try reading:
[url]http://www.cplusplus.com/doc/tutorial/files/[/url]
Something like:
[code]int sum = 0;
int x;
int count = 0;
float average;
ifstream inFile;
inFile.open("whatever.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1);
}
while (inFile >> x) {
sum = sum + x;
count++;
}
inFile.close();
average = sum/count;
cout << "average = " << average << endl; [/code]
Not 100% on the code but something like that should work. Google it.
that constant NUM = 5 ... not 6
Sorry, but don't you understand? Motherfucking code tags, use them!
Also, what you're doing right now is reading the next file line into values[5] (which you shouldn't do, because you allocated space for 5 elements, not 6). This is not what you want. Drop the values array, just stream into a temporary variable, then add it to the sum.
Edit: Damn, ninja'd.
Times like these when i wish i understood this stuff :(
ok so apparently it cant open my file ill read that link you posted
Oh and for the future if you are reading lines from a file without knowing how long it is and you want to store them, use a vector.
vector<float> something;
that creates a vector, then to add something onto the end just use:
something.push_back(value);
So you can keep adding objects without having the trouble of stating the size of the array originally.
To refer to a value in it use:
something.at(position);
[QUOTE=Random112358;26652867]Oh and for the future if you are reading lines from a file without knowing how long it is and you want to store them, use a vector.
vector<float> something;
that creates a vector, then to add something onto the end just use:
something.push_back(value);
So you can keep adding objects without having the trouble of stating the size of the array originally.
To refer to a value in it use:
something.at(position);[/QUOTE]
He can't figure out simple arrays, don't overload his mind with STL containers.
i just cant figure out how to read it from the file cuz i even used that code he posted and it still isnt reading it
[editline]12th December 2010[/editline]
ive done java for like 3 years this is the first time im messing with c++ its kinda different
[QUOTE=q3k;26652898]He can't figure out simple arrays, don't overload his mind with STL containers.[/QUOTE]
I find vectors easier than arrays and about 100 times as useful. None of this dynamic memory allocation crap, just keep throwing stuff on the end.
[editline]12th December 2010[/editline]
[QUOTE=ttrexler99;26652919]i just cant figure out how to read it from the file cuz i even used that code he posted and it still isnt reading it
[editline]12th December 2010[/editline]
ive done java for like 3 years this is the first time im messing with c++ its kinda different[/QUOTE]
Is your file in the right directory?
[QUOTE=ttrexler99;26652919]i just cant figure out how to read it from the file cuz i even used that code he posted and it still isnt reading it
[editline]12th December 2010[/editline]
ive done java for like 3 years this is the first time im messing with c++ its kinda different[/QUOTE]
If you actually did Java and understood what you were doing, you wouldn't do shit like that.
Try to first make a program that actually prints out the values in the file, then try to make one that calculates their average.
[editline]12th December 2010[/editline]
[QUOTE=Random112358;26652940]I find vectors easier than arrays and about 100 times as useful. None of this dynamic memory allocation crap, just keep throwing stuff on the end.[/QUOTE]
Both are useful in different situations. If you want to have a list of objects that dynamically changes, then std::vector or std::map or whatever is the only way to go. If you need a compact array or one that doesn't change in length, a new int[200] is way better. Not to mention it allows you to do pointer arithmetic, and is irreplaceable for binary-oriented calculations.
i feel like an ass but i had a type where i was saving that txt file. Didnt think of double checking that, Thanks though
Sorry, you need to Log In to post a reply to this thread.