Help Duplicating Array

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

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

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.
Could you show me what the file "CSC1244N.txt" look like?

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
struct info
{
    int a ;
    float b ;
};

int main()
{
    const int 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

    // ...
}
IM sorry long weekend. Ok the file is just a notepad file that has these values indie it

10 7.35 -21 45.9 3 -4.56 85 34.1 -9 -32.7
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++;
}

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