I am trying so make a structure to sort int and float values from a file using an array. How can I print the printmaking array? The code sorts them but when I hit option 1 it gives sorted values as well and not the original values.
#include <iostream>
#include <fstream>
using namespace std;
struct info
{
int member1;
float member2;
}infoentry[10];
int main ()
{
const int N = 10;
info info_entry[N] {} ;
info copy_of_entry[N];
for( int i = 0; i < N; ++i)
{
copy_of_entry[i] = info_entry[i];
}
int count;
int count2;
int selection=0;
info temp;
fstream myfile;
myfile.open("CSC2144N.txt");
for(count=0;count<10;count++)
{
myfile>>infoentry[count].member1;
myfile>>infoentry[count].member2;
if(myfile.eof())
{
count++;break;
}
}
myfile.close();
while(selection!=4)
{
count2=0;
cout<<"Enter your selection to display the pairs"<<endl;
cout<<"1. Display the pairs unsorted."<<endl;
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)
{
case 1: while(count2<count)
{
cout<<copy_of_entry<< endl;
break;
}
case 2: for (int i=0;i<count-1;i++)
{
for(int j=i+1; j<count;j++)
{
if (infoentry[i].member1<infoentry[j].member1)
{ temp=infoentry[i];
infoentry[i]=infoentry[j];
infoentry[j]=temp;
}
}
}
while(count2<count)
{
cout<<infoentry[count2].member1<<"\n"<<endl;
count2++;
}
cout<<"Enter your selection to display the pairs"<<endl;
cout<<"1. Display the pairs unsorted."<<endl;
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)
{
case 1: while(count2< count)
{
for (int i; i < 10; i++)
{
cout << infoentry[i].member1 << "" << infoentry[i].member2 << endl;
}
break;
}
case 2: for (int i=0;i<count-1;i++)
{
for(int j=i+1; j<count;j++)
{
if (infoentry[i].member1<infoentry[j].member1)
{ temp=infoentry[i];
infoentry[i]=infoentry[j];
infoentry[j]=temp;
}
}
}
while(count2<count)
{
cout<<infoentry[count2].member1<<"\n"<<endl;
count2++;
}
when i hit option 1 the program is sorting the int values like in option 2. I dont understand why it is doing this when i am just telling it to print the original.
ok so i was piddling around with my previous assignments and finally got this and
IIIIIIIITTTTTTTTTTTT WWWWWORRKKKKSS!!
#include <iostream>
#include <fstream>
using namespace std;
struct info
{
int member1;
float member2;
}infoentry[10];
int main ()
{
int c;
int count2;
int selection=0;
info temp;
info infoentrycopy[10];
fstream myfile;
myfile.open("CSC2144N.txt");
for(c = 0; c < 10; c++)
{
myfile>>infoentry[c].member1;
myfile>>infoentry[c].member2;
if(myfile.eof())
{
c++;
break;
}
}
myfile.close();
while(selection!=4)
{
count2=0;
cout<<"\nEnter your selection to display the pairs\n"<<endl;
cout<<"1. Display the pairs unsorted."<<endl;
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)
{
case 1: