1

I've been working on a shopping cart assignment lately and couldn't understand why would this code below not work.

I have to write a code that will output name, cost and quantity of items, for three items without using arrays.

At the end of the maximum of three items I have to display out the total cost of the items. Currently I'm stuck because after I add in a second item, it appears that the program does not ask for the first input (Item name). Here's the code:

#include <iostream>

int main() {
  int total;

  std::cout << "Item name:";
  std::string itemName;
  std::getline(std::cin,itemName);

  std::cout << "Cost(in cents):";
  int cost;
  std::cin >> cost;

  std::cout << "Quantity:";
  int quantity;
  std::cin >> quantity;

  std::cout << "Do you want to add more items? (Y/N)";
  char option;
  std::cin >> option;

  if (option == 'y') {
    std::cout << "Item name:";
    std::string itemName2;
    std::getline(std::cin,itemName2);

    std::cout << "Cost(in cents):";
    int cost2;
    std::cin >> cost2;

    std::cout << "Quantity:";
    int quantity2;
    std::cin >> quantity2;

    std::cout << "Do you want to add more items? (Y/N)";
    char option2;
    std::cin >> option2;

    if (option2 == 'y') {
      std::cout << "Item name:";
      std::string itemName3;
      std::getline(std::cin,itemName3);

      std::cout << "Cost(in cents):";
      int cost3;
      std::cin >> cost3;

      std::cout << "Quantity:";
      int quantity3;
      std::cin >> quantity3;

      total = cost*quantity + cost2*quantity2 + cost3*quantity3;
      std::cout << "Total value:" << total;
    }
    else {
      total = cost*quantity + cost2*quantity2;
      std::cout << "Total value:" << total;
    }
  }
  else {
    total = cost*quantity;
    std::cout << "Total value:" << total;
  }

  return 0;
}

After I input 'y' after every item input, the code would somehow skip my input for itemName and output the 'Cost(in cents):' together with 'Item name:' in the same line.

I do think that it's got something to do with the getline() function, but I do not know exactly what. Any help is greatly appreciated.

4

3 回答 3

1

首先,您正在使用getline并且没有包含<string>头文件。

第二,你可能会因为cin缓冲而面临问题。您应该cin.ignore()在输入用于option提取字符并丢弃或其他选项可能会清除cin缓冲区后使用。

cin.ignore()将用于忽略流中的 1 个字符。
你可以试试std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 这将提取尽可能多的字符,直到 ``\n''


我在VS2012上尝试了以下代码,它工作正常。

#include <iostream>
#include <string>


int main() {
int total;

std::cout << "Item name:";
std::string itemName;
std::getline(std::cin,itemName);

std::cout << "Cost(in cents):";
int cost;
std::cin >> cost;

std::cout << "Quantity:";
int quantity;
std::cin >> quantity;

std::cout << "Do you want to add more items? (Y/N)";
char option;
std::cin >> option;
std::cin.ignore();

if (option == 'y') {
std::cout << "Item name:";
std::string itemName2;
std::getline(std::cin,itemName2);

std::cout << "Cost(in cents):";
int cost2;
std::cin >> cost2;

std::cout << "Quantity:";
int quantity2;
std::cin >> quantity2;

std::cout << "Do you want to add more items? (Y/N)";
char option2;
std::cin >> option2;
  std::cin.ignore();

if (option2 == 'y') {
  std::cout << "Item name:";
  std::string itemName3;
  std::getline(std::cin,itemName3);

  std::cout << "Cost(in cents):";
  int cost3;
  std::cin >> cost3;
  std::cout << "Quantity:";
  int quantity3;
  std::cin >> quantity3;

  total = cost*quantity + cost2*quantity2 + cost3*quantity3;

  std::cout << "Total value:" << total;
}
  else {
  total = cost*quantity + cost2*quantity2;
  std::cout << "Total value:" << total;
}
}
else {
total = cost*quantity;
std::cout << "Total value:" << total;
}

system("pause");
return 0;
}

有关详细信息,cin.ignore() 请参阅此链接。

于 2013-07-21T18:04:05.733 回答
0

你应该使用

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

对于删除\n,输入后保留option

于 2013-07-21T14:36:51.600 回答
-1

你可以这样做: std:cin>>itemName

而不是这样做: std::getline(std::cin,itemName)

对于没有空格的字符串,这将是最简单的方法!

于 2013-07-21T17:59:17.863 回答