繁体   English   中英

为什么我的程序跳过第一个功能? (初学者C ++)

[英]Why does my program skip the first function? (Beginner C++)

我不明白为什么getAges被跳过了。

    #include <iostream>
    #include <cctype>
    using namespace std;

    int getAges(int age, const int SIZE);
    char getChoice();
    void displayInOrder(int numbers[], const int SIZE, char choice);
    void displayInReverse(int numbers[], const int SIZE, char choice);

    int main()
    {
        const int SIZE = 5;
        int numbers[SIZE] = { 1, 2 ,3 ,4, 5 };
        char answer;
        int age;
        char choice;


            if (toupper(choice) == 'O')
        {
            displayInOrder(numbers, SIZE, choice);

        }
        else if (toupper(choice) == 'R')
        {
            displayInReverse(numbers, SIZE, choice);

        }
        else
        {
            cout << "Invalid entry! - Must be O or R\n\n";

        }
        if (toupper(answer) == 'Y')
        {
            system("cls");

            age = getAges(age, SIZE);
            choice = getChoice();
            displayInOrder(numbers, SIZE, choice);
            displayInReverse(numbers, SIZE, choice);

            cout << "Run program again (Y or N)?  ";
            cin >> answer;
            break;
        }
        else if (toupper(answer) == 'N')
        {
            return 0;
        }
        return 0;
    }

    int getAges(int age, const int SIZE)
    {
        cout << "Enter " << SIZE << " ages: \n\n";
        cin >> age;
        cout << endl;
        cin >> age;
        cout << endl;
        cin >> age;
        cout << endl;
        cin >> age;
        cout << endl;
        cin >> age;
        cout << endl;
        return age;
    }

    char getChoice()
    {
        char choice;
        cout << "How do you want to see the ages displayed? \n\n Enter O for In Order, or R for In Reverse.\n\n";
        cin >> choice;

        return choice;

    }

    void displayInOrder(int numbers[], const int SIZE, char answer)
    {
        cout << "Here are the ages in order: \n\n";
        for (int i = 0; i < SIZE; i++)
        {
            cout << numbers[i] << endl;
        }
    }

    void displayInReverse(int numbers[], const int SIZE, char answer)
    {
        cout << "Here are the ages in reverse order: \n\n";
        for (int i = SIZE - 1; i >= 0; i--)
        {
            cout << numbers[i] << endl;
        }
  }

在OP将标题更新为关于“ while循环和break语句”的原始问题之前,我开始进行此工作。 但是,在我遇到这个问题时,OP最初删除了while循环。 我正在查看提供的功能,以了解OP试图做的事情,这就是我想出的。

  • 首先: while loops正是您想要的,但是您需要特定类型的while loop ,在这种情况下为do-while loop

  • 下一步:如果您知道如何正确构造do-while loop ,则无需break语句。

  • 最后:通过更改或删除不必要的parameter(s)return type(s)code duplication ,我对OP的现有functions进行了一些修改。 我删除了不再需要的function 我更改了消息的output formatting以显示干净的程序。 我还消除了using namespace std;的错误做法using namespace std; 在全球范围内。

我这样做是为了向OP演示如何在无需break语句的情况下构造while循环,它们本来是正确的轨道,但需要一点帮助才能上路。

这是关于我认为OP打算执行的工作程序的源代码。

#include <iostream>
#include <cctype>

void getAge( int& age );
void displayInOrder( int numbers[], const int SIZE);
void displayInReverse( int numbers[], const int SIZE );

int main() {
    const int SIZE = 5;
    int numbers[SIZE] = { 0 };
    char answer = '\0';
    int age = 0;
    char choice = '\0';

    std::cout << "========================================================================\n"
              << "This program will have the user enter in "
              << SIZE
              << " Ages. \nThen ask the user in which order to display the list of ages.\n"
              << "Finally the program will ask the user if they want to continue or not.\n"
              << "========================================================================\n\n";

    do {

        for ( int i = 0; i < SIZE; i++ ) {
            getAge( age );
            numbers[i] = age;
        }

        std::cout << "\nPlease enter 'O' for ordered or 'R' for reversed list.\n";
        std::cin >> choice;

        if ( toupper( choice ) == 'O' ) {
            displayInOrder( numbers, SIZE );
        }

        if ( toupper( choice ) == 'R' ) {
            displayInReverse( numbers, SIZE );
        }

        std::cout << "\nDo you want to run program again (Y/N)?";
        std::cin >> answer;

    } while ( toupper( answer ) == 'Y' );

    return 0;
}

void getAge( int& age ) {
    int temp;
    std::cout << "Enter an age: \n";
    std::cin >> temp;
    age = temp;
}

void displayInOrder( int numbers[], const int SIZE ) {
    std::cout << "\nHere are the ages in order: \n";
    for ( int i = 0; i < SIZE; i++ ) {
        std::cout << numbers[i] << std::endl;
    }
}

void displayInReverse( int numbers[], const int SIZE ) {
    std::cout << "\nHere are the ages in reverse order: \n";
    for ( int i = SIZE - 1; i >= 0; i-- ) {
        std::cout << numbers[i] << std::endl;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM