Issues with sstream and ifstream

Feb 10, 2010 at 3:41pm
I'm trying to mess around with sstream and ifstream in this simple banking program. I'm totally confused as to what exactly is going on with this code, I know I probably just made a silly mistake, but I can't figure out exactly what it is. The program seems to be reading further into the file then it should be. If someone can point out what I did wrong, or an easier way to do this (yes I know I could serialize the class, but trying to do it this way for practice) I would appreciate it. BTW account is a class, not enough room in the post to show the code for it, also not showing code for the other cases.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<fstream>


int main(){
    vector<account> db;
    int tempint;
    string tempstr;
    double tempdouble;
    stringstream stream;
    int current;
    int filesize;
    int choice=1;
    char * tempname;
    char c;
    account* tempacc;
    while(choice!=0){
                     cout<<"\n\n1.List accounts\n2.Change current account\n3.Display current account info\n4.Make Deposit\n5.Withdraw funds\n6.New account\n7.Delete account\n8.Save data\n9.Load data\n0.Exit";
                     cout<<"\nEnter choice: ";
                     cin>>choice;
                     switch(choice){
                                    case 8:
                                         {
                                         ofstream outfile("account.dat");
                                         for(unsigned t=0;t<db.size();++t){
                                                      outfile<<db.at(t).getname()<<","<<db.at(t).getaccno()<<","<<db.at(t).getbalance()<<",";
                                                      }
                                         outfile.close();
                                         cout<<"\n\nData Saved!";
                                         break;
                                         }
                                    case 9:
                                         {
                                         ifstream infile("account.dat");
                                         {
                                                  int counter=0;
                                                  while(!(infile.eof())){
                                                                        infile.get(c);
                                                                        if(c==',')
                                                                                  ++counter;
                                                                        }
                                                  filesize=counter/3;
                                                  
                                         infile.clear();
                                         infile.seekg(0);
                                        
                                         db.clear();
                                         for(int t=0;t<filesize;++t){
                                                 stream.flush();
                                                 counter=0;
                                                 c=' ';
                                                 while(c!=','){cout<<c;
                                                 infile.get(c);                                                 
                                                 if(c==','){
                                                           tempname=new char[81];
                                                           tempstr.clear();
                                                           tempstr.resize(counter);
                                                           stream.getline(tempname,counter+1);
                                                           tempstr=tempname;
                                                           delete tempname;
                                                                   cout<<"\nname="<<tempstr;
                                                           stream.flush(); 
                                                           }
                                                 else{     ++counter;
                                                           stream.put(c);
                                                           }}
                                                 c=' ';
                                                 while(c!=','){cout<<c;
                                                 infile.get(c);
                                                 if(c==','){
                                                           stream>>tempint;
                                                           stream.flush();
                                                           }
                                                 else{
                                                      stream.put(c);
                                                      }}
                                                 c=' ';
                                                 while(c!=','){cout<<c;
                                                 infile.get(c);
                                                 if(c==','){
                                                            stream>>tempdouble;
                                                            stream.flush();
                                                            }
                                                 else{
                                                      stream.put(c);
                                                      }}
                                                 db.resize(db.size()+1);
                                                 db.at(db.size()-1).setup(tempstr,tempint,tempdouble);
                                                 }}
                                         infile.close();
                                         cout<<"\n\n"<<filesize<<" accounts loaded.";
                                         break;
                                         }
                                    case 0:
                                         return 0;
                                         break;
                                    default:
                                         cout<<"\nInvalid Entry!";
                                         break;
                                         }}
                                         return 0;
                                         }
                                                        
Feb 10, 2010 at 3:53pm
ok, changed the code as follows, and now it is portioning out the data correctly, but the double is showing as -1.#QNAN for the value? what the crap does this mean?
Feb 10, 2010 at 4:11pm
You never initialize it, so you have no idea what it will be.
Feb 10, 2010 at 4:36pm
tempdouble is declared at line 8, and then assigned a value from my sstream at line 80.
Topic archived. No new replies allowed.