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 に答える