This is what i have, but when i run the program the only thing that comes out is the inro line "This program opens...". What do i have to do for it to run smoothly.The program is supposed to sort the data either in ascending or descending order depending on the user’s choice. Finally, the sorted data is stored in a data file as well as shown on the screen.
this is what i have so far:
this is what i have so far:
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// Function declaration
void intro ();
void getData (string name[], int& item);
void ascending (string name[], int item);
void descending (string name[], int item);
void output (string name[], int item);
int getChoice();
// Main Function
int main()
{
string name[100], temp;
int item, choice;
intro ();
getData(name, item);
choice = getChoice();
if (choice == '1'|| choice == '1')
ascending(name, item);
if (choice == '2'|| choice == '2')
descending(name, item);
output(name, item);
}
//Intro to the program
void intro ()
{
cout << "This program opens a string data file, reads the data from that file" <<endl;
cout <<"and assigns them to an array" <<endl<<endl;
}
// Input Choice
int getChoice()
{
int choice;
cout << "Please enter 1 for ascending." <<endl;
cout << "or 2 for descending." <<endl<<endl;
cin >> choice;
return choice;
}
// Get Data
void getData(string name[], int& item)
{
ifstream fin;
fin.open("E:\\p9.txt");
item = 0;
while (!fin.eof())
{
fin >> name[item];
item++;
}
cout << "item = " << item << endl << endl;
}
// Ascending
void ascending(string name[], int item)
{
string temp;
for(int j=0; j<item-1; j++)
{
for(int i=0; i<item-1; i++)
if(name[i] > name[i+1])
{
temp = name[i];
name[i] = name[i+1];
name[i+1] = temp;
}
}
}
// Descending
void descending(string name[], int item)
{
string temp;
for(int j=0; j<item-1; j++)
{
for(int i=0; i<item-1; i++)
if(name[i] < name[i+1])
{
temp = name[i];
name[i] = name[i+1];
name[i+1] = temp;
}
}
}
// Output
void output(string name[], int item)
{
for(int i=0; i<item; i++)
cout << name[i] << endl;
cout << endl << endl;
}
Comment