2

我的程序是一个成绩计算器,程序会根据您键入 P、M 或 D 来加分。我想警告用户,这些是您可以使用的唯一字符串。下面是程序的代码:

 #include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int points = 0;

string lesson[18];  
char grades[18];    

void start();
void lessons();  
void grades_selector();
void award_points();
void point();
void BTEC();
void UCAS();

int main(int argc, char *argv[])
{
    start();
    lessons();
    grades_selector();
    point();
    cout << "You have achieved " << points << " points." << endl;
    BTEC();
    UCAS();

    system("PAUSE");
    return EXIT_SUCCESS;
}

void start()
{
     cout << "BTEC & UCAS GRADE CALCULATOR." << endl;
     cout << "For every lesson input either (P)ass, (M)erit, (D)istinction." << endl;
}

void lessons()
{
     lesson[0] = "Year 1:Communication and Employability Skills";
     lesson[1] = "Year 1:Computer Systems";
     lesson[2] = "Year 1:Information Systems";
     lesson[3] = "Year 1:Event Driven Programming";
     lesson[4] = "Year 1:Database Design";
     lesson[5] = "Year 1:Data Analysis and Design";
     lesson[6] = "Year 1:Installing and upgrading Software";
     lesson[7] = "Year 1:Digital Graphics for Interactive Media";
     lesson[8] = "Year 1:CCNA Network Home Small Business";
     lesson[9] = "Year 2:Software Design and Development";
     lesson[10] = "Year 2:Object Oriented Programming";
     lesson[11] = "Year 2:Procedural Programming";
     lesson[12] = "Year 2:Client Side Customisation of Web Pages";
     lesson[13] = "Year 2:Developing Computer Games";
     lesson[14] = "Year 2:Human Computer Interaction";
     lesson[15] = "Year 2:Computer Animation";
     lesson[16] = "Year 2:Computer Games Platforms and technologies";
     lesson[17] = "Year 2:3D Modelling";
}

void grades_selector()
{
     int index; //local variable

     for (index = 0; index < 18; index = index + 1)
     {
         cout << "What grade was achieved for " << lesson[index] << "?" << endl;
         cin >> grades[index];
         } }

void point()
{
int index;
         for (index = 0; index < 18; index = index + 1)
         {

             switch ( grades[index] )
             {
              case 'D':
                   points = points + 90;
                   break;
              case 'd':
                   points = points + 90;
                   break;
              case 'M':
                   points = points + 80;
                   break;
              case 'm':
                   points = points + 80;
                   break;
              case 'P':
                   points = points + 70;
                   break;
              case 'p':
                   points = points + 70; 
                   break;
              default:
                      points = 0;
                      break;
         }
     }
}

void BTEC()
{
                 if( points >= 1590)
                 {
                     cout <<" You have achieved D*D*D*" << endl;
                     }
                 else if( points >= 1560)
                 {
                     cout <<" You have achieved D*D*D" << endl;
                     }
                 else if( points >= 1530)
                 {
                     cout <<" You have achieved D*DD" << endl;
                     }
                 else if( points >= 1500)
                 {
                     cout <<" You have achieved DDD" << endl;
                     }
                 else if( points >= 1460)
                 {
                     cout <<" You have achieved DDM" << endl;
                     }
                 else if( points >= 1420)
                 {
                     cout <<" You have achieved DMM" << endl;
                     }
                 else if( points >= 1380)
                 {
                     cout <<" You have achieved MMM" << endl;
                     }
                 else if( points >= 1340)
                 {
                     cout <<" You have achieved PMM" << endl;
                     }
                 else if( points >= 1300)
                 {
                     cout <<" You have achieved PPM" << endl;
                     }
                 else
                 {
                     cout <<" You have achieved PPP" << endl;
                     }
}

void UCAS()
{
                 if( points >= 1590)
                 {
                     cout <<" You have 420 UCAS points." << endl;
                     }
                 else if( points >= 1560)
                 {
                     cout <<" You have 400 UCAS points." << endl;
                     }
                 else if( points >= 1530)
                 {
                     cout <<" You have 380 UCAS points" << endl;
                     }
                 else if( points >= 1500)
                 {
                     cout <<" You have 360 UCAS points." << endl;
                     }
                 else if( points >= 1460)
                 {
                     cout <<" You have 320 UCAS points." << endl;
                     }
                 else if( points >= 1420)
                 {
                     cout <<" You have 280 UCAS points." << endl;
                     }
                 else if( points >= 1380)
                 {
                     cout <<" You have 240 UCAS points." << endl;
                     }
                 else if( points >= 1340)
                 {
                     cout <<" You have 200 UCAS Points." << endl;
                 }
                 else if ( points >= 1300)
                {
                    cout <<" You have 160 UCAS Points." << endl;
                }
}

我希望能够提示用户,如果他们输入除了 P、p、M、m、D 或 d 之外的任何内容,那么程序会警告他们。

我在stackoverflow上注意到了这一点,并找到了一个接近的解决方案:

char c;

while (std::getline(std::cin, line))
{
    // Iterate through the string one letter at a time.
    for (int i = 0; i < line.length(); i++) {

        c = line.at(i);         // Get a char from string

        // if it's NOT within these bounds, then it's not a character
        if (! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) ) {

             std::cout << "Error!" << std::endl;

             // you can probably just return here as soon as you
             // find a non-letter char, but it's up to you to
             // decide how you want to handle it exactly
             return 1;
        }
     }
 }
4

1 回答 1

1

添加此功能:

bool isValidInput (char input) {
    switch (input) {
        case 'm':
        case 'M':
        case 'p':
        case 'P':
        case 'd':
        case 'D':
            return true;
        default:
            return false;
    }
}

然后,要求输入:

for (index = 0; index < 18; index = index + 1)
{
    cout << "What grade was achieved for " << lesson[index] << "?" << endl;
    cin >> grades[index];
    while (! isValidInput(grades[index])
    {
        cout << "Only m, p and d are valid inputs!";
        cin >> grades[index];
    }
} 
于 2012-12-10T09:09:58.663 回答