Duplicate array??

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++;
}

break;

case 3: for(int i=0;i<count-1;i++)

{
for(int j=i+1;j<count;j++)
{
if (infoentry[i].member2<infoentry[j].member2)
{
temp=infoentry[i];
infoentry[i]=infoentry[j];
infoentry[j]=temp;
}
}
}
while(count2<count)
{
cout<<"\n"<<infoentry[count2].member2<<endl;
count2++;
}
break;

case 4: break;
default: cout<<"Invalid selection. Choose again!"<<endl;

}
}

return 0;
}
The file is just a notepad file that has these values in it

10 7.35 -21 45.9 3 -4.56 85 34.1 -9 -32.7
That's because it is an array do a loop to access element int and float to print
How would you exactly do that?

i used something like:

for (int i; i < 10; i++)
{

cout << infoentry[i] << endl;

}

would this work?

close.
think its

cout << infoentry[i].member1 << " words " << infoentry[i].member2 << endl;

or you can overload the << operator for your structure to print in some fashion.
Last edited on
So I would just put the loop in the case 1 and it should print the array in its original form?
this is what i have now. When i hit 1 the program stops working. When i hit 2 and 3 it still sorts properly.

#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)
{
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++;
}

break;

case 3: for(int i=0;i<count-1;i++)

{
for(int j=i+1;j<count;j++)
{
if (infoentry[i].member2<infoentry[j].member2)
{
temp=infoentry[i];
infoentry[i]=infoentry[j];
infoentry[j]=temp;
}
}
}
while(count2<count)
{
cout<<"\n"<<infoentry[count2].member2<<endl;
count2++;
}
break;

case 4: break;
default: cout<<"Invalid selection. Choose again!"<<endl;

}
}

return 0;
}
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:

{

ifstream file("CSC2144N.TXT");

string content;

while(file >> content)
{
cout << content << "\n" << ' ';
}

break;

}

case 2:

for (int i=0 ; i < c-1 ; i++)

{

for(int j = i+1; j < c; j++)

{

if (infoentry[i].member1 < infoentry[j].member1)
{
temp = infoentry[i];
infoentry[i] = infoentry[j];
infoentry[j] = temp;
}
}
}

while(count2 < c)

{
cout<<infoentry[count2].member1<<"\n"<<endl;
count2++;
}

break;

case 3:

for(int i = 0;i < c-1; i++)

{

for(int j=i+1 ; j<c ; j++)
{

if (infoentry[i].member2 < infoentry[j].member2)

{

temp = infoentry[i];
infoentry[i] = infoentry[j];
infoentry[j] = temp;

}
}
}

while(count2 < c)

{
cout<<"\n"<<infoentry[count2].member2<<endl;
count2++;
}

break;

case 4: break;
default: cout<<"Invalid selection. Choose again!"<<endl;

}
}

return 0;
}
Topic archived. No new replies allowed.