0

So I'm trying to set a while loop to keep asking (y/n), but how come when I press 'y' and enter it skips "char itemtitle" and goes straight down to "double itemprice"? I'm really new to c++, please help, i'd apprreciate it

#include <cstdlib>
#include <iostream>
#include <sstream>

using namespace std;

#define tax 9.99
#define shipping ("Free")

void printmessage ()
{
  cout << "*Thanks for your business! We'll ship out your item as soon as possible*\n"
              "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
}

int main(int argc, char** argv)
{

   cout << "Write an invoice program and print it to the console.\n";
   cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n;

   cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
           "Welcome to my Pc Store.\n"
           "'Where Technology is served right'\n"
           "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"<< endl;

   char name [256];
   cout << "Enter your full name: ";
   cin.getline (name, 256);

   char organization [256];
   cout << "What's your organization?: ";
   cin.getline (organization, 256);

   char quit = 'y';    
   do {
        char itemtitle [256];
        cout << "Enter name of Laptop you'd like to order: ";
        cin.getline (itemtitle, 256);

        double itemprice;
        cout << "Enter price of item: $";
        cin >> itemprice; 

        int quantity;
        quantity = 1;
        cout << "How many items?: ";
        cin >> quantity;

        if (quantity == 1) {     
            cout << "1 item(s)\n";  
        } else  {
           cout << quantity << " item(s)\n";
        } 

        cout << "Tax: $" << tax;

        double subtotal;
        subtotal = quantity * itemprice + tax;
        cout << "\nSub Total: $" << subtotal;

        cout << "\n";       
        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"
               "Validation:\n\n";

        cout << "Item(s):\t\t\t\t\t" << "Amount:\n";       
        cout << itemtitle << "\t\t\t\t\t$" << quantity * itemprice + tax; ;
        cout << "\n" << quantity << " * $" << itemprice << " + $" << tax;

        cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
        cout << "Payment (Credit/Debit only)!\n\n";

        double cardnum;
        cout << "Card Numbers: ";
        cin >> cardnum;

        int ssn;
        cout << "SSN: ";
        cin >> ssn;

        cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
               "Invoice Details:\n\n";

        time_t rawtime;
        struct tm * timeinfo;
        time ( &rawtime );
        timeinfo = localtime ( &rawtime );
        cout << "Date&Time: " << ("Current local time and date: %s", asctime (timeinfo));

        cout << name;
        cout << "\n" << organization;
        cout << "\n" << "("<< quantity << ")" << " " << itemtitle;

        cout << "\n\t\t\t" << "Total: $" << quantity * itemprice;
        cout << "\n\t\t\t" << "Tax: $" << tax;
        cout << "\n\t\t\t" << "Shipping: " << shipping;
        cout << "\n\n\t\t\t" << "Balance Due: $" << quantity * itemprice + tax;

        cout << "\n\t\t\t" << "Payment: " << "Paid" ;

        cout << "\n\n\nDo you want to continue shopping? (y/n): ";
        cin >> quit ;

        cout << "\n\n";

    } while (quit != 'n');

    cout << "\n\n\n";
    printmessage ();

    return 0;
}
jrok
  • 54,456
  • 9
  • 109
  • 141

2 Answers2

2

std::cin.operator>>() leaves the '\n' character in the buffer, making it pretty much unusable with getline. You have to pick either and then change the code accordingly.

For example, use only getline, then parse the resulting string inside.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
0

When you use both "cin >>" and "cin.getline()" to read from user input, you will have this problem mostly. It seems that when using ">>" operator, after reading the input, '\n' is left in the input stream, which cause a following call to "getline" to return immediately.

Two solutions.

First, add "cin.ignore();" before "cin.getline(itemtitle, 256);". "cin.ignore()" extracts characters from the input sequence and discards them. It will clear the input stream.

Second, replace "cin.getline(itemtitle, 256);" with "cin >> itemtitle;". I suggest using either "cin.getline" or "cin >>" in your code, but don't use both. "cin >>" would be better.

Besides, should put "#include < ctime >" in the top of your code.

Huang Zhu
  • 21
  • 2