text file into array of structs

Pages: 12
I can not get this horrible code to this is the question as follows:
Write a program to declare a structure consisting of an integer member and a float member, and declare an array of the structure with space for at least 10 elements. The program should then open the text file named "CSC2144N.TXT" (that you created above) for input, then read all the numbers from the file into the structure array and then display the following menu to prompt the user to display the number pairs in one of three possible orders:
1. display pairs unsorted 2. display pairs sorted in ascending order of the integer values 3. display pairs sorted in ascending order of the float values 4. exit program
Note: the program should work for any number of integer float pairs that might be in the data file, not just 5 pairs.
The program should use an adaptation of the exchange sort procedure to perform the sorts. After displaying the values in the desired order, the user should be prompted again with the menu.
Note: you should probably have a duplicate array to keep the unsorted values in case the user enters 2 or 3 and then enters 1. Alternatively, you could simply re-read the file.

in the text file is:
10 7.35 -21 45.9 3 -4.56 85 34.1 -9 -32.7

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  #include <iostream>
#include <fstream>

using namespace 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;

            }
        }
    }
return 0;
}


I keep getting 00 for every number if you could shed some light it would much apperciated
will somebody please look at this for me and give their thoughts
On line 11 you have }entry[10]; //declaring an array of the data structure with space for 10 elements
You are declaring the entry[10] outside the struct. It should be inside it right?
Also, I think you want to to write info entry[10]. This should go inside the struct definition.
Oh... I just saw: you are using all those infos without initializing them. No wonder you are getting zeroes. Shouldn't you read them in from the file that you were provided with?
Does this answer your question?
Last edited on
yes that makes a lot of sense let me try it on code blocks and see what iget
it gave me a bunch of errors when I put it in the struct like you said.

1
2
3
4
5
6
7
8
struct info  //declaring my data stucture
{
	int member1; //declaring a variables within the data stucture
	float member2;

info entry[10];
} //declaring an array of the data structure with space for 10 elements
so for each entry I need to put info in front it correct?
I put info in front of entry and it didn't fix the problem I'm sorry if I'm not understanding you I'm not very good at this.
No problem, I was a beginner once myself. Actually, try putting the info entry[10]; outside the struct. I'm sorry I told you it goes on the outside. I think you are good to go after this.
Good luck!
man I still got the same problem nothing 000000000. here is my code that I redid tell me if you see if I did something wrong
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <fstream>

using namespace std;

struct info  //declaring my data stucture
{
	int member1; //declaring a variables within the data stucture
	float member2;

}infoentry[10]; //declaring an array of the data structure with space for 10 elements;
int main ()
{
int count1;
int count2;
int selection=0;
info temp;
fstream myfile;//naming my file
myfile.open("CSC2144N.txt");//linking file name to .txt doc.
	for(count1=0;count1<10;count1++)//for loop to cycle through up to 10 entries

	{

		myfile>>infoentry[count1].member1;
		myfile>>infoentry[count1].member2;
		if(myfile.eof())//if end of file is reached the file is closed.
	{count1++;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<count1)
		{
			cout<<infoentry[count2].member1<<""<<infoentry[count2].member2<<endl;//displays all numbers in no order
			count2++;//using a second counter variable.
			break;
		}

case 2: for (int i=0;i<count1-1;i++)
		{
		{	for(int j=i+1; j<count1;j++)
		{   if (infoentry[i].member1<infoentry[j].member2)//used < instead of > to sort in ascending order.
		{	temp=infoentry[i];
			infoentry[i]=infoentry[j];
			infoentry[j]=temp;

		}}}}
		while(count2<count1)
			{cout<<infoentry[count2].member1<<""<<infoentry[count2].member2<<endl;
		count2++;
		}break;

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

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

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

		}}}}
Try this( idk if it'll work,but maybe its worth a try :) )
1
2
3
4
5
6
7
8
9
10
11
char c;
for(count1=0;count1<10;count1++)//for loop to cycle through up to 10 entries 
{ 
 myfiles.get(c);
infoentry[count1].member1 = (int) c;
myfiles.get(c);
 infoentry[count1].member2 = (float) c;
if(myfile.eof())
//if end of file is reached the file is closed.
 {count1++;break;}
 }

Save your earlier implementation with a " /*....*/ " comment.
Note: this mod might hav a trouble,in converting char to float. char to int should work( i guess)
I'm not understanding what you mean Mabunny?
I suppose fstream has a get() function, like iostream, which is maybe in ios or ios-base header(i guess,still a noob). Use the above code in lines19 to 29 of your nxt to last post and see if it works or not.
@idk1234 Do you know how to read in the numbers from the text file? I can show you how if you don't.
I didn't work thanks for yalls I'm really frustrated I think ill just take a 0 before I throw my computer out the window lol O.o!!
yea I know how.... I made the text file and put the numbers in just like I was suppose to is there a certain way they are suppose to be in
is there anybody willing to go through this program to figure out the problem please!!!!!
If you are getting all zeros, most likely the file was not opened properly. You can check after opening, like this:
1
2
3
4
5
6
    ifstream myfile("CSC2144N.txt");    // linking file name to .txt doc.
    if (!myfile)
    {
        cout << "File is not open\n";
        return 1; 
    }

Note you can declare and open the file in a single statement.
?
Last edited on
I'm not understanding sorry
ok @ chervil I figured out what you where saying and the file will not open so how do I fixed that
Pages: 12