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.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
Bili Xu
  • 25
  • 2
  • 8
  • possible duplicate of [getline not asking for input?](http://stackoverflow.com/questions/6642865/getline-not-asking-for-input) – interjay Jul 21 '13 at 14:32

3 Answers3

1

Frsitly, you are using getline and haven't included <string> header file.

2ndly, you may be facing issue because of cin buffer. you should use cin.ignore() after taking input for option for extracting the characters and discarding or other option may be clearing cin buffer.

cin.ignore() will work for ignoring 1 character in stream.
you can try std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); This will extract as many characters till ``\n''


I tried following code on VS2012, and it worked properly.

#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;
}

For details on cin.ignore() see this link.

OGHaza
  • 4,795
  • 7
  • 23
  • 29
Shumail
  • 3,103
  • 4
  • 28
  • 35
0

You should use

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

For remove \n, remained after inputting option.

awesoon
  • 32,469
  • 11
  • 74
  • 99
-1

you can do: std:cin>>itemName

instead of doing: std::getline(std::cin,itemName)

This would be the easiest approach for the strings without spaces!

armanxxx
  • 45
  • 1
  • 10
  • That's not a good approach for string input since it will not work properly if white space comes in string. – Shumail Jul 21 '13 at 18:04