saving and loading vectors from a file

I am writing a program in which I will need to save the vectors I create and modify so a file, and then I will need to be able to close the program, open the program again and load those same vectors from the same file and be able to modify them again. So what I have so far is giving me errors, and I was wondering if anyone could help me out:

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
103
104
105
106
107
108
109
110
111
112
int main(int argc, char *argv[])
{
    int selection;
    int n;
    string file; 
    string filename;
    int numberInput; 
    string nameInput; 
    vector<string> Names; // set to 15???
    vector<int> Numbers;
    int i; 
    
    
    do 
    {
    menuSelection(selection , Names );
    performSelection(selection, file, filename, numberInput, nameInput, Names, Numbers);
    } while (selection !=0);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
 void menuSelection(int &selection, vector<string> &Names)
 {
      cout << "1. Load Inventory" << endl;
      cout << "2. Add Item" << endl; 
      cout << "3. Search for Item" << endl;
      cout << "4. List Items" << endl;
      cout << "5. Save Inventory" << endl;
      cout << "6. End Program" << endl;
      cout << "Number of Items in Inventory: "<< Names.size() << endl;
      cout << endl;
      cout << "Enter the number of the action you would like to do: " << endl;
      cin >> selection;   
 }

void performSelection(int &selection, string &file ,string &filename, int &numberInput, string &nameInput, vector<string> &Names, vector<int> &Numbers)
{ 
     if (selection==1)
     {
        loadfile(filename, Numbers, Names);    
     }
     
     if (selection==2)
     {
        additem(numberInput, nameInput, Names, Numbers);
     }
     
     if (selection == 3)
     {
        searchitems(Numbers, Names);             
     }
        
     if(selection == 4)
     {
        listitems(Names, Numbers);          
     }
     if (selection == 5)
     {
        saveinventory(file, Numbers, Names);              
     }
     if (selection == 6)
     {
        system ("PAUSE");
        exit (1);          
     }
}

void loadfile(string &filename, vector<int> &Numbers, vector<string> &Names)
{
        int i; 
        ifstream inputFile; 
        string line;
        cout << "Enter the filename where the inventory is stored: " << endl;
        cin >> filename;
        inputFile.open(filename.c_str()); 
        for (i = 0 ; i < Numbers.size() ; i++)
        { 
          getline(inputFile, Numbers[i]);
          getline(inputFile, Names[i]);
        } 
        if (inputFile.fail())
        {
           cerr << "File opening failed.\n";
           system ("PAUSE");
           exit (1);    
                         
        }
        else 
        {
           cout << "The file opened" << endl;
        }      
}

void saveinventory(string &file, vector<int> &Numbers, vector<string> &Names)
{
    int i; 
    cout << "Enter what you would like the File Name to be: ";
    cin >> file; 
    ofstream outputFile;            
    outputFile.open("filename");  
   
   for (i = 0 ; i < Numbers.size() ; i++)
   { 
       outputFile << Numbers[i] << endl;  
       outputFile << Names[i] << endl;
       
   }
   
    outputFile.close();     
}
Point out the line number or at least the function that is giving you an error. If it is a compiling error, then specify it.

As a tip for postings: DO NOT JUST SAY "IT GIVES ME ERRORS". Post the error details, point the line of code where the error appears, etc.
well it is giving me errors on lines 80 and 81. But I was also wondering if the loadinventory() and saveinventory() would work together in saving and then loading the vectors from the same file.
Line 80: getline(inputFile, Numbers[ i ] ); Numbers is a vector<int>, getline can be used with strings only. For other types use the >> operator
Line 78: for (i = 0 ; i < Numbers.size() ; i++) are you sure that at the moment which you will call the function, Numbers would already have some elements ( so a size ) ?
Topic archived. No new replies allowed.