错误地放置return语句和character-int不匹配校对

时间:2015-10-27 05:45:06

标签: c++

我需要两部分的帮助。该程序的要点是它创建一个菜单并采用L,P或Q的字符输入。" L"启动一个函数,确定给定年份是否为闰年。 " P"启动一个函数,确定给定的输入是否为素数。 " Q"退出程序。该计划的目的是测试我们调用函数的能力。

  • 第一个问题是我无法确定将返回语句放在何处。将它们放在每个函数的末尾以便返回主菜单似乎是合适的,但这不起作用。这些函数只是保持循环。

  • 第二个问题是,只要在leapYear函数或checkPrime函数中输入字符,就会出现无限循环。例如:进入','或者' o' o到提示创建一个无限循环的错误消息。是否有办法使我能够防止非数字输入以避免这种情况?

  • 我为格式化道歉。我烦躁不安,试着让它也起作用。猜猜这不是我的一天。

// Project 12B

include <iostream>
include <string>
using namespace std;

 void displayMenu();
 void checkLeapYear();
 void checkPrime();
 char option = 'd';

// Beginning of the main function
int main(int argc, const char * argv[]) {

    // User input dialog
    cout << "Hello! This program provides two different services. "
        << "Entering the character 'L' will initiate a program that can determine if a"
        << " given year is a leap year.\n"
        << " Entering the character 'P' will initiate a program that will determine if a"
        << " given number is prime or not.\n "
        << "Lastly, entering 'Q' will quit the main program.\n\n "
        << "Please enter one of the following characters: L, P, or Q.\n\n";

    cin >> option;


    while (option != 'Q' || option != 'q'){
        displayMenu();
    }

    return 0;

   }


// End of main function




// The displayMenu function

void displayMenu(){

    // Switch statement for menu purposes
    switch (option) {

    case 'p':
    case 'P':
        checkPrime();
        break;

    case 'l':
    case 'L':
        checkLeapYear();
        break;

    case 'q':
    case 'Q':
        cout << "Have a nice day!";
        break;

    default:
        cout << "ERROR: You have entered a value beyond the specified range. Please limit your entries to either"
            << " L,P, or Q. \n\n";
        cin >> option;
        break;
    }
    return;
    }

// The checkLeapYear function

void checkLeapYear(){

    // Declaration of variables for checkLeapYear
    int lOptionYear;

    // User Dialog

    cout << "Hello! This part of the program determines if the year you entered is a leap year."
        << "Please enter a year.\n\n";

    cin >> lOptionYear;

    // Verification that the year entered is actually a valid year

    if (lOptionYear <= 0) {
        cout << "ERROR: You have entered an invalid year.\n\n";
    }

    // Actual leap year verification.
    if ((lOptionYear % 4 == 0 && !(lOptionYear % 100 == 0)) || lOptionYear % 400 == 0) {
        cout << "The year you have entered is a leap year. \n";
    }

    else {
        cout << "The year you have entered is not a leap year\n";
    }
    return;
   }

// The checkPrime function

void checkPrime(){

    // Declaration of variables for checkPrime
    int uNumber,
        factorCount = 0;

    // User dialog
    cout << "Please enter a natural number. The output will be the factors (if it is nonprime) of your input, and the "
        << "total count of factors (if they exist).\n\n";
    cin >> uNumber;

    // Guard against bad input
    if (uNumber <= 0) {
        cout << "You have entered a non-positive number. Please enter a positive, natural number \n\n";
    }

    // The loop starts at two and continues for as long as the iteration-variable is less than or equal to the input number
    for (int k = 2; k < uNumber; k++) {
        if (uNumber % k == 0) {
            cout << k << " ";
            factorCount++;
        }

    }
    // Statement telling total number of factors found
    if (factorCount > 0){
        cout << "\nThere were " << factorCount << " factors found. \n";
    }
    if (factorCount == 0){
        cout << "\nNOTE: The number you entered is prime. \n";
    }
    return;
    }

1 个答案:

答案 0 :(得分:0)

问题1 :无论如何都会返回void函数,因此除非您想在中途终止函数,否则无需放置return或担心它们。

问题2

当cin遇到输入时,它无法正确读入指定的变量(例如将字符输入到整数变量中),它会进入错误状态并将输入留在缓冲区中。

您必须做好几件事才能正确处理这种情况。

  1. 您必须测试此错误状态。
  2. 您必须清除错误状态。
  3. 您必须或者处理生成错误状态的输入数据,或者将其刷新并重新提示用户。
  4. 因此,在功能checkleapyear中,将cin >> lOptionYear;替换为

    while(!(cin >> lOptionYear)){   //test for error state
            cin.clear();            //clear the error state
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid input.  Try again: ";    //repromt user
        }
    

    注意:别忘了添加<limits>

    问题3

    在main()中添加 while(option != 'Q' || option != 'q')是不够的,因为输入可能不是lpq。取代在main中输入cin>>option,将其带入displayMenu(),并在默认情况下递归调用它:

    void displayMenu(){
    
        cin >> option;   // added here
        // Switch statement for menu purposes
        switch (option) {
    
        case 'p':
        case 'P':
            checkPrime();
            break;
    
        case 'l':
        case 'L':
            checkLeapYear();
            break;
    
        case 'q':
        case 'Q':
            cout << "Have a nice day!";
            break;
    
        default:
            cout << "ERROR: You have entered a value beyond the specified range. Please limit your entries to either"
                << " L,P, or Q. \n\n";
            //cin >> option;
            displayMenu();   //recursive call
            //break;
        }
        return;
        }
    

    注意:从main移除while(),然后只需调用displayMenu()