C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 4th, 2008, 05:27 PM
Mavigon Mavigon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 4 Mavigon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 58 sec
Reputation Power: 0
File IO - Binary file.read program

Quote:
In an infinite loop show the following menu and allow users to select an operation:

1. List the students sorted by last name
2. Search the students by last name
3. List students with more than certain GPA
4. Quit the program

Once the user selects an operation, your program should perform the operation and display the menu again (except for Quit the program). For options 1 your program needs to sort the array and then display list of students with major and GPA. For options 2 your program should ask the user to enter student’s last name and display the student’s information. If the student doesn’t exist in the array, display appropriate message and show the menu again. For options 3 your program should ask the user to enter a cutoff GPA and list all student whose GPA is greater than or equal to the cutoff GPA. Your program should display appropriate message for user actions.






Code:
//********************************************/
// Author: David Hill
// Description: This program is  confusing
//********************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>

using namespace std;

struct Student
{
	char firstName[16];
	char lastName[16];
	char major[4];
	double GPA;	
};

// Defining Functions
void stdSORT(Student [], int);	// Option 1
void stdSEARCH(Student [], int);	// Option 2
void searchGPA(Student [], int);		// Option 3
void exchange(Student, Student ); // the extra "cup"


int main(int argc, char *argv[])
{
	int choice;			// Menu choice
	int count;			// To count number of students
	int i;
	
	
	// File Operations
	fstream file;
	file.open(argv[1], ios::in | ios::binary);

	file.read(reinterpret_cast<char *>(&count), sizeof(int));

	
	Student *STRUC = new Student[count];

	for (i = 0; i < count; i++)
	{
		file.read(reinterpret_cast<char *>(&STRUC[i]), sizeof(Student));
		
	}
	
	if (count < 0)
	{
		cout << "Error! No students detected in file\n\n";
		exit(-1);
	}
	else if (!file)
	{	
		cout << "File did not open, please try again.\n\n";
		exit(-1);
	}


	for (i = 0; i < count; i++)
	{
		file.read(reinterpret_cast<char *>(&STRUC[i]), sizeof(Student));
	}

		while(10)
		{
			cout << "Please Select an Option: " << endl;
			cout << "1. List the students sorted by last name" << endl;
			cout << "2. Search the students by last name" << endl;
			cout << "3. List students with more than certain GPA" << endl;
			cout << "4. Quit the program" << endl;
			cin.ignore();
			cin >> choice;
		
			while (choice < 1 || choice > 4)
			{
				cout << "Invalid option, please choose a new one: ";
				cin >> choice;
			}
		}
	


		switch (choice)
		{
			case 1: stdSORT(STRUC, count);				
				break;
			case 2: stdSEARCH(STRUC, count);
				break;
			case 3: searchGPA(STRUC, count);
				break;
			case 4:
				return 0;
		}



}

void stdSORT (Student STRUC[], int count)
{
   for (int i = 0; i < count-1; i++)
   {
      for (int j = 0; j < count-1-i; j++)
         if (strcmp(STRUC[j].lastName, STRUC[j+1].lastName) >= 1)
            exchange (STRUC[j], STRUC[j+1]);
		
		
		
   }
   cout << "The student's listed in alphabetical order:" ;
	for (int i = 0; i < count; i++)
		{
		cout << STRUC[i].lastName << "\n";
		cout << endl;
		}
}		

void exchange(Student S1, Student S2)
{

	Student extra;
	extra = S1;
	S1 = S2;
	S2 = extra;
}

void stdSEARCH(Student STRUC[], int count)
{
	bool flag = false;
	char stdsearch[16];

	cout << "What is the Last Name of the student you would like to search? ";
	cin >> stdsearch;
	
	for (int ctr = 0; ctr < count; ctr++)
	{
		if (strcmp(stdsearch, STRUC[ctr].lastName) == 0)
		{
			flag = true;
			cout << STRUC[ctr].firstName << endl;
			cout << STRUC[ctr].lastName << endl;
			cout << STRUC[ctr].major << endl;
			cout << STRUC[ctr].GPA << endl;
	
		}
		
	}
	if (flag == false)
	{
		cout << "Student is non-existent. Please Try again.";
		exit(-1);
	}	
	
	
}	

void searchGPA(Student STRUC[], int count)
{
	double GPAsearch;
	
	cout << "Please enter a GPA you wish to search: ";
	cin >> GPAsearch;
	while (GPAsearch < 0.0 || GPAsearch > 4.0)
	{
		cout << "Invalid GPA. Please enter again: ";
		cin >> GPAsearch;
	for (int i = 0; i < count-1; i++)
		{
			STRUC[i].GPA > GPAsearch;
			if (STRUC[i].GPA > GPAsearch)
			{
				cout << STRUC[i].firstName << "'s GPA: " << STRUC[i].GPA << endl;
			}			
		}
	}	


}





And that is all i've got. Everything works until I get my menu going in cmp. I can't seem to figure out why it won't work. Sorry for the lack of comment...

Reply With Quote
  #2  
Old December 5th, 2008, 03:34 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 38 m 39 sec
Reputation Power: 4
I am not sure what you mean by 'my menu going in cmp'.. But I do have a couple of observations.

Doing 'while(10)' does not mean 'do it 10 times', it means 'do it forever', the expression evaluates to 'while(true)'.

The use of reinterpret_cast is non-portable. This may work on your machine with your current compiler but might break on another platform. You should not read binary data this way, unless you are sure about it. Especially if the file with the data is not made by you.

Hope this helps you.
__________________
Current project: roborally

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > File IO - Binary file.read program


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT