cin.getline(variable, 100) was skipped

Hi. I hope someone can help me. I've done make some research on google and I still not found how to resolve my problem.

The thing that I was did :

- use cin.ignore()
* The result is I got space annoying spaces that force me to click enter to continue the program.

- use dummy variable
* I got error for this

- use getchar()
* I got same problem with cin.ignore()

This is 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
 //AI TRAVEL & TOURS
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    char name[100], packageCode, mealResult, addCustResult;
    int childrenNum, childrenPrice, adultNum, adultPrice, i = 1, mealCharge = 0, aCount = 0, bCount = 0, cCount =0;
    bool meal = false, addCust = true;
    double totalBeforeSST, totalWithSST, totalSST, sst = 0.06, totalSales = 0;
    string packageName;

    while(i>0 && addCust) {

    // START GET_INPUT_FROM_USER

        cout << "Enter customer's name: " ;
        cin.getline(name, 100);

        cout << "Enter package code: " ;
        cin >> packageCode;

        cout << "Enter number of adults: ";
        cin >> adultNum;

        cout << "Enter number of children: ";
        cin >> childrenNum;

        cout << "Do you want to add meals your package? (Y/N): ";
        cin >> mealResult;

    // END GET_INPUT_FROM_USER
    // MEAL SECTION

        if(mealResult == 'Y' || mealResult == 'y'){

            !meal;

        } else {

            meal;

        }

        if(!meal){

            mealCharge = (adultNum*50) + (childrenNum*30);

        }

    // END MEAL SECTION

    // START PACKAGE SELECTION

        if(packageCode == 'A' || packageCode == 'a'){

            packageName = "Langkawi Free & Easy";
            adultPrice = 200.00;
            childrenPrice = 150.00;
            aCount++;

        } else if(packageCode == 'B' || packageCode == 'b'){

            packageName = "Langkawi Island & Nature";
            adultPrice = 250.00;
            childrenPrice = 200.00;
            bCount++;

        } else if(packageCode == 'C' || packageCode == 'c'){

            packageName = "Langkawi Culture & Historical";
            adultPrice = 300.00;
            childrenPrice = 250.00;
            cCount++;

        } else {

            cout << "You have entered invalid Package Code. Please Try Again" << endl;

        }

        // END PACKAGE CODE SELECTION

        // TOTAL PROCESS

            totalBeforeSST = (adultNum*adultPrice) + (childrenNum*childrenPrice) + mealCharge;
            totalSST = totalBeforeSST * sst;
            totalWithSST = totalBeforeSST + totalSST;
            totalSales = totalSales + totalWithSST;

        // RECEIPT

            cout.setf(ios::fixed);
            cout.precision(2);
            cout << endl;
            cout << "RECEIPT" << endl;
            cout << "------------------------------------------" << endl;
            cout << "Customer's name    : " << name << endl;
            cout << "Package code       : " << packageCode << endl;
            cout << "Package name       : " << packageName << endl;
            cout << "Add meals          : " << mealResult << endl;
            cout << "Total before SST   : RM" << totalBeforeSST <<  endl;
            cout << "Total SST          : RM" << totalSST << endl;
            cout << "Total with SST     : RM" << totalWithSST << endl;
            cout << "------------------------------------------" << endl;

        // END RECEIPT

        // ASK USER FOR ADD ANOTHER CUSTOMER

            cout << "\nAdd another customer? (Y/N) : ";
            cin >> addCustResult;
            cout << endl;

            if(addCustResult == 'Y' || addCustResult == 'y' ) {

                addCust;

            } else if(addCustResult == 'N' || addCustResult == 'n'){

                addCust = !addCust;

            }

            if(!addCust){

                break;

            }

            i++;
    }

        cout << "AI TRAVEL & TOURS" << endl;
        cout << "*********************************************" << endl;
        cout << "Package A              : " << aCount << endl;
        cout << "Package B              : " << bCount << endl;
        cout << "Package C              : " << cCount << endl;
        cout << "Total sales            : RM" << totalSales << endl;
        cout << "*********************************************" << endl;

        return 0;

}
Last edited on
I still not found how to resolve my problem.
What problem are you trying to solve?
The thing that I was did :
Thanks for describing what you've done to solve it, but we still don't know what the problem is?

Please describe the input you're giving, the output you're getting, and the output that you expect.
I don't see where but his input buffer is borked, most likely, causing getline to do something screwy. I havent found it yet.
Since you mix up getline and std::cin, you need to clean the buffer from the last '\n' after the last std::cin (just use std::cin.ignore()).
You also forgot to include <string>

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


int main()
{
    int a_count {}, b_count {}, c_count {};
    double total_sales {};
    bool add_cust { true };
    while( add_cust ) {
        // START GET_INPUT_FROM_USER

        std::cout << "Enter customer's name: " ;
        char name[100];
        std::cin.getline(name, 100, '\n');

        std::cout << "Enter package code: " ;
        char package_code;
        std::cin >> package_code;

        std::cout << "Enter number of adults: ";
        int adult_num;
        std::cin >> adult_num;

        std::cout << "Enter number of children: ";
        int children_num;
        std::cin >> children_num;

        std::cout << "Do you want to add meals to your package? (Y/N): ";
        char meal_result;
        std::cin >> meal_result;

        // END GET_INPUT_FROM_USER
        // MEAL SECTION

        bool meal { false };
        if(meal_result == 'Y' || meal_result == 'y') {
            meal = true;
        }

        int meal_charge {};
        if(meal) {
            meal_charge = (adult_num * 50) + (children_num * 30);
        }

        // END MEAL SECTION

        // START PACKAGE SELECTION

        int adult_price;
        int children_price;
        std::string package_name;
        if(package_code == 'A' || package_code == 'a') {
            package_name = "Langkawi Free & Easy";
            adult_price = 200.00;
            children_price = 150.00;
            ++a_count;

        } else if(package_code == 'B' || package_code == 'b'){
            package_name = "Langkawi Island & Nature";
            adult_price = 250.00;
            children_price = 200.00;
            ++b_count;

        } else if(package_code == 'C' || package_code == 'c'){
            package_name = "Langkawi Culture & Historical";
            adult_price = 300.00;
            children_price = 250.00;
            ++c_count;

        } else {
            std::cout << "You have entered invalid Package Code. Please Try Again.\n";
        }

        // END PACKAGE CODE SELECTION

        // TOTAL PROCESS

        double sst = 0.06;
        double total_before_sst {
              (adult_price * adult_num)
            + (children_price * children_num)
            + static_cast<double>(meal_charge)
        };
        double total_sst { total_before_sst * sst };
        double total_with_sst { total_before_sst + total_sst };
        total_sales = total_sales + total_with_sst;

        // RECEIPT

        std::cout.setf(std::ios::fixed);
        std::cout.precision(2);
        std::cout << "\nRECEIPT\n"
                     "------------------------------------------"
                     "\nCustomer's name    : "   << name
                  << "\nPackage code       : "   << package_code
                  << "\nPackage name       : "   << package_name
                  << "\nAdd meals          : "   << meal_result
                  << "\nTotal before SST   : RM" << total_before_sst
                  << "\nTotal SST          : RM" << total_sst
                  << "\nTotal with SST     : RM" << total_with_sst
                  << "\n------------------------------------------\n";

        // END RECEIPT

        // ASK USER FOR ADD ANOTHER CUSTOMER

        std::cout << "\nAdd another customer? (Y/N) : ";
        char add_cust_result;
        std::cin >> add_cust_result;
        std::cin.ignore();

        if(add_cust_result == 'N' || add_cust_result == 'n') {
            add_cust = false;
            break;
        }
    }

    std::cout << "\nAI TRAVEL & TOURS"
                 "\n*********************************************"
                 "\nPackage A              : " << a_count
              << "\nPackage B              : " << b_count
              << "\nPackage C              : " << c_count
              << "\nTotal sales            : RM" << total_sales
              << "\n*********************************************\n";
    return 0;
}


Output:
Enter customer's name: John Doe
Enter package code: B
Enter number of adults: 2
Enter number of children: 3
Do you want to add meals to your package? (Y/N): y

RECEIPT
------------------------------------------
Customer's name    : John Doe
Package code       : B
Package name       : Langkawi Island & Nature
Add meals          : y
Total before SST   : RM1290.00
Total SST          : RM77.40
Total with SST     : RM1367.40
------------------------------------------

Add another customer? (Y/N) : y
Enter customer's name: Jan Smith
Enter package code: A
Enter number of adults: 1
Enter number of children: 2
Do you want to add meals to your package? (Y/N): y

RECEIPT
------------------------------------------
Customer's name    : Jan Smith
Package code       : A
Package name       : Langkawi Free & Easy
Add meals          : y
Total before SST   : RM610.00
Total SST          : RM36.60
Total with SST     : RM646.60
------------------------------------------

Add another customer? (Y/N) : y
Enter customer's name: Bill Buffalo
Enter package code: C
Enter number of adults: 3
Enter number of children: 1
Do you want to add meals to your package? (Y/N): n

RECEIPT
------------------------------------------
Customer's name    : Bill Buffalo
Package code       : C
Package name       : Langkawi Culture & Historical
Add meals          : n
Total before SST   : RM1150.00
Total SST          : RM69.00
Total with SST     : RM1219.00
------------------------------------------

Add another customer? (Y/N) : n

AI TRAVEL & TOURS
*********************************************
Package A              : 1
Package B              : 1
Package C              : 1
Total sales            : RM3233.00
*********************************************

Thank you so much @Enoizat. I got it. 😍
You very welcome. You can still simplify your code.
For example:
1
2
3
4
5
6
7
8
9
bool meal { false };
if(meal_result == 'Y' || meal_result == 'y') {
    meal = true;
}

int meal_charge {};
if(meal) {
    meal_charge = (adult_num * 50) + (children_num * 30);
}


Could simply be:
1
2
3
4
int meal_charge {};
if(meal_result == 'Y' || meal_result == 'y') {
    meal_charge = (adult_num * 50) + (children_num * 30);
}


And
total_sales = total_sales + total_with_sst;
Is the same as
total_sales += total_with_sst;

However the most important thing should be moving everything into functions like askUserData(), askMealData(), reckonPackagePrice(), reckonTotal(), displayResult()
Is bool meal { false } same with bool meal = false?
How to use {} and what made you to not using name namespace std. Because I think it will become complicated if not use it. I'm just asking to be expert in c++. 😁
Last edited on
Is bool meal { false } same with bool meal = false?
How to use {}

It is called "uniform initialization" and was added to the language with C++11, along with "initializer list."

https://mbevin.wordpress.com/2012/11/16/uniform-initialization/

what made you to not using name namespace std. Because I think it will become complicated if not use it. I'm just asking to be expert in c++.


https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice#1452738

You want to be a C++ "expert?" Don't have using namespace std; in your code. Anywhere.

I stopped using it years ago and now adding std:: to every use of the c++ standard library/STL is done almost without thinking. NOT adding it slows me down when typing code.

I consider having it lazy. Code having that statement looks "wrong."

https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/
Furry Guy wrote:
I stopped using it years ago and now adding std:: to every use of the c++ standard library/STL is done almost without thinking. NOT adding it slows me down when typing code.

Similar experience. Now the standard library words without std:: look weird.
@Enoizat,

There is one "cheat" I use, auto to reduce having to type a multi-word STL construct, such as a constant reverse iterator for a std::vector.

Especially helpful for a compact range-based loop.
@DARKADVERSARY,

One change I would make, after adding std::cin::ignore, is change variable name to a std::string.

19
20
21
      std::cout << "Enter customer's name: ";
      std::string name { };
      std::getline(std::cin, name);

If you need to work with a C-style string in other parts of your code, you can obtain one with std::string::c_str.

http://www.cplusplus.com/reference/string/string/c_str/
Topic archived. No new replies allowed.