只允许在程序中使用选择的字母(字符串)?

时间:2012-12-10 09:00:39

标签: c++ validation

我的程序是一个成绩计算器,如果您输入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;
        }
     }
 }

1 个答案:

答案 0 :(得分: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];
    }
}