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*
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[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
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
1 2 3 4 5 6 7 8 9
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];
}
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.
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
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
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
1 2 3
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
If it displays the error message, you aren't loading your file correctly.
If it compiled he wouldn't be copy and pasting correctly.
If the original post is correct in the requirements, each number is read from a different file. This may need to be adjusted to reflect the actual file names.
#include <fstream>
#include <iostream>
int main()
{
constunsigned nFiles = 4 ;
constchar* names[nFiles] =
{
"text1.txt",
"text2.txt",
"text3.txt",
"text4.txt"
};
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.
}
else
std::cout << "Unable to open file: " << names[i] << '\n' ;
} // file is closed automatically.
std::cout << "Sum is: " << sum << '\n' ;
}