Noob at c++

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).

I didnt get very far

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
string line;
ifstream open("data_file.txt");




after that im pretty much lost. the book is useless haha

any help would be greatly appreciated
Hm.... The book is not useless. Read all pages of it. And then read again. After, you will be able to write the code.
Personally I hate to write home works of others if they don't try.
I've read the chapter we are supposed to be on...and all the previous ones. there is nothing like this in the book, just vague answers to vague questions
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

#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main ()
string line;
ifstream file1 ("data_file.txt");
if (file1.is_open())
while (file1.good() )
cout << line << " ";


any idea what it could be?

Anyone have any tips on doing the second part of this program in my OP?

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.
Last edited on
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
Last edited on
yoda - thanks bra

maeriden - I believe there are 20 characters, so your thinkin somethin like...

char word(20);

I understand what your tryin to say about the extraction operators....but how would I take those and add the up together to get 55??

would I do something like cin >> n+n+ >> n, this part really confuses me.
To declare arrays you use the square brackets, so
char word[20];
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

Something like
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
Thanks dude, I'm pretty sure I understand it now.
Topic archived. No new replies allowed.