I have two problems, I have two working programs. Now I am trying to
take one and put it within the other...because of a class project I am trying
to put the one working code within the other as a void function.
Now my second problem is that the working code my menu...when it is working and
say you pick option 2 instead of printing this is two once and ending it goes into
a endless loop of print this is two without stopping
thanks for any help and if you need more information from me just let me know.
can I have a structure as a void function?????
the combine code is below:
#include <iostream.h>
#include <fstream.h>
#include <string.h>
//define record structure
struct car
{
char name[25];
short seats;
};
//function prototypes
char getmenuchoice();
void tran(char, short);
void main()
{
//declare and initialize menu variable
char menuchoice =' ';
//display menu and get menu choice
menuchoice=getmenuchoice();
while (menuchoice=='1'||menuchoice=='2'||menuchoice=='3'||menuchoice=='4')
{
if (menuchoice=='1')//display
tran(trans.name, trans.seats); //cout<<"this is one"<<endl;
else
if(menuchoice=='2')//display
cout<<"this is two"<<endl;
else
if(menuchoice=='3')//display
cout<<"this is three"<<endl;
else
//(menuchoice=='4')//display
cout<<"this is four"<<endl;
}//end while
}//end of main function
//********Programmer-defined function definitions********
char getmenuchoice()
{
//this function displays a menu and gets the user's choice
char choice =' ';
//display menu
cout<<endl;
cout<<"Menu "<<endl;
cout<<"1 Will help decide if Campout is on and what to pack"<<endl;
cout<<"2 Will do something else for scoutmaster"<<endl;
cout<<"3 Will do another thing for the scoutmaster"<<endl;
cout<<"4 Will do something more for a scoutmaster"<<endl;
cout<<"5 Will Exit your program "<<endl;
cout<<"Enter your choice here: ";
//get user's menu choice
cin>>choice;
cin.ignore(100,'\n');
return choice;
}//end of getmenuchoice function
void tran(char name, short seats);
{
//declare and initiaialize record variable
car trans ={"",0};
//create and open output file
ofstream outfile;
outfile.open("car.txt", ios::app);
//determine if open was successful
if (!outfile.fail())//open was successful
{
//enter and write records
cout<<"Enter First Name of Driver (or exit to quit): ";
cin>>trans.name;
cin.ignore(100, '\n');
while (1)
if (strcmp(trans.name, "exit" == 0)
break;
else
{
cout<<"Enter have many seats in Car or Truck: ";
cin>>trans.seats;
outfile<<trans.name<<'#'<<trans.seats<<endl;
cout<<"Enter First Name of Driver (or exit to quit): ";
cin>>trans.name;
cin.ignore(100, '\n');
}//end while
outfile.close();//close file
}
else //open was not successful
cout<<"Error in opening file."<<endl;
//end if
}//end of tran function
take one and put it within the other...because of a class project I am trying
to put the one working code within the other as a void function.
Now my second problem is that the working code my menu...when it is working and
say you pick option 2 instead of printing this is two once and ending it goes into
a endless loop of print this is two without stopping
thanks for any help and if you need more information from me just let me know.
can I have a structure as a void function?????
the combine code is below:
#include <iostream.h>
#include <fstream.h>
#include <string.h>
//define record structure
struct car
{
char name[25];
short seats;
};
//function prototypes
char getmenuchoice();
void tran(char, short);
void main()
{
//declare and initialize menu variable
char menuchoice =' ';
//display menu and get menu choice
menuchoice=getmenuchoice();
while (menuchoice=='1'||menuchoice=='2'||menuchoice=='3'||menuchoice=='4')
{
if (menuchoice=='1')//display
tran(trans.name, trans.seats); //cout<<"this is one"<<endl;
else
if(menuchoice=='2')//display
cout<<"this is two"<<endl;
else
if(menuchoice=='3')//display
cout<<"this is three"<<endl;
else
//(menuchoice=='4')//display
cout<<"this is four"<<endl;
}//end while
}//end of main function
//********Programmer-defined function definitions********
char getmenuchoice()
{
//this function displays a menu and gets the user's choice
char choice =' ';
//display menu
cout<<endl;
cout<<"Menu "<<endl;
cout<<"1 Will help decide if Campout is on and what to pack"<<endl;
cout<<"2 Will do something else for scoutmaster"<<endl;
cout<<"3 Will do another thing for the scoutmaster"<<endl;
cout<<"4 Will do something more for a scoutmaster"<<endl;
cout<<"5 Will Exit your program "<<endl;
cout<<"Enter your choice here: ";
//get user's menu choice
cin>>choice;
cin.ignore(100,'\n');
return choice;
}//end of getmenuchoice function
void tran(char name, short seats);
{
//declare and initiaialize record variable
car trans ={"",0};
//create and open output file
ofstream outfile;
outfile.open("car.txt", ios::app);
//determine if open was successful
if (!outfile.fail())//open was successful
{
//enter and write records
cout<<"Enter First Name of Driver (or exit to quit): ";
cin>>trans.name;
cin.ignore(100, '\n');
while (1)
if (strcmp(trans.name, "exit" == 0)
break;
else
{
cout<<"Enter have many seats in Car or Truck: ";
cin>>trans.seats;
outfile<<trans.name<<'#'<<trans.seats<<endl;
cout<<"Enter First Name of Driver (or exit to quit): ";
cin>>trans.name;
cin.ignore(100, '\n');
}//end while
outfile.close();//close file
}
else //open was not successful
cout<<"Error in opening file."<<endl;
//end if
}//end of tran function