C ++ While语句在第一次之后不会停止重复

时间:2015-09-25 01:51:11

标签: c++ loops while-loop

我正在尝试为编程类编写代码,并且由于某种原因我发现如果您对该问题的第一个响应是“n”,它将不会重复。否则,无论你输入什么,它都会继续重复。

while (again == 'y')
{
cout << endl << endl;

//Header Art
cout << "     .\'(     .-./(     )\\.-.        .\'(     /`-.      /`-.  .-,.-.,-.   )\\.--.  \n";
cout << " ,\') \\  )  ,\'     )  ,\' ,-,_)   ,\') \\  )  ,\' _  \\   ,\' _  \\ ) ,, ,. (  (   ._.\' \n";
cout << "(  \'-\' (  (  .-, (  (  .   __  (  /(/ /  (  \'-\' (  (  \'-\' ( \\( |(  )/   `-.`.   \n";
cout << " ) .-.  )  ) \'._\\ )  ) \'._\\ _)  )    (    )   _  )  ) ,_ .\'    ) \\     ,_ (  \\  \n";
cout << "(  ,  ) \\ (  ,   (  (  ,   (   (  .\'\\ \\  (  ,\' ) \\ (  \' ) \\    \\ (    (  \'.)  ) \n";
cout << " )/    )/  )/ ._.\'   )/\'._.\'    )/   )/   )/    )/  )/   )/     )/     \'._,_.\'  \n";

cout << endl;

 //Variables
 int TuitionBill, BoardBill, TotalBill;

 char tuition, board, again;

 cout << "Welcome to Hogwarts School of Witchcraft and Wizardry!\n\n";

 cout << "Please input 'I' if you are in-country or 'O' if you are out of country\n";
 cin >> tuition;

 cout << endl << endl;

 cout << "Please input 'Y' if you will be staying in one of the four houses\n and 'N' if you will be hiding out in the Shrieking Shack.\n";
 cin >> board;

 cout << endl << endl;

 switch (tuition)
 {
     case 'I':
     TuitionBill = 3000;
     if (board == 'Y')
     BoardBill = 2500;
     else if (board == 'N')
     BoardBill = 0;
    TotalBill = TuitionBill + BoardBill;

    cout << "Your total bill for the semester is $" << TotalBill;

    break;

    case 'O':

     TuitionBill = 4500;
     if (board == 'Y')
     BoardBill = 3500;
     else if (board == 'N');
     BoardBill = 0;
    TotalBill = TuitionBill + BoardBill;

    cout << "Your total bill for the semester is $" << TotalBill;

    break;

    default:
    cout << "That answer is invalid. Please input only an 'I' or an 'O.'";
 }

 cout << endl << endl;

 cout << "Would you like to run the program again? (y/n)\n";
 cin >> again;

如果有人可以提供帮助,我们将非常感激。

2 个答案:

答案 0 :(得分:1)

请删除循环中cperl-tips的定义 它可以防止在mode-compile.el条件下评估的again被更新。

答案 1 :(得分:-1)

#include<stdio.h>
#include<iostream>
#include<string>


using namespace std;



int main() {
    char again = 'y';

    while (again == 'y')
    {
        cout << endl << endl;

        //Header Art
        cout << "     .\'(     .-./(     )\\.-.        .\'(     /`-.      /`-.  .-,.-.,-.   )\\.--.  \n";
        cout << " ,\') \\  )  ,\'     )  ,\' ,-,_)   ,\') \\  )  ,\' _  \\   ,\' _  \\ ) ,, ,. (  (   ._.\' \n";
        cout << "(  \'-\' (  (  .-, (  (  .   __  (  /(/ /  (  \'-\' (  (  \'-\' ( \\( |(  )/   `-.`.   \n";
        cout << " ) .-.  )  ) \'._\\ )  ) \'._\\ _)  )    (    )   _  )  ) ,_ .\'    ) \\     ,_ (  \\  \n";
        cout << "(  ,  ) \\ (  ,   (  (  ,   (   (  .\'\\ \\  (  ,\' ) \\ (  \' ) \\    \\ (    (  \'.)  ) \n";
        cout << " )/    )/  )/ ._.\'   )/\'._.\'    )/   )/   )/    )/  )/   )/     )/     \'._,_.\'  \n";

        cout << endl;

         //Variables
         int TuitionBill, BoardBill, TotalBill;

         char tuition, board;

         cout << "Welcome to Hogwarts School of Witchcraft and Wizardry!\n\n";

         cout << "Please input 'I' if you are in-country or 'O' if you are out of country\n";
         cin >> tuition;

         cout << endl << endl;

         cout << "Please input 'Y' if you will be staying in one of the four houses\n and 'N' if you will be hiding out in the Shrieking Shack.\n";
         cin >> board;

         cout << endl << endl;

         switch (tuition)
         {
             case 'i':
             case 'I':
                 TuitionBill = 3000;
                 if (board == 'y' || board == 'y')
                 BoardBill = 2500;

                 else if (board == 'N')
                     BoardBill = 0;

                 TotalBill = TuitionBill + BoardBill;
                 cout << "Your total bill for the semester is $" << TotalBill;
                break;

            case 'O':
            case 'o'://just in case.
             TuitionBill = 4500;
             if (board == 'Y')
                 BoardBill = 3500;
             else if (board == 'N');
                 BoardBill = 0;

            TotalBill = TuitionBill + BoardBill;
            cout << "Your total bill for the semester is $" << TotalBill;

            break;

            default:
            cout << "That answer is invalid. Please input only an 'I' or an 'O.'";
            break;
         }

         cout << endl << endl;

         cout << "Would you like to run the program again? (y/n)\n";
         cin >> again;
         if(again == 'n'|| again == 'N'){break;}

             cout << "again:  " << again << endl;
    }
    cout << "\nBYEEE";
    return 0;
}