My last resort! A grocery list program...

Hello! I've been working on this program for quite some time now... and I still can't get it right! I've tried many different things, but there's always an issue. My issue; displaying the infile input in a way that clears the "____" after each word and "presses" enter, instead of it all being on one line. At the moment, my problem is when I press "3," it just freezes and does nothing until I close the program. I thought maybe it was because I was using for loops (I've used them before in another program, and had to remove them by using while loops, making the program work. No idea what was wrong but oh well...) so I have while loops in the "listView()" function. I started to learn C++ about a year and half ago, so be gentle! Here's my code...

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
void listCreate();
void listAdd();
void listView();
void listPrint();
int main()
{
    cout << "Eric's Grocery List!\n";
    int a = 1;
    do
    {   
    cout<<"\n";
    cout<<"1 - Create a list\n";
    cout<<"2 - Add to an existing list\n";
    cout<<"3 - View an existing list\n";
    cout<<"4 - Print an existing list\n";
    cout<<"5 - Exit\n";
    
    cin>>a;
    
    switch (a)
    {
           case 1:
                listCreate();
                break;
           case 2:
                listAdd();
                break;
           case 3:
                listView();
                break;
           case 4:
                listPrint();
                break;
           case 5:
                cout<<"Closing...\n";
                return 0;
           default:
                   cout<<"ERROR! Enter a number 1-5...\n";
                   break;
    }
    }
    while (a != 5);
    return 0;
}

void listCreate()
{
     ofstream outfile;
     outfile.open("list.txt", ios::out);
     if (outfile)
     {
                 cout << "Enter your item(s)...\n";
                 cout << "Type \"end\" when finished...\n";
                 int i = 0;
                 string input = " ";
                 while (i != 500 && input != "end")
                 {
                       cin >> input;
                       if (input != "end")
                       {
                                 outfile << input;
                                 outfile << "____";
                                 i++;
                       }
                 }
                 outfile.close();
     }
     else
     {
         cout << "There was an error opening the file...\n";
     }
}

void listAdd()
{
     ofstream outfile;
     outfile.open("list.txt", ios::app);
     if (outfile)
     {
                 cout << "Enter your item(s)...\n";
                 cout << "Type \"end\" when finished...\n";
                 int i = 0;
                 string input = " ";
                 while (i != 500 && input != "end")
                 {
                       cin >> input;
                       if (input != "end")
                       {
                                 outfile << input;
                                 outfile << "____";
                                 i++;
                       }
                 }
                 outfile.close();
     }
     else
     {
         cout << "There was an error opening the file...\n";
     }
}
                                               
void listView()
{
     ifstream infile;
     infile.open("list.txt", ios::in);
     if (infile)
     {
                string input;
                infile >> input;
                vector<string> list;
                while (input.find("____"))
                {
                      string word;
                      char letter;
                      int f = 0;
                      string::iterator iList;
                      int pos1 = input.find("____");
                      int pos2 = pos1 - 1;
                      iList = input.begin();
                      while (iList < input.end())
                      {
                          while (f < pos2)
                          {
                                letter = *iList;
                                if (letter == '_')
                                {
                                           break;
                                }
                                else
                                {
                                    word = word + letter;
                                    f = f++;
                                }
                          }
                          iList = iList++;
                      }
                      list.push_back(word);
                      input.erase(0, (pos2 + 4));              
                }
                int size = list.size();
                int i = 0;
                while (i < size)
                {
                    cout << list[i] << endl;
                    i = i++;
                }
                infile.close();
     }
     else
     {
         cout << "There was an error opening the file...\n";
     }
}

void listPrint()
{
}
     
                                 
    
Last edited on
I don't want someone to baby me and go step by step... but a push in the right direction would be a dream come true!
while (input.find("____"))

The only time find will return something that evaluates to false is when the substring occurs at the very beginning of input.

std::string::find() returns string::npos when the substring is not found.
Okay, thank you very much. I'll look for something else to use then.
Topic archived. No new replies allowed.