My C++ program stops half way

I am new to the programming. I have an assignment that required me to write a program that sells items. The program is supposed to ask the user a couple question like name, address. Then give a description of the items being sold. Then ask the user to select an item and quantity. The program is then supposed to multiply the price of the product with the quantity and state how much the customer owes. Here is my code so far, but C++ stops the code after the descriptions. I am lost. I have no idea. Would someone look at my code and give me ideas?

#include <iostream>
#include <string>
using namespace std;

int main ()
{
//Request information from user
string mystr;
cout << "Hello, please enter your name to get started.";
getline (cin, mystr);
cout << "Hello " << mystr << ", welcome.\n";
cout << "What is your home address? ";
getline (cin, mystr);
cout << "Thank you, you entered " << mystr << ". Please enter your home phone number.";
getline (cin, mystr);
cout << "Thank you, you entered " << mystr << ".\n";

//Variable declaration
int book, quantity=0;
int bookA=4, bookB=5, bookC=6, bookD=7, bookE=8;

//Display the items for sale
cout<< "Please choose a book you would like to purchase. The following is a list of books and a quick summary. " ;
cout<< "( Book A ) Where the Sidewalk Ends is a classic collection of clever and hilarious poems for every age. " ;
cout<< "( Book B ) Charlie and the Chocolate Factory is a delightful story of a poor boy winning a golden ticket that changes his life. " ;
cout<< "( Book C ) Bridge to Terabithia is a heart felt story about imagination, friendship and loss." ;
cout<< "( Book D ) The Hobbit is an epic story full of adventure, intrigue and imagination." ;
cout<< "( Book E ) Heartlight is a fantastically written science fiction story that inspires the imagination of science." ;


//Customer selection
cout<< "Enter book number to purchase";
cin>> book;
cout<< "Enter the quantity you would like";
cin>> quantity;

switch ( book )
{
if (bookA==4)
cout<< bookA*quantity;

if (bookB==5)
cout<< bookB*quantity;

if (bookC==6)
cout<< bookC*quantity;

if (bookD==7)
cout<< bookD*quantity;

if (bookE==8)
cout<< bookE*quantity;

}
return 0;
}
Not sure what is your problem exactly, but if your program is skipping a cin, you can try putting a cin.ignore(); the line before that cin you have a problem with, like so :

1
2
3
4
// ...
cin.ignore();
cin >> x;
// ... 
Last edited on
Does it stop running as in you get errors? If you get errors, what are they? You should look at this websites articles on switch statements, as it looks like you have the syntax wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Switch(book)
{
case 'A':
    cout << quantity*bookA;
case 'B':
    cout << quantity*bookB;
case 'C':
    cout << quantity*bookC;
case 'D':
    cout << quantity*bookD;
case 'E':
    cout << quantity*bookE;
default:
     //Error, not a valid entry, handle accordingly
}

This is what I think you are trying to accomplish
Last edited on
A int can not be assigned a letter only a number so if you try to assign a letter to an int your compiler is going to freak out and exit the program. So instead of int book try char book as a char can be assigned a letter.
Thank you to everyone. This did truly help. I was able to get it to finish. I know I have alot of work left on this project, especially because I now have to add a new class to it. But thank you for your help thus far.
Glad to hear you got it working. Good luck.
Topic archived. No new replies allowed.