### Sum float within a loop

Hey, I have been learning c++ for a few weeks now and i am writing code that opens text files in sequential order text1,text2,text3 ,stores a string from a selection of text and coverts the selection (which is a number with decimals) to a float.

This works perfectly but now I want to make a running sum of the float each loop.

The loop is a for loop that loops 4 times (4 text files).

Example:

`float myno;` contains 4.56 that it has pulled from the first text file

the next loop

`float myno;` contains 5.19 from the second text file

This loops until it hits the 4th file, each time it loops I want the sum which will either be another float I would imagine or a double to store the sum.

so

`float myno;` contains 4.56
`float sum;` = 4.56

the next loop

`float myno;` contains 5.19 from the second text file
`float sum;` = 9.75

Proabably very simple and I am over/under thinking it! but seem a bit stuck at the moment and cant work out how to sum *face palm*

Thanks!
Make an array.

 ``123456789101112`` ``````float array[4]; std::ifstream file("mytxt.txt"); for(int a = 0; a < 4; a++) { // Code // Input file >> array[a]; // More code }``````
Yeh thats what I thought earlier but I kept getting a compiler error so thought perhaps it was an incorrect way of doing it.

 ``1234567`` ``````float buyIn; //has the decimal number I need fed into it , couts correctly. float test[3];//array to test for(int a = 0; a < 4; a++) { buyIn >> test[a]; }``````

Line 60 is ` buyIn >> test[a];`

1>c:\users\ben\documents\visual studio 2012\projects\programming exercises\hh project\test of hh\test of hh\source.cpp(60): error C2296: '>>' : illegal, left operand has type 'float'
1>c:\users\ben\documents\visual studio 2012\projects\programming exercises\hh project\test of hh\test of hh\source.cpp(60): error C2297: '>>' : illegal, right operand has type 'float'

and the error I get from the red underscore before trying to compile is:

expression must have integral or unscoped enum type

any ideas?
Last edited on
If you have 3 elements, why is it going through the loop 3 times? You're gonna make your program crash.

Either change 4 to 3, or vice verse.

The problem is you're trying to get input from a float variable, which makes no sense.

Open the file you want to read, and change the type to std::ifstream

 ``123456789`` ``````std::ifstream buyIn; //has the decimal number I need fed into it , couts correctly. float test[4];//array to test buyIn.open("mytext.txt"); for(int a = 0; a < 4; a++) { buyIn >> test[a]; }``````

BTW - I changed the elements of the array to 4.
Last edited on by Fredbill30
Hmm still not working , here Ill paste u the source of whats happening.

Basically opens a text file , grabs first 4 lines of text , grabs a string from inside the first line , turns it into a float , does the same with another number in there i am using and I want sum the float buyIn, maybe its just a structure issue of why the loop isnt working , hope u can shed some light on it
Last edited on
 ``1234`` `````` if (! handHistory.is_open () ) //Close Program if unable to open HH { cout << "Unable to open " << filename << endl; }``````

Once it's unable to load, exit the for loop. If not, you can crash your program. A simple way of doing this is

 ``12345`` `````` if (! handHistory.is_open () ) //Close Program if unable to open HH { cout << "Unable to open " << filename << endl; i = 4; }``````

Also, is this all the code?
Its not all the code the cout is there just for testing purposes, but I have couted each loop correctly so for error checking the cout in the if statement is fine for now, but I get no errors in the loop itself just whenever I try to add another loop to store the buyIn float to an array :(

The other functions I have is just a void calculating function, and a function that sticks the strings together I am looking for. All of that is working perfectly.
Last edited on
So you want to add buyIn to your array? I'm confused. Explain to me exactly what you want to do.
float buyIn , each loop (currently 4) will contain a value for example:

5.16 loop1
5.32 loop2
1.25 loop3
7.32 loop4

I want to be able add these together so when the loop is done I can simply say the contents of buyIn throughout the loop is, In this case 19.05.

Which is 5.16+5.32+1.25+7.32

In theory I should just be able to make a simple array called `float sum[4]` , make a for loop that sticks the data in the array each loop, as u pasted, but I keep getting errors from it :S
Last edited on
Well i'd personally use it as a buffer first, then add the variables.
Could you give me some code to test, only been coding for a few weeks and really trying my best to get efficiency and structure down. Not to mention been messing around with this for way too long now and slowly going mad lol
A buffer? http://en.wikipedia.org/wiki/Data_buffer

Just use the for loop to get data, then set the part of the array to equal it:
 ``123456789101112131415161718192021`` ``````std::ifstream file; float buyIn; double array[4]; file.open("mytext.txt"); for(int a = 0; a < 4; a++) { file >> buyIn; array[a] = buyIn; } buyIn = 0; for(int a = 0; a < 4; a++) { buyIn += array[a]; } std::cout << "Price: " << buyIn;``````
Hmm I am already inside the for Loop so using i will mean use the loop to grab the bi everytime so something like this should work in theory

 ``123`` ``````BI[i] = buyIn; buyIn = 0; buyIn += BI[i];``````

tried initialising BI[4] and buyIn outside of the for loop , your method just loops 4 times over the buyIn value rather than grabbing the next buyIn from the loop i. So say for example

`float buyIn` contains 200

your code will loop 200 x 4 and return 800 , so when the next text file is open , it will just grab the value of buyIn again and loop x4 on that which is not the same as summing.

I cant beleive how difficult this one peice has become lol in theory it should be ridic easy and I have tried so many variations and just cant get it to work sighhhh
Last edited on
 ``12345678910111213141516171819202122232425`` ``````std::ifstream file; float buyIn = 0; float buf; double array[4]; file.open("mytext.txt"); if(file.open == NULL) { std::cout << "Failure to open file.\n"; cin.get(); } for(int a = 0; a < 4; a++) { file >> buf; array[a] = buf; } for(int a = 0; a < 4; a++) { buyIn += array[a]; } std::cout << "Price: " << buyIn;``````

Last edited on by Fredbill30
 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include int main() { const unsigned nFiles = 4 ; const char* names[nFiles] = { "text1.txt", "text2.txt", "text3.txt", "text4.txt" }; float sum = 0 ; for ( unsigned i=0; i> value ; // Read in the value. sum += value ; // add it to the sum. } else std::cout << "Unable to open file: " << names[i] << '\n' ; } // file is closed automatically. std::cout << "Sum is: " << sum << '\n' ; }``````