ok so here it is. Here is my code to read my file 'CSC1244N.txt' and sort the integer values and float values in ascending order using a structure.
#include <iostream>
#include <fstream>
using namespace std;
struct info
{
int member1;
float member2;
}infoentry[10];
int main ()
{
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<<infoentry[count2].member1<<""<<infoentry[count2].member2<<endl;
count2++;
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++;
}
case 4: break;
default: cout<<"Invalid selection. Choose again!"<<endl;
}
}
return 0;
}
Can someone please explain to me how i can duplicate my Infoentry array so thati can use it to output the original file values? When the program is run and you select 1 it gives me random values.
struct info
{
int a ;
float b ;
};
int main()
{
constint N = 10 ;
info info_entry[N] {} ;
// read values into array info_entry
// ...
// make a copy of the array info_entry
// note: as it is, the type info is a trivially copyable type,
// so we could just do memcpy or std::uninitialized_copy here
// this is the code for the more general case, where info need not be trivially copyable
info copy_of_info_entry[N] ;
// more robust: decltype(info_entry) copy_of_info_entry ;
for( int i = 0 ; i < N ; ++i ) copy_of_info_entry[i] = info_entry[i] ;
// or: #include <algorithm> and
std::copy( info_entry, info_entry+N, copy_of_info_entry ) ;
// sort info_entry
// ...
}
I am getting this. I am not sure if i am following correctly.
#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++;
}
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: