String Help

When i input the name of the item on the first lable it works but on the second the getline get skipped can somebody 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
113
114
115
116
117
118
119
120
121
122
123
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <iomanip>
#include <string>
#include <math.h>
using namespace std;
string date, item_1, item_2, item_3, item_4,  item_5, item_6, item_7, item_8, item_9, item_10;
float Total, TotalTax, Tax,cost_1,cost_2,cost_3,cost_4,cost_5;
char again;
int main(int argc, char *argv[])
{
    cout << setiosflags(ios::fixed) << setprecision(2);
    cout<<"Enter in a date:";
    getline (cin, date);
    cout<<"The item name:";
    getline (cin, item_1);
    cout<<"Finally, enter in the cost of the item:";
    cin>>cost_1;
    Sleep(2000);
    system ("cls");
       cout<<"Do You Have Another item [y] or [n]?";
    cin>>again;
               switch (again)
            {
                      case 'y':
                      goto item2;
                      
               default:
               goto printitems;
               }
    
               
               item2: 
               system("cls");                    
    cout<<"The item name:";
    getline (cin, item_2);
    cout<<"\n\n";
    cout<<"Finally, enter in the cost of the item:";
    cin>>cost_2;
    Sleep(2000);
    system ("cls");
       cout<<"Do You Have Another item [y] or [n]?";
    cin>>again;
               switch (again)
            {
                      case 'y':
                      goto item3;
                      
               default:
               goto printitems;
               }
    
                   item3:                     
    cout<<"The item name:";
    getline (cin, item_3);
    cout<<"\n\n";
    cout<<"Finally, enter in the cost of the item:";
    cin>>cost_3;
    Sleep(2000);
    system ("cls");   
   cout<<"Do You Have Another item [y] or [n]?";
    cin>>again;
               switch (again)
            {
                      case 'y':
                      goto item4;
                      
               default:
               goto printitems;
               }
                   item4:                     
    cout<<"The item name:";
    getline (cin, item_5);
    cout<<"\n\n";
    cout<<"Finally, enter in the cost of the item:";
    cin>>cost_4;
    Sleep(2000);
    system ("cls");
    cout<<"Do You Have Another item [y] or [n]?";
    cin>>again;
               switch (again)
            {
                      case 'y':
                      goto item5;
                      
               default:
               goto printitems;
               }
                   item5:                     
    cout<<"The item name:";
    getline (cin, item_5);
    cout<<"\n\n";
    cout<<"Finally, enter in the cost of the item:";
    cin>>cost_5;
    Sleep(2000);
    system ("cls");

               goto printitems;

    
    printitems:
    system("cls");
                                                  
    cout<<setw(25)<<"DATE"<<setw(15)<<"ITEMS"<<setw(15)<<"COST\n\n";
    cout<<setw(25)<<date<<setw(15)<<item_1<<setw(15)<<cost_1<<endl;
    cout<<setw(40)<<item_2<<setw(15)<<cost_2<<endl;
    cout<<setw(40)<<item_3<<setw(15)<<cost_3<<endl;
    cout<<setw(40)<<item_4<<setw(15)<<cost_4<<endl;
    cout<<setw(40)<<item_5<<setw(15)<<cost_5<<endl;    
    Total=(cost_1+cost_2+cost_3+cost_4+cost_5);
    Tax=(Total*.06);
    TotalTax=(Total*1.06);
    cout<<setw(40)<<"SubTotal:"<<Total<<endl;
    cout<<setw(40)<<"Total Tax:"<<Tax<<endl;    
    cout<<setw(40)<<"Total With Tax:"<<TotalTax<<endl;
    system("pause");
    
                   
    
    return EXIT_SUCCESS;
}
Last edited on
May B U need to fflush the standard input prior to getting input from user through cin.
You need to use cin.clear() and cin.ignore() to clear errors and discard unwanted items in the stream.

They are documented as part of istream (http://www.cplusplus.com/reference/iostream/istream/), and also in several older answers on this forum.

This question pos up reasonbly frequently - Grey Wolf do you fancy adding another of your excellent articles on this?
Topic archived. No new replies allowed.