Creating a file in c++

Pages: 12
Hello guys, i have a problem at storing some infos of different type in a file.I created a struct so that i can deal with the dif types of the variables, but the vector is problematic,although the compiler gives no error.Could you please help me?Here is the part of my code...


while(cin>>Account>>Name>>Balance){
temps.push_back(Reading(Account,Name,Balance));
}

for(int i=0;i<temps.size();++i){
ost<<temps[i].Account<<'\t'<<temps[i].Name<<'\t'<<temps[i].Balance;
}
What is the exact nature of the problem you are having?
the user cannot give the infos...there must be something wrong with the vector..:/
I don't think there's enough information to be able to help.
For example, what does the data entered by the user actually look like?
What does the rest of the code look like?
the data will look like:

Account Name Balance
000 Roberts 200.6
110 Jones 145.5
224 Allen 300.4
Last edited on
to me it looks like you are trying to use the vector as a struct on the second to last line
Edit: probably would be more helpful with a bit more code and with code tags
Last edited on
i'm not sure if it is safe to post it here...
:/
Last edited on
It's probably not safe if you are worried that your teacher will find your code here.

You have not actually told us what the problem is. Is the file not created? Maybe you want to output a new line character after Balance to separate Balance and Account.

ost << temps[i].Account << '\t' << temps[i].Name << '\t' << temps[i].Balance << '\n';
Again.
http://www.cplusplus.com/forum/articles/1295/

Please provide a detailed description of your problem and ensure the code is wrapped in the [code] tags.
the user cannot write the information into the file..the program crushes at the point were i give the data...i don't know if the file is created because the problem is before that...at the while statement...it doesn't work...!!!
is there a way to delete the code?
http://www.cplusplus.com/forum/articles/6046/
You need to use cin.ignore() to handle newlines.
Where would he be needing cin.ignore()? Can't see it.
me neither...:/
cin >> name should have a newline in the buffer then his calls to while(cin>>Account>>Name>>Balance) should fail because of the newline still in the buffer.

Note: I haven't tested it because his code won't compile in that state as is and I don't have time to quickly tweak it to make it compile.
Last edited on
how is this fixed?
Newline is a white space so it will be skipped like spaces and other whitespace characters.
1
2
3
4
5
6
7
struct Reading{
    int Account;
    char Name;
    double Balance;
    Reading(int A,char N,double B):Account(A),Name(N),Balance(B){}

};


There's no way that a name such as "Roberts" can be stored in the single character field char Name;

yeah that's exactly what i mean..what's wrong? i don't get it!
Pages: 12