if control statement

hey, I am working on project and I want to make the person choose and enter what he like to see and eat during movies
the problem that it stops after I enter "yes" to the program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
main()
{
	/* noft: number of tickets , nofi: number of items (other than the tickets) 
	sumt: sum of noft , sumpt: sum of price of ticket , sumi : sum of nofi(popcorn,juice,water)
	sum: sum of price of everything ,n_p: number of popcorn , p_p: price of popcorn , etc...*/
	int n_t=1,n_i;
	char name,response1,yes,no,choice,A,B,C;
	int n_p,p_p=0,n_j,p_j=0,n_w,p_w=0,sumpt=0,sumi=0,sum=0;
	const int pticket=20,ppopcorn=10,pjuice=10,pwater=3;
	
	
	cout<<" Welcome to the Cinema ";
	cout<<"\n Please Enter your Name :";
	cin>>name;
	cout<<"\n Please Enter the number of tickets you would like"<<endl;
	cin>>n_t;
	sumpt=sumpt+pticket*n_t;
	cout<<"\n Price of tickets ="<<sumpt<<"SR";
	cout<<"\n Would you like any other item ?";
	cout<<"\n Enter yes or no"<<endl;
	cin>>yes>>no;
	if (yes)
	{   cout<<"\n Choose from the following items : \n A-Popcorn \n B-Juice \n C-Water"<<endl;
	switch (choice)
	{
		case'A':cout<<"\n How many Popcorn buckets would you like"<<endl;
		cin>>n_p;
		{
				(p_p=p_p+ppopcorn*n_p);
			cout<<"\n Price of Popcorn ="<<p_p<<"SR";
			}
			break;
		case'B':cout<<"\n How many juice bottles would you like ?"<<endl;
			cin>>n_j;
			{
			
			(p_j=p_j+pjuice*n_j);
			cout<<"\n Price of Juice ="<<p_j<<"SR";
			}
			break;
		case'C':cout<<"\n How many water bottles would you like ?"<<endl;
			cin>>n_w;
			{
			
			(p_w=p_w+pwater*n_w);
			cout<<"\n Price of Water ="<<p_w<<"SR";
			}
			break;	
			cout<<"\n Thank you for choosing our cinema ";
		(sumi=sumi+p_p+p_j+p_w);
		(sum=sum+sumi+sumpt);
		cout<<"\n Total Price of Items = "<<sumi<<"SR";
		cout<<"\n Total Price of Items Including Tickets ="<<sum<<"SR";		
	}
}
	else if (no)
	{
		cout<<"\n Thank you for choosing our cinema";
		cout<<"\n Total Price of Tickets ="<<sumpt<<"SR";
		
	}

	getch();
}
cin>>yes>>no;
You're asking the user to enter two different things here. Also, your 'yes' and 'no' variables are chars, meaning they can only store a single character of data each. Furthermore, note that the name of a variable is solely for the benefit of the person reading/writing the code. Your variables could be called "apple" and "banana" and the behavior of your program would still be the same.

First, add:
#include <string> to the beginning of your code.
Then, remove your 'yes' and 'no' variables. You also don't use your 'response1' variable, so you that can be removed, too.

Next, declare a variable like:
string response;

Then, to put input into that string, you can do:
cin >> response; instead of cin>>yes>>no;

To check if the user entered a valid option, you can do something like:
1
2
3
4
if (response == "yes" || response == "Yes" || response == "y" || response == "Y")
{

}

Last edited on
Hello samzavax,

In addition to what Ganado has said I worked on your code a bit and ended up with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <limits>
#include <string>
//#include<conio.h>
//#include <cmath>  // <--- Not using anything from this header file and it should be the C++ version cmath.
#include <cctype>   // <--- For "std::tolower() and std::toupper()" + others.

using namespace std;  // <--- Best not to use.

int main()  // <--- "main" must return an "int"
{
    constexpr int TICKET_PRICE{ 20 }, PPOPCORN{ 10 }, PJUICE{ 10 }, PWATER{ 3 };

    int numOfTickets{}, numOfItems{};  // <--- ALWAYS initialize your variables.
    char /*response1{},*/ yesNo{}, choice{'a'}/*, A{}, B{}, C{}*/;
    int numOfPopcorn{}, priceOfPopcorn{}, numOfJuice, priceOfJuice{}, numOfWater{}, priceOfWater{};
    int totalTicketPrice{}, totalSale{}, sum{};
    std::string name;  // <--- Empty at this point. Does not need initialized.

    cout <<
        "\n Welcome to the Cinema\n"
        "\n Please Enter your Name: ";
    std::getline(std::cin, name);  // <--- Changed from formatted input to unformatted input incase there is a space in the name.

    cout << "\n Please Enter the number of tickets you would like: ";
    cin >> numOfTickets;

    totalTicketPrice += TICKET_PRICE * numOfTickets;

    cout << "\n Price of tickets = " << totalTicketPrice << "SR";

    cout << "\n\n Would you like any other item (Y or N)? ";
    //cout << "\n Enter yes or no (Y or N): ";
    cin >> yesNo;

    if (std::toupper(yesNo) == 'Y')
    {
        cout << 
            "\n"
            " Choose from the following items:\n"
            "  A - Popcorn \n"
            "  B - Juice \n"
            "  C - Water\n"
            "  D - Exit\n"
            "   Enter choice: ";
        // <---  Nice menu, but what do you enter the choice into?

        switch (std::toupper(choice))  // <--- "choice" would have a value of (\0) at this point.
        {
            case'A':
                cout << "\n\n How many Popcorn buckets would you like: ";
                cin >> numOfPopcorn;

                priceOfPopcorn += PPOPCORN * numOfPopcorn;

                cout << "\n Price of Popcorn = " << priceOfPopcorn << "SR";

                break;
            case'B':
                cout << "\n How many juice bottles would you like: ";
                cin >> numOfJuice;

                priceOfJuice += PJUICE * numOfJuice;

                cout << "\n Price of Juice =" << priceOfJuice << "SR";

                break;
            case'C':
                cout << "\n How many water bottles would you like: ";
                cin >> numOfWater;

                priceOfWater += PWATER * numOfWater;

                cout << "\n Price of Water =" << priceOfWater << "SR";

                break;

                //cout << "\n Thank you for choosing our cinema ";  // <--- These lines are never executed because of the break. Should be after the last else statement.
                //totalSale += priceOfPopcorn + priceOfJuice + priceOfWater;
                //(sum += totalSale + totalTicketPrice);
                //cout << "\n Total Price of Items = " << totalSale << "SR";
                //cout << "\n Total Price of Items Including Tickets =" << sum << "SR";
        }
    }
    else if (std::toupper(yesNo) == 'N')
    {
        cout << "\n Thank you for choosing our cinema";
        cout << "\n Total Price of Tickets =" << totalTicketPrice << "SR";

    }
    else
    {
        std::cerr << "\n     Invalid input!\n\n";
    }

    cout << "\n\n Thank you " << name << " for choosing our cinema.";  // <--- These lines are never executed because of the break. Should be after the last else statement.
    totalSale += priceOfPopcorn + priceOfJuice + priceOfWater;
    sum += totalSale + totalTicketPrice;

    cout << "\n Total Price of Items = " << totalSale << "SR";
    cout << "\n Total Price of Items Including Tickets = " << sum << "SR";

    // A fair C++ replacement for "system("pause")" or "getch()". Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}


When I ran it I get this output:

 Welcome to the Cinema

 Please Enter your Name: Handy Andy

 Please Enter the number of tickets you would like: 2

 Price of tickets = 40SR

 Would you like any other item (Y or N)? y

 Choose from the following items:
  A - Popcorn
  B - Juice
  C - Water
  D - Exit   Enter choice:

 How many Popcorn buckets would you like: 1

 Price of Popcorn = 10SR

 Thank you Handy Andy for choosing our cinema.
 Total Price of Items = 10SR
 Total Price of Items Including Tickets = 50SR

 Press Enter to continue:


In your code: The "conio.h" header file is no longer used.
salem c once wrote:

#include<conio.h>
Obsolete since 1990, when the world stopped using DOS as a primary operating system.


You are not using anything from the "math.h" header file and as noted it should be "cmath" for a C++ program.

In "main" I like to put constant variables first, so that they are easy to find if a change is made.

If you need a comment block to explain your variables then they need a better name. The names I used are suggestions or at least an idea of what you can do. And do not be afraid of long names. A good IDE can help you finish writing a variable name.

Along with the blank lines to make your code more readable I added some (\n)s in some of the strings for a better looking output.

I used the "std::getline" in line 23 in case someone would enter 1 or more spaces in what they enter. The formatted would only store whatever is entered up to the first space leaving what is left in the buffer for the next input.

Lines 32 and 33 I shortened to 1 line. And as you can see i combined the variables "yes" and "no" into 1 name. You might also call it "menuChoice" for what it does.

I did notice 1 problem. Should you enter the if statement you only get to choose 1 item and you are done. What is in the if statement should be in a do/while loop until the person is finished. Hence the "Exit" in the menu choices.

At the end of "case 'C': the lines that follow the "break" are never reaches because the "break" will leave the switch. That is why I moved them to after all the if/else if/else statements because they are adding every purchase up before the final output.

Anything that you do not understand let me know.

Andy
samzavax,
This makes no sense.
1
2
3
4
char name;

cout<<"\n Please Enter your Name :";
cin>>name;

Names are generally more than one letter. Declare it as string and use getline to make it simpler.

e.g.
1
2
3
4
5
string name;

cout << " Welcome to the Cinema! \n";
cout << "\n Please enter your name. \n: ";
getline (cin, name);

You will need to #include the <string> library.

Handy Andy,
I thought he was trying to make a default case with the weird stuff after the last break. But what you said makes more sense.
Perhaps something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <string>
#include <limits>
#include <cctype>

int getInt(const std::string& prm)
{
	int i {};

	while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
		std::cout << "Not an integer\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

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

double getNum(const std::string& prm)
{
	double i {};

	while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
		std::cout << "Not a number\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

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

int main()
{
	constexpr double TICKET_PRICE {20.0}, PPOPCORN {10.0}, PJUICE {10.0}, PWATER {3.0};

	double priceOfPopcorn {}, priceOfJuice {}, priceOfWater {};
	std::string name, yesNo;

	std::cout <<
		"\nWelcome to the Cinema\n"
		"\nPlease Enter your Name: ";

	std::getline(std::cin, name);

	const auto numOfTickets {getInt("\nPlease Enter the number of tickets you would like: ")};
	const auto totalTicketPrice {TICKET_PRICE * numOfTickets};

	std::cout << "\nPrice of tickets = " << totalTicketPrice << "SR\n";

	char other {};

	while ((std::cout << "\nWould you like any other item (Y or N)? ") && (std::cin >> yesNo) && ((other = static_cast<char>(std::toupper(yesNo[0]))) != 'N' && other != 'Y'))
		std::cout << "Invalid option\n";

	if (other == 'Y') {
		for (bool again {true}; again; ) {
			std::cout << "\n"
				"Choose from the following items:\n"
				" A - Popcorn \n"
				" B - Juice \n"
				" C - Water\n"
				" D - Exit\n";

			std::string choice;

			std::cout << "\nEnter choice: ";
			std::cin >> choice;

			switch (std::toupper(choice[0])) {
				case 'A':
					priceOfPopcorn += PPOPCORN * getInt("\n\nHow many Popcorn buckets would you like: ");
					std::cout << "\nPrice of Popcorn = " << priceOfPopcorn << "SR\n";
					break;

				case 'B':
					priceOfJuice += PJUICE * getInt("\nHow many juice bottles would you like: ");
					std::cout << "\nPrice of Juice =" << priceOfJuice << "SR\n";
					break;

				case 'C':
					priceOfWater += PWATER * getInt("\nHow many water bottles would you like: ");
					std::cout << "\nPrice of Water =" << priceOfWater << "SR\n";
					break;

				case 'D':
					again = false;
					break;

				default:
					std::cout << "\nInvalid input!\n";
					break;
			}
		}
	}

	std::cout << "\n\nThank you " << name << " for choosing our cinema.\n";
	std::cout << "\nTotal Price of Tickets = " << totalTicketPrice << "SR";

	const auto totalSale {priceOfPopcorn + priceOfJuice + priceOfWater};
	const auto sum {totalSale + totalTicketPrice};

	std::cout << "\nTotal Price of Items = " << totalSale << "SR";
	std::cout << "\nTotal Price of Items Including Tickets = " << sum << "SR\n";
}

Last edited on
Um, don't you need curly brackets for separating cases in switch statements? Unless they're one line, I mean.

e.g.
1
2
3
4
5
6
case 'A':
{
     priceOfPopcorn += PPOPCORN * getInt("\n\nHow many Popcorn buckets would you like: ");
     std::cout << "\nPrice of Popcorn = " << priceOfPopcorn << "SR\n";
}
break;


I was taught to always use curly brackets in places like that, but if I'm wrong, please enlighten me!
You don't need braces in case statements for most uses. Only when a variable is defined within a case might braces be needed.

I don't put braces around case statements until the compiler whinges about not having them.

I learned that the hard way when creating Windows Desktop apps. The idea also applies to C++ console apps.
Topic archived. No new replies allowed.