Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Void function and Menu function 2

Status
Not open for further replies.

ggreg

Programmer
Mar 9, 2001
201
US
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<<&quot;this is one&quot;<<endl;
else
if(menuchoice=='2')//display
cout<<&quot;this is two&quot;<<endl;
else
if(menuchoice=='3')//display
cout<<&quot;this is three&quot;<<endl;

else
//(menuchoice=='4')//display
cout<<&quot;this is four&quot;<<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<<&quot;Menu &quot;<<endl;
cout<<&quot;1 Will help decide if Campout is on and what to pack&quot;<<endl;
cout<<&quot;2 Will do something else for scoutmaster&quot;<<endl;
cout<<&quot;3 Will do another thing for the scoutmaster&quot;<<endl;
cout<<&quot;4 Will do something more for a scoutmaster&quot;<<endl;
cout<<&quot;5 Will Exit your program &quot;<<endl;
cout<<&quot;Enter your choice here: &quot;;
//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 ={&quot;&quot;,0};
//create and open output file
ofstream outfile;
outfile.open(&quot;car.txt&quot;, ios::app);
//determine if open was successful
if (!outfile.fail())//open was successful
{
//enter and write records
cout<<&quot;Enter First Name of Driver (or exit to quit): &quot;;
cin>>trans.name;
cin.ignore(100, '\n');
while (1)
if (strcmp(trans.name, &quot;exit&quot;) == 0)
break;
else
{
cout<<&quot;Enter have many seats in Car or Truck: &quot;;
cin>>trans.seats;
outfile<<trans.name<<'#'<<trans.seats<<endl;
cout<<&quot;Enter First Name of Driver (or exit to quit): &quot;;
cin>>trans.name;
cin.ignore(100, '\n');


}//end while

outfile.close();//close file
}
else //open was not successful
cout<<&quot;Error in opening file.&quot;<<endl;
//end if


}//end of tran function














 
You need to call &quot;getmenuchoice&quot; inside the while. menuchoice's value is not changing and that is what is causing the infinite loop

Matt
 
Zyrenthian,

I comment out my void trans function to try what you suggest on the menu but to move
getmenuchoice inside the while all it does is show me
the 4th option then if I move it inside { then it shows
me nothing

so in one way your right it does end the infinite loop
X-)

but it does not show me my menu
any other sugguestion to keep the menu working???
 
I dont know how you rewrote it but this is what I would do

do
{
menuchoice=getmenuchoice();

if (menuchoice=='1')//display
tran(trans.name, trans.seats); //cout<<&quot;this is one&quot;<<endl;
else
if(menuchoice=='2')//display
cout<<&quot;this is two&quot;<<endl;
else
if(menuchoice=='3')//display
cout<<&quot;this is three&quot;<<endl;

else
//(menuchoice=='4')//display
cout<<&quot;this is four&quot;<<endl;
}while(menuchoice=='1'||menuchoice=='2'||menuchoice=='3'||menuchoice=='4');


Matt
 
try it:
char get_menu_choise()
{
if(....)return menu_choice;
return 0;
}

the while loop

while(get_menu_choise())
{
switch(menuchoice)
{
case '1': //do something
cout<<&quot;this is one&quot;<<endl;
break;
case '2'://display
cout<<&quot;this is two&quot;<<endl;
break;
case '3':
cout<<&quot;this is three&quot;<<endl;
break;
case '4':
cout<<&quot;this is four&quot;<<endl;
break;
}
}//end while
John Fill
1c.bmp


ivfmd@mail.md
 
Zyrenthian and John,
thanks a bunch on the menu problem!!:)
can you now tell me whats wrong with with my Void tran
function????
 
Well, i notice you are passing a char* to the funciton and the function is recieiving a character... that is just a side note though

i dont think the ignore is required in your tran function.

you also have no closing bracket on your else within the wile loop... the closing bracket for the while is the end of your else

Matt

 
1.
car trans ={&quot;&quot;,0};
sould be
char* trans = new char[100]; or
char trans[100]=&quot;&quot;;
1. if you want to open in append mode
instead of ofstram you should use fstream John Fill
1c.bmp


ivfmd@mail.md
 
zyrenthian

Ok now I have try changing about 5 things one on the bracket
and 4 on my function header...if I understand you right
your telling me I am passing a char array (thats right)
but the function is recieiving only a character
I have try change the line below like it is but
that does not do it
void tran(char name[25], short seats);

And on the bracket I try moving a bracket and did not do any good... can you tell me more?
 
John,
Do you understand that

car trans ={&quot;&quot;,0};

is setting name to nothing
and seats to zero

Greg
 
I try you code John it
gives me the same errors
:\Program Files\Microsoft Visual Studio\MyProjects\men\mm.cpp(30) : error C2065: 'trans' : undeclared identifier
D:\Program Files\Microsoft Visual Studio\MyProjects\men\mm.cpp(30) : error C2228: left of '.name' must have class/struct/union type
D:\Program Files\Microsoft Visual Studio\MyProjects\men\mm.cpp(30) : error C2228: left of '.seats' must have class/struct/union type
D:\Program Files\Microsoft Visual Studio\MyProjects\men\mm.cpp(69) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.
 
look there, the error is what in the red place you put a not declared variable.

while (menuchoice=='1'||menuchoice=='2'||menuchoice=='3'||menuchoice=='4')
{
if (menuchoice=='1')//display
tran(trans.name, trans.seats); //cout<<&quot;this is one&quot;<<endl;
else
if(menuchoice=='2')//display
cout<<&quot;this is two&quot;<<endl;
else
if(menuchoice=='3')//
John Fill
1c.bmp


ivfmd@mail.md
 
hey John that one line of code that you were trying to get
me to change I bet it should be like this
car trans[100]={&quot;&quot;,0};
what do you think??
 
the function trans isn't using what you pass in as well. If you are passing an array to the funciton you can do it 1 of 2 ways

trans(char*
or
trans(char c[]

matt
 
Zyrenthian and John and anyone else who have been following along....I finally worked it out with all your help
I did change scout for tran but that did not really matter!
below is the work able code thanks a billion!!!!!!!!!!!:)
#include <iostream.h>
#include <fstream.h>
#include <string.h>
//define record structure
struct car
{
char name[25];
short seats;

};

//function prototypes
char getmenuchoice();
void scout(car[]);
void main()
{

//declare and initialize menu variable
char menuchoice =' ';

car trans[10] ={&quot;&quot;,0};


//display menu and get menu choice
do

{menuchoice=getmenuchoice();

if (menuchoice=='1')//display
scout (trans);
else
if(menuchoice=='2')//display
cout<<&quot;this is two&quot;<<endl;
else
if(menuchoice=='3')//display
cout<<&quot;this is three&quot;<<endl;

else
//(menuchoice=='4')//display
cout<<&quot;this is four&quot;<<endl;

}
while (menuchoice=='1'||menuchoice=='2'||menuchoice=='3'||menuchoice=='4');



//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<<&quot;Menu &quot;<<endl;
cout<<&quot;1 Will help decide if Campout is on and what to pack&quot;<<endl;
cout<<&quot;2 Will do something else for scoutmaster&quot;<<endl;
cout<<&quot;3 Will do another thing for the scoutmaster&quot;<<endl;
cout<<&quot;4 Will do something more for a scoutmaster&quot;<<endl;
cout<<&quot;5 Will Exit your program &quot;<<endl;
cout<<&quot;Enter your choice here: &quot;;
//get user's menu choice
cin>>choice;
cin.ignore(100,'\n');
return choice;
}//end of getmenuchoice function
void scout(car[])
{


//declare and initiaialize record variable
car trans ={&quot;&quot;,0};

//create and open output file
ofstream outfile;
outfile.open(&quot;car.txt&quot;, ios::app);
//determine if open was successful
if (!outfile.fail())//open was successful
{
//enter and write records
cout<<&quot;Enter First Name of Driver (or exit to quit): &quot;;
cin>>trans.name;
cin.ignore(100, '\n');


while (1)
if (strcmp(trans.name, &quot;exit&quot;) == 0)
break;
else
{
cout<<&quot;Enter have many seats in Car or Truck: &quot;;
cin>>trans.seats;
outfile<<trans.name<<'#'<<trans.seats<<endl;
cout<<&quot;Enter First Name of Driver (or exit to quit): &quot;;
cin>>trans.name;
cin.ignore(100, '\n');


}//end while

outfile.close();//close file
}
else //open was not successful
cout<<&quot;Error in opening file.&quot;<<endl;
//end if


}//end of tran function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top