i am supposed to create a program in c++ that would
a) Prompt the user to input a string of characters up to a maximum of 40 characters
b) Read the string input from the keyboard
c) Output the sequence of characters in alphabetical order
d) Prompt the user to repeat the process if desired
i have created program and tried it on c++ but there is error saying eh.h is not in c++.
here is the code:
Code:
#include <iostream>
#include <string>
using namespace std;
const int MAXSIZE = 9;
char* names[MAXSIZE] = { "a", "c", "d", "f", "i", "b", "e", "g", "h" };
char* tmp;
void start();
void decSort(char* arr[], int size);
void ascSort(char* arr[], int size);
void runAgain();
int main()
{
start();
return 0;
}
void start()
{
int k, choice;
cout << "Press 1 to sort in decending order: \n";
cout << "Press 2 to sort in ascending order: \n";
cin >> choice;
if(choice == 1)
{
cout << "------------\n";
cout << "Before Sort: \n";
cout << "------------\n";
for(k = 0;k < MAXSIZE;k++)
cout << names[k] << "\n";
decSort(names, MAXSIZE);
cout << "------------\n";
cout << "After Sort: \n";
cout << "------------\n";
for(k = 0;k < MAXSIZE;k++)
cout << names[k] << "\n";
runAgain();
}
else if(choice == 2)
{
cout << "------------\n";
cout << "\nBefore Sort: \n";
cout << "------------\n";
for(k = 0;k < MAXSIZE;k++)
cout << names[k] << "\n";
ascSort(names, MAXSIZE);
cout << "------------\n";
cout << "\nAfter Sort: \n";
cout << "------------\n";
for(k = 0;k < MAXSIZE;k++)
cout << names[k] << "\n";
runAgain();
}
}
void decSort(char* arr[], int size)
{
for(int i = 0; i < size - 1;i++)
for(int j = 0; j < size - 1 - i;j++)
{
if(strcmp(arr[j], arr[j + 1]) < 0)
{
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
void ascSort(char* arr[], int size)
{
for(int i = 0; i < size - 1;i++)
for(int j = 0; j < size - 1 - i;j++)
{
if(strcmp(arr[j], arr[j + 1]) > 0)
{
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
PLs can anyone find the error or help me out?????!!!!!!!!!