Loop problem

Hi everyone, any idea why the first time you run through the loop where you enter the information for the product that it skips the first cin? very strange!

any help would be great.

royal1664

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void desc();
void error();
void select();
void desktop();
void laptop();
void tablet();
int deskOutput(int dSum);
int lapOutput(int lSum);
int tabOutput(int tSum);

#define d 100

struct desktop
{
string make;
string model;
float price;
int qnty;
}des[d];
struct laptop
{
string make;
string model;
float price;
int qnty;
}lap[d];
struct tablet
{
string make;
string model;
float price;
int qnty;
}tab[d];
int main()
{
desc();
select();



return 0;
}
void select()
{
int nSelect;
cout << "Enter a number to select which product to enter information for:" << endl
<< "1. Desktop" << endl
<< "2. Laptop" << endl
<< "3. Tablet" << endl;
cin >> nSelect;

if (nSelect == 1)
{
desktop();
}
if (nSelect == 2)
{
laptop();
}
if (nSelect == 3)
{
tablet();
}
if (nSelect < 1)
{
error();
}
if (nSelect > 3)
{
error();
}
}
void desc()
{
cout << "This program allows you to enter information about a product.\n" << endl;
}
void error()
{
cout << "You've entered an incorrect number.\n";
select();
}
void desktop()
{
int amount;
cout << "\nHow many desktops would you like to enter?\n";
cin >> amount;

for (int a = 0; a < amount; a++)
{
string sPrice;
string sQnty;
cout << "Enter make: ";
getline(cin,des[a].make);
cout << endl;
cout << "Enter model: ";
getline(cin,des[a].model);
cout << endl;
cout << "Enter price: ";
getline(cin,sPrice);
stringstream(sPrice) >> des[a].price;
cout << endl;
cout << "Enter quantity: " << endl;
getline(cin,sQnty);
stringstream(sQnty) >> des[a].qnty;
}

int data = 0;
cout << "Would you like to view your data?\nEnter 1 for yes and 2 for no.\n";
cin >> data;

if(data == 1)
{
deskOutput(amount);
}



}

int deskOutput(int dSum)
{
cout << "Desktop information: \n";

for(int a = 0;a <= dSum; a++)
{
cout << "Make........." << des[a].make
<< "\nModel........" << des[a].model
<< "\nPrice........" << des[a].price
<< "\nQuantity....." << des[a].qnty
<< endl << endl;
}
}
void laptop()
{
int amount;
cout << "\nHow many laptops would you like to enter?\n";
cin >> amount;

for (int a = 0; a < amount; a++)
{
string sPrice;
string sQnty;
cout << "Enter make: ";
getline(cin,lap[a].make);
cout << endl;
cout << "Enter model: ";
getline(cin,lap[a].model);
cout << endl;
cout << "Enter price: ";
getline(cin,sPrice);
stringstream(sPrice) >> lap[a].price;
cout << endl;
cout << "Enter quantity: " << endl;
getline(cin,sQnty);
stringstream(sQnty) >> lap[a].qnty;
}

int data = 0;
cout << "Would you like to view your data?\nEnter 1 for yes and 2 for no.\n";
cin >> data;

if(data == 1)
{
lapOutput(amount);
}

}
int lapOutput(int lSum)
{
cout << "Laptop information: \n";

for(int a = 0;a <= lSum; a++)
{
cout << "Make........." << lap[a].make
<< "\nModel........" << lap[a].model
<< "\nPrice........" << lap[a].price
<< "\nQuantity....." << lap[a].qnty
<< endl << endl;
}
}
void tablet()
{
int amount;
cout << "\nHow many tablets would you like to enter?\n";
cin >> amount;

for (int a = 0; a < amount; a++)
{
string sPrice;
string sQnty;
cout << "Enter make: ";
getline(cin,tab[a].make);
cout << endl;
cout << "Enter model: ";
getline(cin,tab[a].model);
cout << endl;
cout << "Enter price: ";
getline(cin,sPrice);
stringstream(sPrice) >> tab[a].price;
cout << endl;
cout << "Enter quantity: " << endl;
getline(cin,sQnty);
stringstream(sQnty) >> tab[a].qnty;
}

int data = 0;
cout << "Would you like to view your data?\nEnter 1 for yes and 2 for no.\n";
cin >> data;

if(data == 1)
{
tabOutput(amount);
}
}
int tabOutput(int tSum)
{
cout << "Tablet information: \n";

for(int a = 0;a <= tSum; a++)
{
cout << "Make........." << tab[a].make
<< "\nModel........" << tab[a].model
<< "\nPrice........" << tab[a].price
<< "\nQuantity....." << tab[a].qnty
<< endl << endl;
}
}
You need to properly format your code before posting.
http://www.cplusplus.com/articles/z13hAqkS/

This code won't compile for me because some of your functions are supposed to return values but they don't.

if you are going to use statements like this cin >> a; to input data you should follow each statement with cin.ignore(); to remove the newline left in the input stream from the user pressing enter.
perfect, it now works great! many thanks.

How does simply putting cin.ignore(); after a cin >> actually work?

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void desc();
void error();
void select();
void desktop();
void laptop();
void tablet();
int deskOutput(int dSum);
int lapOutput(int lSum);
int tabOutput(int tSum);

#define d 100

struct desktop
{
    string make;
    string model;
    float price;
    int qnty;
}des[d];
struct laptop
{
    string make;
    string model;
    float price;
    int qnty;
}lap[d];
struct tablet
{
    string make;
    string model;
    float price;
    int qnty;
}tab[d];
int main()
{
    desc();
    select();



    return 0;
}
void select()
{
    int nSelect;
    cout << "Enter a number to select which product to enter information for:" << endl
        << "1. Desktop" << endl
        << "2. Laptop" << endl
        << "3. Tablet" << endl;
    cin >> nSelect;

    if (nSelect == 1)
    {
        desktop();
    }
    if (nSelect == 2)
    {
        laptop();
    }
    if (nSelect == 3)
    {
        tablet();
    }
    if (nSelect < 1)
    {
     error();
    }
    if (nSelect > 3)
    {
        error();
    }
}
void desc()
{
    cout << "This program allows you to enter information about a product.\n" << endl;
}
void error()
{
    cout << "You've entered an incorrect number.\n";
    select();
}
void desktop()
{
    int amount;
    cout << "\nHow many desktops would you like to enter?\n";
    cin >> amount;
    cin.ignore();

    for (int a = 0; a < amount; a++)
    {
        string sPrice;
        string sQnty;
        cout << "Enter make: ";
        getline(cin,des[a].make);
        cout << endl;
        cout << "Enter model: ";
        getline(cin,des[a].model);
        cout << endl;
        cout << "Enter price: ";
        getline(cin,sPrice);
        stringstream(sPrice) >> des[a].price;
        cout << endl;
        cout << "Enter quantity: " << endl;
        getline(cin,sQnty);
        stringstream(sQnty) >> des[a].qnty;
    }

            int data = 0;
            cout << "Would you like to view your data?\nEnter 1 for yes and 2 for no.\n";
            cin >> data;

            if(data == 1)
            {
             deskOutput(amount);
            }



}

int deskOutput(int dSum)
{
    cout << "Desktop information: \n";

    for(int a = 0;a <= dSum; a++)
    {
        cout << "Make........." << des[a].make
            << "\nModel........" << des[a].model
            << "\nPrice........" << des[a].price
            << "\nQuantity....." << des[a].qnty
            << endl << endl;
    }
}
void laptop()
{
    int amount;
    cout << "\nHow many laptops would you like to enter?\n";
    cin >> amount;
    cin.ignore();

    for (int a = 0; a < amount; a++)
    {
        string sPrice;
        string sQnty;
        cout << "Enter make: ";
        getline(cin,lap[a].make);
        cout << endl;
        cout << "Enter model: ";
        getline(cin,lap[a].model);
        cout << endl;
        cout << "Enter price: ";
        getline(cin,sPrice);
        stringstream(sPrice) >> lap[a].price;
        cout << endl;
        cout << "Enter quantity: " << endl;
        getline(cin,sQnty);
        stringstream(sQnty) >> lap[a].qnty;
    }

            int data = 0;
            cout << "Would you like to view your data?\nEnter 1 for yes and 2 for no.\n";
            cin >> data;

            if(data == 1)
            {
             lapOutput(amount);
            }

}
int lapOutput(int lSum)
{
    cout << "Laptop information: \n";

    for(int a = 0;a <= lSum; a++)
    {
        cout << "Make........." << lap[a].make
            << "\nModel........" << lap[a].model
            << "\nPrice........" << lap[a].price
            << "\nQuantity....." << lap[a].qnty
            << endl << endl;
    }
}
void tablet()
{
     int amount;
    cout << "\nHow many tablets would you like to enter?\n";
    cin >> amount;
    cin.ignore();

    for (int a = 0; a < amount; a++)
    {
        string sPrice;
        string sQnty;
        cout << "Enter make: ";
        getline(cin,tab[a].make);
        cout << endl;
        cout << "Enter model: ";
        getline(cin,tab[a].model);
        cout << endl;
        cout << "Enter price: ";
        getline(cin,sPrice);
        stringstream(sPrice) >> tab[a].price;
        cout << endl;
        cout << "Enter quantity: " << endl;
        getline(cin,sQnty);
        stringstream(sQnty) >> tab[a].qnty;
    }

            int data = 0;
            cout << "Would you like to view your data?\nEnter 1 for yes and 2 for no.\n";
            cin >> data;

            if(data == 1)
            {
             tabOutput(amount);
            }
}
int tabOutput(int tSum)
{
    cout << "Tablet information: \n";

    for(int a = 0;a <= tSum; a++)
    {
        cout << "Make........." << tab[a].make
            << "\nModel........" << tab[a].model
            << "\nPrice........" << tab[a].price
            << "\nQuantity....." << tab[a].qnty
            << endl << endl;
    }
}
royal1664 wrote:
How does simply putting cin.ignore(); after a cin >> actually work?
If you enter "Hello, Hi", your line actully looks like: "Hello, Hi\n"
operator>> will leave whitespaces, so after first std::cin >> xx; your buffer will look like: " Hi\n" (note whitespace in the beginning). After second: "\n" (newline symbol). getline will tale all symbols unti it encounter a delimeter (newline by default). As it encounters it immidetly, it returns an empty string.
std::cin.ignore() will skip one symbol (usually a newline in your input)
Last edited on
brilliant, thanks very much!
Topic archived. No new replies allowed.