Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I'm new to this forum. I'm currently taking this C++ class online, and I'm stuck on a program; I'm still new to learning the C++ language, and the notes that we're given for this course don't always match the assignments that we're working on. My "C++ for Dummies" book hasn't been very helpful, either. I've browsed through several tutorials and asked some people for help, but I'm still not understanding the material very well. Can someone please help modify/change my code in order to get it to do what it's supposed to do so that I can see where I went wrong? (Note: I'm fully aware that my code is not 100% correct or complete.)
Thanks in advance. Here are the instructions.
Create a text file named "CSC2143N.TXT" using Notepad or other text editor. Copy
the following lines of text and paste them into the file and save.10 7.35
-21 45.9
3 -4.56
85 34.1
-9 -32.7Write 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 "CSC2143N.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
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.#include <fstream>
#include <iostream>using namespace std;
struct record {
int i;
float f;
} numbers[10];
int main(int argc, char *argv[])
{
ifstream file1; // declare the file variable
file1.open("CSC2143N.TXT"); // open the file for input
if(file1 == 0) {
cout << "Error opening CSC2143N.TXT" << endl;
return 1;
}
int n, j, sel; // sel means selection
n = 0;
n = 0; // keeps up with size of array
while(1) // endless loop unless break is encounterd
{
file1 >> numbers[n].i;
file1 >> numbers[n].f;
n++;
if(file1.eof())
break; //exit the loop at the end of the file
}
// main menu
sel = 0;
while(sel != 4) {
cout << "Main menu" << endl << endl;
cout << "1. Display pairs unsorted" << endl;
cout << "2. Display pairs sorted in ascending order of the integer values" << endl;
cout << "3. Display pairs sorted in ascending order of the float values" << endl << endl;
cout << "4. Exit program";
cout << endl;
cin >> sel;
cout << endl;
}
file1.close(); // close file1
system ("PAUSE");
return 0;
}

Your code compiles and works, as far as it goes. Actually reads the file correctly, a step in the right direction.
No 'PAUSE' on my system, but that is no big.
What exactly is your question?
What additional have you tried?
Guy
Edit to nitpick:
. The variable j is never used
. The variable n is needlessly initialized twice

Well, I don't know how to go about setting this part up.
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 programI don't know what's going on, but it's not displaying any numbers or working with my DEVC++ compiler when I simply try to open the text and display the numbers to the console. I need help in getting the program to run the menu and do what the options tell it to do.
Edit to nitpick:
. The variable j is never used
. The variable n is needlessly initialized twiceI'm aware about the variable j. I only used it because someone suggested I do so.

Probably not what your professor was expecting, but I got this. Just realize option #1 is only valid until you use option 2 or 3. If that's not what you want, you'll have to make a copy of your array and pass it to the sort, then print out that array. (My changes in red):
#include <iostream>
#include <fstream>
#include <algorithm> //Required for sort()using namespace std;
struct record {
int i;
float f;
} numbers[10];
struct recordI {
record r;
bool operator< (const recordI& comp) const {
return r.i < comp.r.i; }
};struct recordF {
record r;
bool operator< (const recordF& comp) const {
return r.f < comp.r.f;}
};
void pntRec(int count) {
int j = -1;
while(++j < count)
cout << numbers[j].i << " " << numbers[j].f << endl;
cout << endl;
}
int main(int argc, char *argv[]) {ifstream file1; // declare the file variable
file1.open("CSC2143N.TXT"); // open the file for inputif(!file1) {
cout << "Error opening CSC2143N.TXT" << endl;
return 1;
}int n, sel; // sel means selection
n = 0;while(1) // endless loop unless break is encounterd
{
file1 >> numbers[n].i;
file1 >> numbers[n].f;
n++;
if(file1.eof())
break; //exit the loop at the end of the file
}// main menu
sel = 0;
while(sel != 4) {
cout << "Main menu" << endl << endl;
cout << "1. Display pairs unsorted" << endl;
cout << "2. Display pairs sorted in ascending order of the integer values" << endl;
cout << "3. Display pairs sorted in ascending order of the float values" << endl << endl;
cout << "4. Exit program";
cout << endl;
cin >> sel;
cout << endl;
switch(sel) {
case 1:
pntRec(n);
break;
case 2:
sort(reinterpret_cast<recordI*>(&numbers[0]), reinterpret_cast<recordI*>(&numbers[n]));
pntRec(n);
break;
case 3:
sort(reinterpret_cast<recordF*>(&numbers[0]), reinterpret_cast<recordF*>(&numbers[n]));
pntRec(n);
}
}
file1.close(); // close file1system ("PAUSE");
return 0;
}

![]() |
Destop Poblem
|
disabling a device progra...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |