C++ 为什么while循环跳过char?

C++ 为什么while循环跳过char?,c++,C++,所以我试图设置一个while循环来继续询问(y/n),但为什么当我按下“y”并输入时,它会跳过“char itemtitle”,直接转到“double itemprice”?我对C++很陌生,请帮助,我会认可它< /P> #include <cstdlib> #include <iostream> #include <sstream> using namespace std; #define tax 9.99 #define shipping ("Free

所以我试图设置一个while循环来继续询问(y/n),但为什么当我按下“y”并输入时,它会跳过“char itemtitle”,直接转到“double itemprice”?我对C++很陌生,请帮助,我会认可它< /P>
#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;
}
#包括
#包括
#包括
使用名称空间std;
#定义税9.99
#定义运输(“免费”)
无效打印消息()
{
cout
std::cin.operator>>()
'\n'
字符保留在缓冲区中,使其在
getline
中几乎不可用。您必须选择其中一个,然后相应地更改代码

例如,仅使用
getline
,然后解析其中的结果字符串。

当同时使用“cin>>”和“cin.getline()”从用户输入中读取时,大多数情况下都会出现此问题。似乎在使用“>>”运算符时,在读取输入后,“\n”留在输入流中,这会导致对“getline”的以下调用立即返回

两种解决方案

首先,在“cin.getline(itemtitle,256);”之前添加“cin.ignore();”。“cin.ignore()”从输入序列中提取字符并丢弃它们。它将清除输入流

其次,将“cin.getline(itemtitle,256);”替换为“cin>>itemtitle;”。我建议在代码中使用“cin.getline”或“cin>>”,但不要两者都使用。“cin>>”会更好


此外,还应加上“#include”在代码顶部。

与往常一样的问题。cin将\n保留在缓冲区中,因此下一个getline立即返回。旁注:太多的
#define
。Etienne请写一封回复信,这样我就可以进行正式的向上投票:)@speeder我不会回复重复的邮件。我不是重复的。这是一个上帝****你在那里做循环或者,你可以使用<代码> CIN。IGRANE()/CUT>技巧来在“代码>操作符> <代码>调用之后清空缓冲区。这是神秘的。我们这里讨论的是C++。