【问题标题】:Why does my program skip the first function? (Beginner C++)为什么我的程序会跳过第一个函数? (初级 C++)
【发布时间】:2017-11-15 03:47:40
【问题描述】:

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

【问题讨论】:

  • "程序直接进入选择函数,"你的意思是getChoice吗?这是因为它在 while 语句中被调用。
  • 如果我希望在 getChoice 中输入的选项根据输入执行 2 个单独的功能,我应该只为 'O' 和 'R' 做 2 个 while 循环而不是嵌套 if 吗?跨度>
  • 放弃while循环并使用ifswitch语句。
  • 更新了没有while循环的帖子,但它仍然跳过getAges并直接转到getChoice
  • 如何分配给answer

标签: c++ arrays while-loop break


【解决方案1】:

在 OP 将标题更新为他们关于“while 循环和中断语句”的原始问题之前,我就开始着手解决这个问题。但是,当我遇到这个问题时,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; 的不良做法。

我这样做是为了向 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;
    }
}

【讨论】:

  • 找不到更好的解释,非常感谢
  • @dribs 完全没问题。当我刚接触编程时,我必须在很少的帮助下艰难地学习,所以我不介意帮助那些刚接触编程的人,尽我所能。
  • 你这么说很有趣,我觉得这里的很多人都忘记了新人的感觉。几乎每次我在堆栈溢出上发帖时,我都会被那些有能力帮助我但没有帮助的人投反对票并在 cmets 中大肆攻击。我的意思是我在计算机科学课上得了 B+,但他们喜欢让我觉得自己很愚蠢。不管怎样,我淹死了,你是我的救生筏。在审查了您的代码一段时间后,这是非常有意义的,即使我整天都在怀疑自己,但我已经接近让它工作了。你不仅修复了一个程序,而且还让我对编程充满信心。再次感谢。
  • @dribs 我没有受过正规教育,没有学位,都是自学的,我的研究领域是 3D 图形编程、物理模拟、动画等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 2015-10-08
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多