Help!

I am not getting the proper outputs that relate to the menu displayed.What do I need to fix?
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
#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;

system("pause");
		}}}}
Last edited on
or(count=0;count<=10;count++)
You're going out of bounds! Change it from count<=10 to count<10

{count++;break;}
You're breaking out of the for loop...why are you pointlessly incrementing count?

And the rest if your code is unreadable. Please format it correctly and place your code inside [code][/code] tags so it has syntax highlighting. Then maybe you'll get more help ;)
Last edited on
Thanks. I made the change for the <= to <.

I think I need to remove the break and let the for loop work?
On line 52 you forgot to add a break; so that it doesn't go on to case 2 after case 1.
Topic archived. No new replies allowed.