Computing.Net > Forums > Programming > Need Help with Structures

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Need Help with Structures

Reply to Message Icon

Name: Catalinaeva1
Date: September 2, 2007 at 13:47:26 Pacific
OS: XP
CPU/Ram: Intel/256
Product: HP
Comment:

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




Sponsored Link
Ads by Google

Response Number 1
Name: Guy
Date: September 2, 2007 at 17:01:47 Pacific
Reply:

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


0

Response Number 2
Name: Catalinaeva1
Date: September 2, 2007 at 18:15:31 Pacific
Reply:

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 program

I 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 twice

I'm aware about the variable j. I only used it because someone suggested I do so.


0

Response Number 3
Name: Razor2.3
Date: September 2, 2007 at 21:04:04 Pacific
Reply:

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 input

if(!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 file1

system ("PAUSE");
return 0;
}



0

Response Number 4
Name: Catalinaeva1
Date: September 2, 2007 at 21:17:27 Pacific
Reply:

Thank you so much!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Destop Poblem disabling a device progra...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Need Help with Structures

Need help with WinXP batch script www.computing.net/answers/programming/need-help-with-winxp-batch-script/13165.html

Need help with an SQL join query www.computing.net/answers/programming/need-help-with-an-sql-join-query/13358.html

Need help with algorithm!! www.computing.net/answers/programming/need-help-with-algorithm/594.html