There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox freezes freezing hard drive hardware hijackthis internet internet explorer ipod itunes laptop malware motherboard mouse network networking outlook 2007 power printer problem ram router screen slow sound trojan upgrade usb virus vista vista 32-bit windows windows vista windows xp winxp wireless
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Please Help With C++ Structure Program


Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
certified's Avatar
Junior Member with 17 posts.
 
Join Date: Nov 2003
02-Mar-2004, 08:01 PM #1
Please Help With C++ Structure Program
Hello,

i tried to do this program in structure but have a hard time with it. tried everything and keep getting strange errors and i don't know off. please help

here is the program:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


const int max = 20;


void inputData(People *);
void openfile();
void Quit();
int menu();

struct People
{
char *fn[max];
char *ln[max];
char *id[max];
int ssn[max];
char citizen[max];
};



int main()
{

People main;

int selection;

do
{
selection = menu();

switch (selection)
{
case 1:
inputData(main);
break;
case 2:
openfile();
break;
case 3:
Quit();
break;
default:
cout << "Invalid choice!";
}
}while(selection != 4);

return 0;
}

void inputData(People *input)
{


for (int count = 0; count < numEmployee; count++)
{

ofstream marketingfile("data.txt", ios:ut | ios::app);


cout << "Enter First Name:\n";

*input.fn[count] = new char[max];
cin >> *input.fn[count];

cout << "Enter Last Name: \n";

*input.ln[count] = new char[max];
cin >> *input.ln[count];

cout << "Enter ID: \n";

*input.id[count] = new char[max];
cin >> *input.id[count];

cout << "Enter SSN: \n";

cin >> *input.ssn[count];

cout << "Are You a Citizen Y/N? \n";
cin >> *input.citizen[count];


marketingfile << *input.fn[count] << "\t\t" << *input.ln[count] << "\t\t" << *input.id[count] << "\t" << *input.ssn[count] << "\t\t" << *input.citizen[count] << endl;


marketingfile.close();

}


}

void openfile()
{
char filecontent[51];

fstream OpenFile;
// open the file
OpenFile.open("data.txt", ios::in);

// check if the file exits
if(!OpenFile)
{
cout << "ERROR: file does not exist.\n";
exit(0);
}

OpenFile.getline(filecontent, 51, '\n');
while (!OpenFile.eof())
{
cout << filecontent << endl;
OpenFile.getline(filecontent, 51, '\n');
}
// close the file
OpenFile.close();
}

void Quit()
{
exit(0);
}

int menu()
{

int choice;

do
{
cout << endl;
cout << "Menu Option\n\n";
cout << "1: Enter Data\n";
cout << "2: Display Data\n";
cout << "3: Quit";
cin >> choice;

if ((choice >= 1) && (choice <= 3))
{
return choice;
}
else
{
return 0;
}
}while (choice != 4);

exit(0);
}
coderitr's Avatar
Distinguished Member with 3,080 posts.
 
Join Date: Oct 2003
02-Mar-2004, 08:25 PM #2
Hi Cert ... welcome to TSG.

I see a few issues with your code:

(1) you refer to the struct People in the prototype for the InputData function before it is declared Move the struct declaration above the function prototypes.

(2) You need to add a typedef qualifier to the struct People declaration. The way you have it coded, you're using more memory than you need to; People is a defined instance of the structure using memory as opposed to a type definition that you can use later to declare an instance of that type.

(3) I'm not 100% sure that the int count declaration in InputData function has scope to the entire loop but I may be wrong. Move declaration of count above the for( ; ; ) loop to be safe. It doesn't use any more memory and is simpler to read.

(4) If the file open fails, you are assuming that the file does not exist when, in fact, there could be another problem (access denied, file in use, etc.). Meaningful error reporting suggests that you interrogate the error code and display an appropriate message based on the value.

(5) Instead of dereferencing the pointer to input in the InputData function why not use -> to set the values? A matter of style, yes, but I'm not clear why you're doing that.

(6) The loop in main( ) will never end. You are ending when you get a value of 4 back from menu( ) but if the user enters 4, you are returning 0. The exit() will never be reached. You need to return a value from menu( ) and have the loop in main( ) decide whether or not to exit the program. Using exit( ) from a called function can lead to stack overflows.

Good Luck
certified's Avatar
Junior Member with 17 posts.
 
Join Date: Nov 2003
02-Mar-2004, 09:24 PM #3
Thank you for your reply. could you post examples, it would easier for me as it is so hard trying to understand with words. thank you so much
coderitr's Avatar
Distinguished Member with 3,080 posts.
 
Join Date: Oct 2003
02-Mar-2004, 09:48 PM #4
You've got a good starting point there. Follow my instructions above and you'll be fine. Also, three things that I didn't see before:

(1) You don't need the subscript for putting the values from cin into the char variables nor do you need to instantiate them with the new keyword. Remove the "... = new char[max]" lines entirely and code the following:

cin >> input->fn;

(2) You are keying on the value 4 to exit the program but the Quit option is #3. Fix that.

(3) Don't use exit( ) from the Quit( ) function. Just return from main( ) -- it's that easy.

This looks like homework. There are talented and helpful people here at TSG and you're welcome to ask any question you like but we won't do your work for you.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 07:02 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.