#include <iostream>
#include <fstream>
usingnamespace 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;
system("pause");
}}}}
or(count=0;count<=10;count++)
You're going out of bounds! Change it from count<=10 to count<10
{count++;break;}
You're breaking out of the for loop...why are you pointlessly incrementing count?
And the rest if your code is unreadable. Please format it correctly and place your code inside [code][/code] tags so it has syntax highlighting. Then maybe you'll get more help ;)