arrays and while loop

i am currently writing a program that loads portfolio.txt when you type "L", displays portfolio.txt when you type "D", and exits when you type "Q". I was successful in writing a program that did this with the first set of data but it fails to finish loading after i added the arrays and eof while loop.

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
#include <iostream> 
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
   char          answer;
   fstream       data;
   string        fundName[3];
   int           i = 0, numberOfShares[3];
   double        purchasePrice[3], currentValue[3];
   
   cout << fixed << showpoint;
   cout << left;
   
   // Original menu that tells user to type "L" for "load from file," "D" for "display," and "Q" to "quit."
   cout << setw(5) << " " << "Menu" <<  endl;
   cout << setw(5) << "L" << "load from file" << endl;
   cout << setw(5) << "D" << "display" << endl;
   cout << setw(5) << "Q" << "quit" << endl;
   
   // answer holds menu option
   cin >> answer;
   
   //run through the loop as long as answer is not Q for Quit
   while (answer != 'Q')
   {
      cin.clear();  //restore input stream
      cin.ignore(200,'\n');   //clear the buffer
      
      // if you type "L" you load information from "funds.txt"
      if (answer == 'L')
      {
         data.open("portfolio.txt",ios::in);
         
         while (!data.eof() )
         {
            getline(data,fundName[i]);
            data >> numberOfShares[i];
            data >> purchasePrice[i];
            data >> currentValue[i];
            
            i++;
         }
      }
      
      //if you type "D" you display the loaded data or output "no data"
      else if (answer == 'D')
      {
         
         // if "portfolio.txt is open then display "portfolio.txt"
         if (data.is_open())
         {
            while (i<40)
            {
               cout << fundName[i] << endl;
               cout << numberOfShares[i] << endl;
               cout << setprecision(2);
               cout << purchasePrice[i] << endl;
               cout << setprecision(2);
               cout << currentValue[i] << endl << endl;
               i++;
            }   
         }
         
         //if "funds.txt" is not open then display "no data"
         else 
         {
            cout << "no data" << endl;
         }
      }
      
      // display menu after option is input                    
      cout << setw(5) << " " << "Menu" <<  endl;
      cout << setw(5) << "L" << "load from file" << endl;
      cout << setw(5) << "D" << "display" << endl;
      cout << setw(5) << "Q" << "quit" << endl;
      
      cin >> answer;   //holds new menu in answer
   }
   
   data.close();   //closes data
         
   system("pause");
   return 0;   
}    


portfolio.txt:

Michael 1 $ Co
1
1.00
2.00
Michael 2 $ Co
2
1.00
2.00
Michael 3 $ Co
3
1.00
2.00
can someone please help?
Last edited on
http://www.cplusplus.com/forum/articles/1624/

But just look at it, you've got some NASTY potential buffer overflows.

e.g.
currentValue[3];
Can only ever old 3 values. Yet you never limit what i can become.
Topic archived. No new replies allowed.