Mid Project pt 1. PLZ HELP

Oct 8, 2015 at 12:13am
Assume an individual is purchasing a ticket or tickets for a concert. The program should start by asking the user how many tickets are being purchased (accept only a positive number from 1 - 25- keep asking the user until an appropriate response is entered). I Have No Clue How To DO THis PLZ HELP

#include <iostream>
using namespace std;

int main()
{
int tickets;
cout << "How Many Tickets Do You Want?" << endl;
cin >> tickets;
cout << "You order is" << endl;
tickets = tickets +20 ;
cout << tickets << ;

system("pause");
return 0;
}
Oct 8, 2015 at 12:19am
Look at the lines 10-14. I posted something to give you an idea.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
int tickets;
cout << "How Many Tickets Do You Want?" << endl;
cin >> tickets;

//while (tickets is not between 1 and 25)
//{
     //notify the user the input is not correct.
    // ask the user to input the number of tickets again.
//}

cout << "You order is" << endl;
tickets = tickets +20 ;
cout << tickets << ;

system("pause");
return 0;
} 


I hope it helps.
Last edited on Oct 8, 2015 at 9:20pm
Oct 8, 2015 at 12:25am
Maybe you could try this..

1
2
3
4
5
6
7
8

if (tickets < 1 || tickets > 25)
{

cout << " You have entered an invalid ticket number, please enter between 1-25" << endl;
cin >> tickets;
}
Oct 8, 2015 at 12:27am
Thank you so much im going to try now and see if i can make it work!
Oct 8, 2015 at 9:11pm
That is the code i wrote for it but i keep getting a syntax error



#include <iostream>
using namespace std;

int main()
{
int tickets;

if (tickets < 1 || tickets > 25)
{
cout << "How Many Tickets Do You Want?" << endl;
cin >> tickets;

cout << " You have entered an invalid ticket number, please enter between 1-25" << endl;
cin >> tickets;
}

system("pause");
return 0;
}
Oct 8, 2015 at 9:18pm
On line 5 you declare the variable. However, on line 7, you are using an if statement. Something to think about, How does the program know the value of tickets to compare without the user input? Wouldn't the program like to know the number of tickets to purchase prior to compare if it is a valid input?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
 using namespace std;
 
int main()
 {
 int tickets;

 if (tickets < 1 || tickets > 25)
 {
 cout << "How Many Tickets Do You Want?" << endl;
 cin >> tickets;
 
cout << " You have entered an invalid ticket number, please enter between 1-25" << endl;
 cin >> tickets;
 }
 
system("pause");
 return 0;
 } 


I hope it helps
Last edited on Oct 8, 2015 at 9:27pm
Oct 8, 2015 at 9:18pm
That's because in your if statement you check if tickets is less than 1 or bigger than 25. But the variable ticket has no value, you never set any value to it.

1
2
3
int tickets;

if (tickets < 1 || tickets > 25)


Do you see the problem? Ticket has no value. Wouldnt you want to ask the question of how many tickets they want before the if statement?
Oct 8, 2015 at 9:22pm
tickets is an uninitialized variable (it contains garbage). You can't meaningfully test it until you have entered a number.

Your instructions say "keep asking the user until an appropriate response is entered". That implies a loop. You don't have a loop. A do while loop is the usual idiom for doing this.

1
2
3
4
5
6
    do
    { cout << "How Many Tickets Do You Want (1-25)?";
        cin >> tickets;
        if (tickets < 1 || tickets > 25) 
          cout << " You have entered an invalid number of tickets" << endl;
    } while (tickets < 1 || tickets > 25);


edit: corrected a missing cout << at lline 2.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on Oct 9, 2015 at 3:57pm
Oct 8, 2015 at 9:26pm
This should fix your issue, didn't compile it myself however. Just moved your ticket question outside of the loop, now the loop has data to work with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
 using namespace std;
 
int main()
 {
 int tickets;

 cout << "How Many Tickets Do You Want?" << endl;
 cin >> tickets;
 

 if (tickets < 1 || tickets > 25) 
 {

cout << " You have entered an invalid ticket number, please enter between 1-25" << endl;
 cin >> tickets;
 }
 
system("pause");
 return 0;
 } 
Last edited on Oct 8, 2015 at 9:29pm
Oct 8, 2015 at 9:30pm
okay thank you very much everyone
Oct 8, 2015 at 9:32pm
What @Outlaw782 presented fixes the syntax error, but it's probably not what you want. You want a loop and not an if statement. Currently with the if statement, it only allows the user to enter a wrong number twice. so if I first type 55, it will tell me it's invalid and ask me agian. If I now type 100, the program will go on as if it's all good and end.

You want a loop replacing the if statement, saying that it should give the invalid message and ask the user to enter again as long as ticket is less than 1 or above 25.
Oct 8, 2015 at 10:03pm
@TarikNeaj is correct, didn't read your question well enough :P
I think this is what you may be after..
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
#include <iostream>
 using namespace std;
 
int main()
 {
 int tickets;

 cout << "How Many Tickets Do You Want?" << endl;
 cin >> tickets;

 if (tickets >= 1 && tickets <= 25)
 {
 cout << "Correct Response!" << endl;
 }

 while (tickets < 1 || tickets > 25)
 {
	cout << "Invalid response, Enter again" << endl;;
	cin >> tickets;

	if (tickets >= 1 && tickets <= 25)
		{
		cout << "Correct Response!" << endl;
		}
 }
 
system("pause");
 return 0;
 } 
Topic archived. No new replies allowed.