It basically prints out the user prompt, then after you enter the number of names, such as 3... it prints out:
Enter a name: Enter a name:
Enter a name:
instead of
Enter a name:
Enter a name:
Enter a name:
After some checking it's not the function that reads in the names to the array that is faulty. The error seems to be in the function to display the array and I can't figure it out for the life of me.
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <cstring>
- using namespace std;
- //Constants
- const int MaxNames = 21;
- const int MaxChars = 16;
- //Variables
- char name[MaxChars];
- char Array[MaxNames][MaxChars];
- int i, j, pass;
- //Functions
- void ReadNames(char Array[MaxNames][MaxChars], int pass);
- void ShowArray(char Array[MaxNames][MaxChars]);
- void SortArray(char Array[MaxNames][MaxChars], int pass);
- void main()
- {
- //User prompt
- cout << "-------------------------------------------------------------------------------" << endl;
- cout << "This program will allow you to enter names and sort them in alphabetical order." << endl;
- cout << "-------------------------------------------------------------------------------" << endl;
- cout << "First, how many sets of names you will enter? (Must be less than 20): ";
- cin >> pass;
- if (pass==0)
- {
- cout << "ERROR: You have entered 0 or a character. Try again." << endl;
- //do something, break a loop?
- }
- while (pass < 0 || pass > 20)
- {
- cout << "ERROR: You cannot enter less than 1 or more than 20 names. Try again: ";
- cin >> pass;
- }
- ReadNames(Array, pass);
- //Display unsorted array
- cout << "UNSORTED:" << endl;
- ShowArray(Array);
- //SortArray
- SortArray(Array, pass);
- //Display sorted array
- cout << endl << "SORTED:" << endl;
- ShowArray(Array);
- }
- void ReadNames (char Array[MaxNames][MaxChars], int pass)
- {
- for (i=0; i<pass; i++)
- {
- cout << "Enter a name: ";
- cin.getline(name, MaxChars);
- for (j=0; j<MaxChars; j++)
- {
- Array[i][j]=name[j];
- }
- }
- }
- void ShowArray (char Array[MaxNames][MaxChars])
- {
- for (i=0; i<MaxNames; i++)
- {
- for (j=0; j<MaxChars; j++)
- {
- cout << Array[i][j];
- }
- cout << endl;
- }
- }
- void SortArray (char Array[MaxNames][MaxChars], int pass)
- {
- bool swap;
- int temp[MaxChars];
- do
- {
- swap=false;
- for (i=0; i<MaxNames-1; i++)
- {
- if (Array[i][0]>Array[i+1][0])
- {
- for (j=0; j<MaxChars; j++)
- {
- temp[j]=Array[i][j];
- Array[i][j]=Array[i+1][j];
- Array[i+1][j]=temp[j];
- swap=true;
- }
- }
- }
- } while(swap);
- }