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