String Array problem
Feb 22, 2016 at 10:49pm UTC
So I have two issues I cant seem to figure out the first one is when I run my program once it works fine but when they click "Y" to run it again it only lets them input 3 instead of the original 4. I know it involves using cin.ignore() but where does it go? also im having issues displaying the array in reverse order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int SIZE = 4;
void getCities(string cities[], int SIZE);
char getChoice();
void displayInOrder(string cities[], int SIZE);
void displayInReverse(string cities[], int SIZE);
int main()
{
string cities[SIZE] ;
char selection;
char answer;
do {
getCities(cities, SIZE);
selection = getChoice();
if (toupper(selection) == 'O' )
{
displayInOrder(cities, SIZE);
}
else if (toupper(selection) == 'R' )
{
displayInReverse(cities, SIZE);
}
else
{
cout << "Invalid selection - Must choose O or R" ;
}
cout << endl << endl << "Do you want to do this again? (Y/N): " ;
cin >> answer;
} while (toupper(answer) == 'Y' );
return 0;
}
void getCities(string cities[], int SIZE)
{
cout << "Enter 4 Cities:\n" ;
for (int i = 0; i < SIZE; i++)
{
getline(cin, cities[i]);
}
}
char getChoice()
{
char choice;
cout << "How do you want the cities displayed?\n" ;
cout << "\nEnter \"O\" for in order, or enter \"R\" for in reverse order: " ;
cin >> choice;
return choice;
}
void displayInOrder(string cities[], int SIZE)
{
for (int i = 0; i < SIZE; i++)
{
cout << endl << cities[i];
}
}
void displayInReverse(string cities[], int SIZE)
{
for (int i = 4; i > SIZE; i--)
{
cout << endl << cities[i];
}
}
Last edited on Feb 22, 2016 at 10:49pm UTC
Topic archived. No new replies allowed.