Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Need Help with Structures

Original Message
Name: Catalinaeva1
Date: September 2, 2007 at 13:47:26 Pacific
Subject: Need Help with Structures
OS: XP
CPU/Ram: Intel/256
Model/Manufacturer: 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;
}



Report Offensive Message For Removal


Response Number 1
Name: Guy
Date: September 2, 2007 at 17:01:47 Pacific
Subject: Need Help with Structures
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 2
Name: Catalinaeva1
Date: September 2, 2007 at 18:15:31 Pacific
Subject: Need Help with Structures
Reply: (edit)
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.


Report Offensive Follow Up For Removal

Response Number 3
Name: Razor2.3
Date: September 2, 2007 at 21:04:04 Pacific
Subject: Need Help with Structures
Reply: (edit)
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;
}



Report Offensive Follow Up For Removal

Response Number 4
Name: Catalinaeva1
Date: September 2, 2007 at 21:17:27 Pacific
Subject: Need Help with Structures
Reply: (edit)
Thank you so much!

Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Need Help with Structures

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




acer 312T BIOS problem

K7 Turbo possible max fsb?

Pc anywher problem

WinFLP & OE/Outlook2003

Computer resets after a few minutes


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC