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

时间:2017-11-15 03:47:40

标签: c++ arrays while-loop break

我不明白为什么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;
        }
  }

1 个答案:

答案 0 :(得分:1)

在OP将标题更新为关于“while循环和中断语句”的原始问题之前,我开始研究这个问题。然而,当我遇到这个问题时,OP最初删除了while循环。我正在查看提供的函数,以了解OP正在尝试做什么,这就是我想出的。

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

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

  •   
  • 最后:我通过更改或删除不必要的functionsparameter(s)&amp;来对OP的现有return type(s)做了一些修改。 code duplication。我删除了不再需要的function。我更改了消息的output formatting以显示干净的程序。我还删除了在全局范围内使用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;
    }
}