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 << ;
#include <iostream>
usingnamespace 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;
}
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>
usingnamespace 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;
}
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?
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 dowhile 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.
#include <iostream>
usingnamespace 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;
}
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.