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).
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.
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*
Yeh thats what I thought earlier but I kept getting a compiler error so thought perhaps it was an incorrect way of doing it.
1 2 3 4 5 6 7
float buyIn; //has the decimal number I need fed into it , couts correctly.
float test;//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
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
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.
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
constunsigned nFiles = 4 ;
constchar* names[nFiles] =
float sum = 0 ;
for ( unsigned i=0; i<nFiles; ++i )
std::ifstream in(names[i]) ; // Open the file.
if ( in.is_open() )
float value ;
in >> value ; // Read in the value.
sum += value ; // add it to the sum.
std::cout << "Unable to open file: " << names[i] << '\n' ;
} // file is closed automatically.
std::cout << "Sum is: " << sum << '\n' ;