2017-05-14 11 views
-3

関数を使用して入力検証を書く方法については、ここで本当に助けが必要でした。私はループ検証中に知っていますが、この場合は入力を検証する関数を書く方法を知りたかったので、誤った入力を処理するために入力から関数を呼び出すことができます。私のコードで関数を使用して入力を検証する

、私は、ユーザーの入力や文字から整数を受け入れる:

# 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; 
} 
+1

(https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)[小さなプログラムのデバッグ方法]お読みください。 SOは、コード全体をダンプして誰かにあなたのためにそれを修正させるサービスではありません。 [mcve]の問題に関する特定の質問がある場合は、質問してください。 – StoryTeller

+0

私は私のコード全体を置くことは、コードが何であるかを私たち全員が理解する助けになると思います。 – Surdz

+0

あなたは間違っていると思います。ヘルプセンターには、[あなたが尋ねることができる](http://stackoverflow.com/help/on-topic)、[尋ねることができない](http://stackoverflow.com/help)の情報が満載です。/dont-ask)、そして[良い質問をする]方法(http://stackoverflow.com/help/how-to-ask)を参照してください。最後のリンクは品質が低いため、特にあなたの質問に関係しています。 – StoryTeller

答えて

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; 
} 
関連する問題