Hey how's it goin fellas? I am taking a computer programming class at school and I am relatively new to c++. I understand the basics of it but some of it blows my mind. Right now I have two homework problems I need to do, but I am counting on figuring out one so help me do the other.
Basically I need to write a program using a while loop which reads a data file that I create (i got that part).Then the program prints the file contents to the screen (which will be 1 2 3 4 5 6 7 8 9 10). Then it needs to print the sum of all the digits (55).
Well since some people think I just want them to do my homework for me........when it clearly says I just want help........I THINK I figured out how to do the loop but im getting an error on the back arrows on my cout.
my object is file1 and the file I wanted opened is data_file
After reading your topic .. yeah seemed like you were flaking and trying to get homework done easy from a forum.
After previewing your post though, it seems like the book could fail you because I've been in that situation with python books before, and bad teachers.
I wish i could help you understand, but I myself am very new to C++. I figured instead maybe I could write asking ppl to maybe show you in example of a new problem that is close to this one maybe.
This would teach you to learn things still, but not give you a full answer.
Back in my python class the book was very vague after the 5th or so chapter and everyone dropped the class on THE LAST DAY with a W) withdrawl- instead of an F .... The class was online and book only, and this was their first year of python programming classes.
So all in all ... it was a very sad experience imo, and not to mention the 4-5 typos in the first 3 chapters.
So if anyone can help using examples please post here to help the kind sir because it does seem like he's trying.
Your string is empty and it will print just that last space (and print if forever since file1.good() will always evaluate to true, unless you do some operation to change that inside the loop)
What I'd do to print the contents is:
- check how many characters there are in the text file
- create a char array to hold all of them plus one (the '\0')
- write from the file to the array
- print the array
To read the contents of the file as numbers you can use the extraction operator >>, which will take care of interpreting them as numbers if you put them inside numerical variables
Keep in mind I voluntarily omitted some intermediate passages. It's homework after all. But if you have more specific questions I'd be happy to aswer them
Note: I learned C++ from self-study, and the string and fstream libraries still confuse me, so there might be a better way to do this, but sadly I don't know it
To declare arrays you use the square brackets, so
If you know the size of the file in advance then it's ok, but when it's not the case the job gets more complex (I don't think this is the case though)
To add the values you declare a variable to hold the total and add to it the numbers you read until there are no more numbers, and then you print the total
1 2 3 4 5 6 7 8
int total = 0;
int num = 0;
file1 >> num; // this works the same way as cin >> variable
total += num;
cout << total << endl;
To know if there are still numbers to read I suppose you can use file1.good(). I THINK it returns false when you extract the last character in the file