I can not get this horrible code to this is the question as follows:
Write a program to declare a structure consisting of an integer member and a float member, and declare an array of the structure with space for at least 10 elements. The program should then open the text file named "CSC2144N.TXT" (that you created above) for input, then read all the numbers from the file into the structure array and then display the following menu to prompt the user to display the number pairs in one of three possible orders:
1. display pairs unsorted 2. display pairs sorted in ascending order of the integer values 3. display pairs sorted in ascending order of the float values 4. exit program
Note: the program should work for any number of integer float pairs that might be in the data file, not just 5 pairs.
The program should use an adaptation of the exchange sort procedure to perform the sorts. After displaying the values in the desired order, the user should be prompted again with the menu.
Note: you should probably have a duplicate array to keep the unsorted values in case the user enters 2 or 3 and then enters 1. Alternatively, you could simply re-read the file.
in the text file is:
10 7.35 -21 45.9 3 -4.56 85 34.1 -9 -32.7
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 99
|
#include <iostream>
#include <fstream>
using namespace std;
struct info //declaring my data stucture
{
int member1; //declaring a variables within the data stucture
float member2;
}entry[10]; //declaring an array of the data structure with space for 10 elements
int main ()
{
int count;
int count2;
int selection=0;
info temp;
fstream myfile;//naming my file
myfile.open("CSC2144N.txt");//linking file name to .txt doc.
for(count=0;count<10;count++)//for loop to cycle through up to 10 entries
{
myfile>>entry[count].member1;
myfile>>entry[count].member2;
if(myfile.eof())//if end of file is reached the file is closed.
{
count++;break;
}
}
myfile.close();
while(selection!=4)//while selection is not 4(end), the program will run.
{
count2=0;
cout<<"Enter your selection to display the pairs"<<endl;
cout<<"1. Display the pairs unsorted."<<endl;//shows numbers in current order
cout<<"2. Display the integer values from low to high."<<endl;
cout<<"3. Display the float values from low to high."<<endl;
cout<<"4. Exit Program"<<endl;
cin>>selection;
switch (selection)//using a switch statement to offer user a menu
{
case 1: while(count2<count)
{
cout<<entry[count2].member1<<""<<entry[count2].member2<<endl;//displays all numbers in no order
count2++;//using a second counter variable.
break;
}
case 2: for (int i=0;i<count-1;i++)
{
for(int j=i+1; j<count;j++)
{
if (entry[i].member1<entry[j].member2)//used < instead of > to sort in ascending order.
{ temp=entry[i];
entry[i]=entry[j];
entry[j]=temp;
}
}
}
while(count2<count)
{
cout<<entry[count2].member1<<""<<entry[count2].member2<<endl;
count2++;
}
break;
case 3: for(int i=0;i<count-1;i++)
{
for(int j=i+1;j<count;j++)
{
if (entry[i].member2<entry[j].member2)
{
temp=entry[i];
entry[i]=entry[j];
entry[j]=temp;
}
}
}
while(count2<count)
{
cout<<entry[count2].member1<<""<<entry[count2].member2<<endl;
count2++;
}
break;
case 4: break;
default: cout<<"Invalid selection. Choose again!"<<endl;
}
}
}
return 0;
}
|
I keep getting 00 for every number if you could shed some light it would much apperciated