-4

Only problems remaining now are that my choice while loop is infinite since the break statements dont seem to be breaking out of the loop at all, so the program doesn't read the answer loop. Also, "Invalid Entry" displays every other incorrect input instead of displaying everytime an invalid character is entered

#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 = 0;
int age = 0;
char choice = 0;

while (choice = getChoice())
{
    if (toupper(choice) == 'O')
    {
        displayInOrder(numbers, SIZE, choice);
        break;
    }
    else if (toupper(choice) == 'R')
    {
        displayInReverse(numbers, SIZE, choice);
        break;
    }
    else
    {
        cout << "Invalid entry! - Must be O or R\n\n";
        break;
    }
}

while (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;

  if (toupper(answer) == 'N')
    {
        exit();
    }
}
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 Answers1

0

1."Invalid Entry" displays every other incorrect input: you have a getChoice() call inside the while loop, here:

else
{
    cout << "Invalid entry! - Must be O or R\n\n";
    choice = getChoice();
}

which is followed by a getChoice() call in the

while (choice = getChoice())

Hence the previous getChoice() is not processed.

2."I'm not sure how to close the program if the user enters N for answer", You should think about if you can ever reach the second while loop and where you are setting the answer variable or basically where you are taking the user input for ending the program? You should see if it can it be taken care of in the first while loop ?

tinus91
  • 247
  • 1
  • 6
  • 22
  • Easily the most effort put into helping me so far and I thank you for that. Removing `choice = get Choice();` in the else statement didn't change anything and for the second part are you saying try to put both while loops together inside main or try to put the if statement into the `answer` loop ? – Spencer Cumbie Nov 14 '17 at 23:54
  • First of all, removing `choice = get Choice();` in the else statement actually does take care of the alternating invalid type message. check: https://ideone.com/wt4Z86 – tinus91 Nov 15 '17 at 00:00
  • For the second part, you should check out how while loops work, your first loop is a never-ending loop. You should think about how to exit from that loop, at least. – tinus91 Nov 15 '17 at 00:06
  • Hmm, I believe you that it works but the invalid message still alternates. I even tried copy pasting the code from that link without any success, The break statements don't seem to be working either :( – Spencer Cumbie Nov 15 '17 at 00:31
  • Updated code with breaks and 2nd `choice = getChoice();` removed – Spencer Cumbie Nov 15 '17 at 00:56
  • For this code `while (toupper(answer) == 'Y')` where are you setting the answer variable/ where are you asking the user to enter the `answer` (Y/N). You should do something like `getAnswer()` similar to `getChoice()` to get user input for answer from answer. – tinus91 Nov 15 '17 at 08:23