I have my program...which displays a main menu and gives you four choices, one of which is exit. It is then supposed to move on to the next menu or the part of the program you have chosen, i.e. add temp, delete temp, list temps(this has a serperate menu of three things, list temp, list all temps, and list job), from the names i have given you can guess what they do.
It also has a readInFile and writeOutFile they both read and write to the one text file which contains details of 20 people (name, sex, job, and salary) the file is called temps.txt .
My code is complete...no errors at all...even the build is fine...but when i run it i only get my main menu popping up, i select a choice and press enter and then the main menu shows again...none of the choices work at all...and i have no idea what to do..help anyone?

//---------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include "validate.h"


#define MAX 12



void readInFile(void);
void writeFile(void);
void executeMenu(void);
void displayMenu(void);
void processOption(void);
void addTemp(void);
void deleteTemp(void);
void listTemps(void);
void displayListMenu(void);
void processListOption(void);
void list1Temp(void);
void listAllTemps(void);
void listJob(void);



typedef struct
{
char tempNo[5];
char surname[30];
char forename[30];
char sexCode;
char job[30];
float expectedSalary;
} recType;



recType tempList[MAX];
int count=0;
char option, listChoice;


int main(int argc, char* argv[])
{
readInFile();
executeMenu();
writeFile();


holdScreen();


getch();
}
void executeMenu(void)
{
do
{
displayMenu();
processOption();
}
while(option != '0');
}
void writeFile(void)
{
ofstream outfile;
int i;
char ch;


clrscr();
cout<<endl<<"Writing data to disk....";
holdScreen();
outfile.open("E:\\College\\C++\\Coding Case Study\\temps.txt");
for (i=0;i<count;i++)
{
outfile<<tempList.tempNo<<endl;
outfile<<tempList.surname<<endl;
outfile<<tempList.forename<<endl;
outfile<<tempList.sexCode<<endl;
outfile.put(ch);
outfile<<tempList.job<<endl;
outfile<<tempList.expectedSalary;
outfile.put(ch);
}
outfile.close();
}
void readInFile(void)
{
int index;
ifstream infile;
char ch;


infile.open("E:\\College\\C++\\Coding Case Study\\temps.txt");


if (!infile)
{
clrscr();
cout<<endl<<"Input file does not exist..."<<endl;
holdScreen();
exit(1);
}
else
{
index=0;
while (!infile.eof())
{
infile>>tempList[index].tempNo;
infile>>tempList[index].surname;
infile>>tempList[index].forename;
infile>>tempList[index].sexCode;
infile.get(ch);
infile.getline(tempList[index].job,30);
infile>>tempList[index].expectedSalary;
infile.get(ch);
index++;
}
infile.close();
count=index;
}
}


void displayMenu(void)
{
do
{
cout<<"Temp Agency"<<endl;
cout<<"Main Menu"<<endl;
cout<<setw(10)<<setiosflags(ios::left)<<"1"<<"Add New Temp"<<endl;
cout<<setw(10)<<setiosflags(ios::left)<<"2"<<"Delete Temp"<<endl;
cout<<setw(10)<<setiosflags(ios::left)<<"3"<<"List Temps"<<endl;
cout<<endl<<setw(10)<<setiosflags(ios::left)<<"0"<<"Exit"<<endl;


option=getValidChar("Please enter your choice...","0123");


}
while(option>='0' && option<'4');
}
void processOption(void)
{
switch(option)
{
case '1': addTemp();
break;
case '2': deleteTemp();
break;
case '3': listTemps();
break;
}
}
void listTemps(void)
{
displayListMenu();
processListOption();
}
void displayListMenu(void)
{
do
{
cout<<"Temp Agency"<<endl;


cout<<setw(10)<<setiosflags(ios::left)<<"1"<<"List One Temp"<<endl;
cout<<setw(10)<<setiosflags(ios::left)<<"2"<<"List All Temps"<<endl;
cout<<setw(10)<<setiosflags(ios::left)<<"3"<<"List Job"<<endl;
cout<<endl<<setw(10)<<setiosflags(ios::left)<<"0"<<"Exit"<<endl;


listChoice=getValidChar("Please enter your choice...","0123");
}
while(listChoice>='0' && listChoice<'4');
}
void processListOption(void)
{
switch(listChoice)
{
case '1': list1Temp();
break;
case '2': listAllTemps();
break;
case '3': listJob();
break;
}
}
void list1Temp(void)
{
int index;
bool found;
char searchTempNo[5];


found=false;
index=0;


getValidString("Enter temp no. : ",tempList[index].tempNo,4);


if (found==true)
{
cout<<setw(25)<<setiosflags(ios::left)<<"Temp Number:  "<<tempList[index].tempNo<<endl;
cout<<setw(25)<<setiosflags(ios::left)<<"Name: "<<tempList[index].forename<<" " << tempList[index].surname<<endl;
cout<<setw(25)<<setiosflags(ios::left)<<"Sex code:  "<<tempList[index].sexCode<<endl;
cout<<setw(25)<<setiosflags(ios::left)<<"Job:  "<<tempList[index].job<<endl;
cout<<fixed;
cout.precision(2);
cout<<setw(25)<<setiosflags(ios::left)<<"Expected salary:  "<<tempList[index].expectedSalary<<endl;
}
else
{
cout<<"Temp not found...Please check you have entered it correctly..."<<endl;
}
}
void listAllTemps(void)
{
int index;


cout<<"All temp details..."<<endl;


for(index=0;index<count;index++)
{
cout<<endl<<setw(25)<<setiosflags(ios::left)<<"Temp Number:  "<<tempList[index].tempNo<<endl;
cout<<setw(25)<<setiosflags(ios::left)<<"Name: "<<tempList[index].forename<<" " << tempList[index].surname<<endl;
cout<<setw(25)<<setiosflags(ios::left)<<"Sex code:  "<<tempList[index].sexCode<<endl;
cout<<setw(25)<<setiosflags(ios::left)<<"Job:  "<<tempList[index].job<<endl;
cout<<fixed;
cout.precision(2);
cout<<setw(25)<<setiosflags(ios::left)<<"Expected salary:  \x9c"<<tempList[index].expectedSalary<<endl;
}
}
void listJob(void)
{
int index;
char searchJob[30];
bool found;
float salary;


cout<<"Enter job... ";
cin.get(searchJob ,30);


salary=getValidFloat("Enter salary... ",10000,1000000);


found=false;


for(index=0;index<count;index++)
{
if(found==true && salary>=tempList[index].expectedSalary)
{
cout<<endl<<setw(25)<<setiosflags(ios::left)<<"Temp Number:  "<<tempList[index].tempNo<<endl;
cout<<setw(25)<<setiosflags(ios::left)<<"Name: "<<tempList[index].forename<<" " << tempList[index].surname<<endl;
cout<<setw(25)<<setiosflags(ios::left)<<"Sex code:  "<<tempList[index].sexCode<<endl;
cout<<setw(25)<<setiosflags(ios::left)<<"Job:  "<<tempList[index].job<<endl;
cout<<fixed;
cout.precision(2);
cout<<setw(25)<<setiosflags(ios::left)<<"Expected salary:  \x9c"<<tempList[index].expectedSalary<<endl;
}
}


if (found==false)
{
cout<<"No temp matches your criteria...Please try again..."<<endl;
}
}
bool findPosition(char searchTempNo[4],int& position)
{       int index;
char target[4];
bool found=false;
index=0;
while (found==false && index<count)
{
if (strcmp (searchTempNo,target))
found=true;
else
index++;
}
if (found==true)
{
position=index;
}
return found;
}
void addTemp(void)
{
int index, positionInList;
ofstream outfile;
char searchNo[5];
if (count<MAX)
{
getValidString("Enter temp no. : ",searchNo,4);


if(findPosition(searchNo,positionInList)==true)
{
cout<<"Enter surname : ";
cin>>tempList[index].surname;
cout<<"Enter forname : ";
cin>>tempList[index].forename;
tempList[index].sexCode=getValidChar("Enter sex: ","MF");
cout<<"Enter job: ";
cin.get(tempList[index].job,30);
tempList[index].expectedSalary=getValidFloat("Enter salary: ",10000,1000000);


outfile.open("E:\\College\\C++\\Coding Case Study\\TempList.txt");


for (index=0;index<count;index++)
{
outfile<<tempList[index].tempNo<<endl;
outfile<<tempList[index].surname<<endl;
outfile<<tempList[index].forename<<endl;
outfile<<tempList[index].sexCode<<endl;
outfile<<tempList[index].job<<endl;
outfile<<tempList[index].expectedSalary;
}


outfile.close();
}
}
}
void deleteTemp(void)
{
int index,  positionInList;
char searchNo[5];


if(count>0)
{
getValidString("Enter temp no. : ",searchNo,4);
if(findPosition(searchNo,positionInList)==true)
{
if(positionInList==count-1)
{
count--;
}
else
{
for(index=positionInList; index<count; index++)
{
tempList[index]=tempList[index+1];
}
count--;
}
}
else
{
cout<<"Please check you hvae entered the correct temp no...."<<endl;
}
}
else
{
cout<<"List is empty, you cannot delete a temp..."<<endl;
}
}
//---------------------------------------------------------------------------

Future thanks for any help.
Lbs Btw.

Recommended Answers

All 6 Replies

#define MAX 12 has been changed to 20, hadnt done it before i posted.
Lbs Btw.

How does the option chosen in the displayMenu( ) get to the processOption( ) function?

For that matter, in displayMenu, your loop control keeps one seeing the menu and making a choice, as long as the choice is valid. Shoudn't that go the other way round?

You need to work on the idea of function parameters and return values in order to connect the parts of your program together.

Your problem is that you have not read http://www.daniweb.com/forums/thread78223.html yet!!!

Jencas... i did actually read that before i posted...but i couldnt make my code any shorter as it is all relevant to the problem...otherwise i have met all the requirements of it.

How does the option chosen in the displayMenu( ) get to the processOption( ) function?

For that matter, in displayMenu, your loop control keeps one seeing the menu and making a choice, as long as the choice is valid. Shoudn't that go the other way round?

You need to work on the idea of function parameters and return values in order to connect the parts of your program together.

I have a global variable called option which is used between them...when i put a break point in and go through it...it gets to the beginning of the switch in processOption and then jumps straight back up to executeMenu and displays the main menu again... i did change the constraints in my while's and i got the next menu but....i selected 1 for addTemp and when i got to entering my surname..it crashed and said "Access Violation"... Thanks for your help :)

Without the validate.h file, no one can compile this.

Also, you should adopt the current header files
#include <cstdio>
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include "validate.h"
using namespace std;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.