我真的需要帮助,如何使用函数编写输入验证。我知道while循环验证,但在这种情况下我真的想知道如何编写一个函数来验证我的输入,这样我就可以从输入中调用函数来处理不正确的输入。
在我的代码中,我接受来自用户输入和字符的整数:
# include <iostream>
using namespace std;
//Function Declarations
//Shouf shouf Habibi row.
int shouf_normal (int a, int b )
{
int shouf_normal=a* b;
return shouf_normal;
}
int shouf_Premium(int b, int c)
{
int shouf_premium=b*c;
return shouf_premium;
}
//The interview row
double interview_normal(double c, double d)
{
double interview_normal=(c*d);
return interview_normal;
}
double interview_premium(double d, double e)
{
double interview_premium=d*e;
return interview_premium;
}
//New Kids turbo row
int turbo_normal(int e, int f)
{
int turbo_normal=e*f;
return turbo_normal;
}
// The Butterflies row
int butterflies_normal(int f, int g)
{
int butterflies_normal=f*g;
return butterflies_normal;
}
int main()
{
cout<<"\n\t"<<endl;
cout<<"\t|******************************************************************|"<<endl;
cout<<"\t|Welcome to A31 Cinema, where you watch latest and cheapest movies.|"<<endl;
cout<<"\t|******************************************************************|"<<endl;
cout<<"\n\tBelow are the movies screening lists.\n\tThe price of tickets are given according to seats classes\n\tand quality of movie."<<endl;
cout<<"\n\tNote:Glassses for 3D movies cost $3.00."<<endl;
cout<< "\t__________________________________________________________________"<<endl;
cout<<"\t| Movie Title | Normal | Premium |"<<endl;
cout<<"\t|_____________________|_____________________|______________________|"<<endl;
cout<<"\t|1.Shouf Shouf Habibi | $5.00 | $7.00 |"<<endl;
cout<<"\t|_____________________|_____________________|______________________|"<<endl;
cout<<"\t|2.The Interview (3D) | $7.00 | $9.50 |"<<endl;
cout<<"\t|_____________________|_____________________|______________________|"<<endl;
cout<<"\t|3.New Kids Turbo (3D)| $7.00 |"<<endl;
cout<<"\t|_____________________|_____________________|"<<endl;
cout<<"\t|4.Black Butterflies | $5.00 |"<<endl;
cout<<"\t|_____________________|_____________________|"<<endl;
//constant initials
const int a=5;
const int b=7;
const double c=7;
const double d=9.5;
const int e=7;
const int glass=3;
const int f=5;
double total_tickets=0;
int choice=0;
int number=0;
char reply;
bool correct_entry=true;
string not_int;
int not_char;
cout<<"\n\tPlease choose from the lists (1-4) above and press enter:";
cin>>number;
while(number>=1 && number<=4)
{
if (number==1)
{
cout<<"\n\tYou have selected \"Shouf Shouf Habibi\".";
cout<<"\n\tFor Normal; Press \"N\" or for premium class; Press \"P\":";
cin>>reply;
if(reply=='N'|| reply=='n')
{
cout<<"\n\tYou have selected Normal class."<<endl;
cout<<"\n\tPlease choose the number of tickets you want:";
cin>>choice;
total_tickets=shouf_normal(a,choice);
cout<<"\n\tYour total costs is=$"<<total_tickets<<endl; //calculating & printing out cost
}
if(reply=='P'||reply=='p')
{
cout<<"\n\tYou have selected Premium class."<<endl;
cout<<"\n\tPlease choose the number of tickets you want:";
cin>>choice;
total_tickets=shouf_Premium (b,choice);
cout<<"\n\tYour total costs is=$"<<total_tickets<<endl; //calculating & printing out cost
}
}
if(number==2)
{
cout<<"\n\tYou have selected \"The interview\"in 3D quality.";
cout<<"\n\tFor Normal; press \"N\" or for premium class; Press \"P\":";
cin>>reply;
if(reply=='N'|| reply=='n')
{
cout<<"\n\tYou have selected Normal class.";
cout<<"\n\tPlease choose the number of tickets you want:";
cin>>choice;
total_tickets=interview_normal(c,choice)+(glass*choice);
cout<<"\n\t Price for one ticket is $"<<c<<endl;
cout<<"\n\tPrice for one 3D glass is $"<<glass<<endl;
cout<<"\n\t Your total costs is=$"<<total_tickets<<endl;
}
if(reply=='P'|| reply=='p')
{
cout<<"\n\tYou have selected Premium class.";
cout<<"\n\tPlease choose the number of tickets you want:";
cin>>choice;
total_tickets=interview_premium(d,choice)+(glass*choice);
cout<<"\n\t Price for one ticket is $"<<d<<endl;
cout<<"\n\tPrice for one 3D glass is $"<<glass<<endl;
cout<<"\n\t Your total costs is=$"<<total_tickets<<endl;
}
}
if(number==3)
{
cout<<"\n\tYou have selected \"The New Kids Turbo\" in 3D quality.";
cout<<"\n\tThis moview is available only in Normal class.";
cout<<"\n\tPlease choose the number of tickets you want:";
cin>>choice;
total_tickets=turbo_normal(e,choice)+(glass*choice);
cout<<"\n\t Price for one ticket is $"<<e<<endl;
cout<<"\n\tPrice for one 3D glass is $"<<glass<<endl;
cout<<"\n\tYour total costs is=$"<<total_tickets<<endl;
}
if (number==4)
{
cout<<"\n\tYou have selected \"Black Butterflies\".";
cout<<"\n\tThis Movie is available only in Normal class.";
cout<<"\n\tPlease choose the number of tickets you want:";
cin>>choice;
total_tickets=butterflies_normal(f,choice);
cout<<"\n\tYour total costs is=$"<<total_tickets<<endl;
}
cout<<"\n\tPlease choose from the lists (1-4) above and press enter or \n\t press \"Q\" to quit:";
cin>>number;
while( number!=(number>=1&& number<=4))
{
cout<<"\n\tPlease choose from the lists (1-4) above and press enter or \n\t press \"Q\" to quit:";
cin>>number;
}
if(number=='Q'|| number=='q')
{
cout<<"\n\t\tThank You purchasing movie tickets with us!"<<endl;
}
}
return 0;
}
答案 0 :(得分:0)
cin
具有fail()
方法,可用于检查读取是否成功,并附带正确放置的代码。
试试这个:
# include <iostream>
using namespace std;
//Function Declarations
//Shouf shouf Habibi row.
int shouf_normal (int a, int b )
{
int shouf_normal=a* b;
return shouf_normal;
}
int shouf_Premium(int b, int c)
{
int shouf_premium=b*c;
return shouf_premium;
}
//The interview row
double interview_normal(double c, double d)
{
double interview_normal=(c*d);
return interview_normal;
}
double interview_premium(double d, double e)
{
double interview_premium=d*e;
return interview_premium;
}
//New Kids turbo row
int turbo_normal(int e, int f)
{
int turbo_normal=e*f;
return turbo_normal;
}
// The Butterflies row
int butterflies_normal(int f, int g)
{
int butterflies_normal=f*g;
return butterflies_normal;
}
void display_menu() {
cout<<"\n\t"<<endl;
cout<<"\t|******************************************************************|"<<endl;
cout<<"\t|Welcome to A31 Cinema, where you watch latest and cheapest movies.|"<<endl;
cout<<"\t|******************************************************************|"<<endl;
cout<<"\n\tBelow are the movies screening lists.\n\tThe price of tickets are given according to seats classes\n\tand quality of movie."<<endl;
cout<<"\n\tNote:Glassses for 3D movies cost $3.00."<<endl;
cout<<"\t___________________________________________________________________"<<endl;
cout<<"\t| Movie Title | Normal | Premium |"<<endl;
cout<<"\t|_____________________|_____________________|______________________|"<<endl;
cout<<"\t|1.Shouf Shouf Habibi | $5.00 | $7.00 |"<<endl;
cout<<"\t|_____________________|_____________________|______________________|"<<endl;
cout<<"\t|2.The Interview (3D) | $7.00 | $9.50 |"<<endl;
cout<<"\t|_____________________|_____________________|______________________|"<<endl;
cout<<"\t|3.New Kids Turbo (3D)| $7.00 |"<<endl;
cout<<"\t|_____________________|_____________________|"<<endl;
cout<<"\t|4.Black Butterflies | $5.00 |"<<endl;
cout<<"\t|_____________________|_____________________|"<<endl;
}
int main()
{
//constant initials
const int a=5;
const int b=7;
const double c=7;
const double d=9.5;
const int e=7;
const int glass=3;
const int f=5;
double total_tickets=0;
int choice=0;
int number=0;
char reply;
bool correct_entry=true;
string not_int;
int not_char;
while(true){
display_menu();
do {
cout<<"\n\tPlease choose from the lists (1-4) above and press enter or \n\tPress any other key to quit:";
cin>>number;
// You cannot check number for Q or q because number is int, and Q/q is char
if(cin.fail()) {
cout<<"\n\t\tThank You purchasing movie tickets with us!"<<endl;
return 0;
}
} while(number < 1 && number > 4 );
if (number==1) {
cout<<"\n\tYou have selected \"Shouf Shouf Habibi\".";
cout<<"\n\tFor Normal; Press \"N\" or for premium class; Press \"P\":";
cin>>reply;
if (reply=='N'|| reply=='n') {
cout<<"\n\tYou have selected Normal class."<<endl;
cout<<"\n\tPlease choose the number of tickets you want:";
cin>>choice;
total_tickets=shouf_normal(a,choice);
cout<<"\n\tYour total costs is=$"<<total_tickets<<endl; //calculating & printing out cost
} else if(reply=='P'||reply=='p'){
cout<<"\n\tYou have selected Premium class."<<endl;
cout<<"\n\tPlease choose the number of tickets you want:";
cin>>choice;
total_tickets=shouf_Premium (b,choice);
cout<<"\n\tYour total costs is=$"<<total_tickets<<endl; //calculating & printing out cost
}
} else if(number==2) {
cout<<"\n\tYou have selected \"The interview\"in 3D quality.";
cout<<"\n\tFor Normal; press \"N\" or for premium class; Press \"P\":";
cin>>reply;
if(reply=='N'|| reply=='n'){
cout<<"\n\tYou have selected Normal class.";
cout<<"\n\tPlease choose the number of tickets you want:";
cin>>choice;
total_tickets=interview_normal(c,choice)+(glass*choice);
cout<<"\n\t Price for one ticket is $"<<c<<endl;
cout<<"\n\tPrice for one 3D glass is $"<<glass<<endl;
cout<<"\n\t Your total costs is=$"<<total_tickets<<endl;
} else if (reply=='P'|| reply=='p') {
cout<<"\n\tYou have selected Premium class.";
cout<<"\n\tPlease choose the number of tickets you want:";
cin>>choice;
total_tickets=interview_premium(d,choice)+(glass*choice);
cout<<"\n\t Price for one ticket is $"<<d<<endl;
cout<<"\n\tPrice for one 3D glass is $"<<glass<<endl;
cout<<"\n\t Your total costs is=$"<<total_tickets<<endl;
}
} else if(number==3) {
cout<<"\n\tYou have selected \"The New Kids Turbo\" in 3D quality.";
cout<<"\n\tThis moview is available only in Normal class.";
cout<<"\n\tPlease choose the number of tickets you want:";
cin>>choice;
total_tickets=turbo_normal(e,choice)+(glass*choice);
cout<<"\n\t Price for one ticket is $"<<e<<endl;
cout<<"\n\tPrice for one 3D glass is $"<<glass<<endl;
cout<<"\n\tYour total costs is=$"<<total_tickets<<endl;
} else if(number==4) {
cout<<"\n\tYou have selected \"Black Butterflies\".";
cout<<"\n\tThis Movie is available only in Normal class.";
cout<<"\n\tPlease choose the number of tickets you want:";
cin>>choice;
total_tickets=butterflies_normal(f,choice);
cout<<"\n\tYour total costs is=$"<<total_tickets<<endl;
}
// Wait for user to enter some key so that they can view their bill.
cout<<endl<<"Press any key to continue....";
cin.ignore();
cin.get();
}
return 0;
}