help with number menu

This is a discussion on help with number menu within the C Programming forums, part of the General Programming Boards category; Hey people, I m having trouble with some stuff with using c program..i want to calculate some number but i ...

  1. #1
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14

    help with number menu

    Hey people, I m having trouble with some stuff with using c program..i want to calculate some number but i want them to write some detail as well such as first name initial and last name and so some other question(but i don't need you to help me on that because i already know how to do it)
    i want to do a number menu on my program so that when you click the option number it will bring you to the question and when you finished with the question it will promp back to the menu number...
    I tried using the boolean and use for loop because i know how many loop i need and how many question i have...
    but the problem is i don't know to promp it back to the menu number and also if i have already enter question number 1 i wanted to be so that i can't go to question 1 again and the last problem is that if i don't enter all the answer and they wanted to get the result of the calculation it will have an error message...
    Thank you

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I'd create an 2d array of answers, where the 1st dimension is the answer, and the 2nd dimision is a flag specifing if they have answered the question.

    The rest sounds pretty logical...

  3. #3
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14
    ohh by the way if you are curious with the full extent of the question here it is

    1) Your code will need to display a numbered menu and
    loop getting all required information so as to be able
    to calculate the BMI:
    1) Please enter blah blah
    2) Please tell me......
    3) :
    4) :
    Please enter your menu number....
    2) As it loops, your code will need to accept several
    pieces of required information through the menu system:
    a) If ? is entered, a usage or help message for this
    program will be printed and the program will finish
    with no other processing.
    b) If Q is entered, the menu system looping will finish
    and the BMI will be printed out for this individual
    as specified below.
    c) If a single digit is entered, the menu system will
    input and process the required information after
    prompting for it. (See example menu above).
    The information required for the menu item single
    digit is:
    1) Prompt and get the person's integer age in years.
    This age should be validated for reasonable ages.
    2) Prompt and get the person's real weight and height
    (in that order). The weight and height should be
    validated for reasonable values.
    These values are metric in kilograms and
    centimetres.
    3) Prompt and get the initial of the person's first
    name. This initial must be an uppercase letter,
    or an error message should be printed out and
    the program finishes.
    Any initial space characters may be skipped.
    4) Prompt and get the person's last name. You should
    declare a char array to hold this name and its
    size is 20 characters. You must ensure that there
    is enough room in the array to hold the lastname.
    Any initial space characters may be skipped.
    5) Prompt and ask if the person is male or female.
    They can respond with either an 'm', an 'f' or
    a '-' to signify the result.
    Error and finish if not 'm', 'f' or '-'.


    3) If the user chooses the same menu number twice, an
    error message is issued and the program finishes.
    4) The user may choose a menu number in any order.
    5) If the user chooses Q before all required information
    is obtained, then an error message is issued and
    the program finishes.
    6) The normal output is something along the lines of:
    initial last_name is male and is X years old, is such and such a weight
    and height, and has a BMI of xxx. This is a Weight Status of yyy.
    The height must be specified to 0 decimal places,
    the weight must be specified to 2 decimal places.
    Choose an informative and professional output format for the entire output!
    7) Your code must contain your development of the algorithm
    from english to pseudo-code as comments at the start of
    the program.
    Each refinement must show a logical progression of your
    development.
    Your final pseudo-code must conform to that in the
    course notes.
    8) Your C code style must conform to that shown in the
    course notes.

  4. #4
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14
    im really really new to c program so i haven't done much array, but i m getting there soon, maybe in 2 or 3 weeks time..

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Not a tiny bit interested in your homework. Try it yourself, and ask specifically where you get stuck with code examples where required.

  6. #6
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14
    ohh never mind if you miss understood me....
    my question was the at the first post, the third post is just that if you don't really understand what i mean then i m giving the full extent of my question...
    ok but maybe you can help me with this one then...
    if i want to make a number menu, would you suggest i should use boolean or while loop or for loop??

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What? If you want a numbered menu, unless your numbers only consist of one and zero, why would you use a boolean?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14
    well i was thinking of declaring all my variable name to false then every time some one enter the menu the eg if i enter 1, the the false become true and i won't be able to re-enter 1 on the number menu again....thats how its suppose to work..but i'm missing some thing and i m not sure...

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Without code it's like searching for a moth in a dark room...

  10. #10
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14
    ok here is my code so far
    Code:
    /*This program converts your Mass and Height into Body Mass Index(BMI).*/
    
    int main(int argc, char ** argv)
    {
    // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    //Declareation of Variables.
    double Kilograms;
    int CentiMetres;
    double BMI;
    int Age;
    char * FName[20];
    char * LName[20];
    char * Gender[1];
    
    	fprintf(output,"1 = Enter Your First Name Initial? \n"); fflush(output);
    	fprintf(output,"2 = Enter Your Last Name?\n") fflush(output);
    	fprintf(output,"3 = Enter Your Age?\n") fflush(output);
    	fprintf(output,
    
    //******************************************************************
    	//Enter first name.
    	fprintf(output,"--------------------------\n"); fflush(output);
    	fprintf(output,"Enter Your first name: "); fflush(output);
    
    	//input first name.
    	fscanf(input,"%s",&FName);
    	
    //******************************************************************
    	//Enter last name.
    	fprintf(output,"--------------------------\n"); fflush(output);
    	fprintf(output,"Enter Your last name: "); fflush(output);
    
    	//input last name.
    	fscanf(input,"%s",&LName);
    	
    //******************************************************************
    	//Enter the Age.
    	fprintf(output,"--------------------------\n"); fflush(output);
    	fprintf(output,"Enter Your Age Between 18 to 100: "); fflush(output);
    
    	// input age.
    	fscanf(input,"%d",&Age);
    
    	// Reasonable value for age.
    	while ((Age < 18 )||( Age > 100))
    	{
    		//This error message will appear if the number enter in not with in the range.
    		fprintf(output,"--------------------------\n"); fflush(output);
    		fprintf(output,"Error\nYour Age Must be Between 18 to 100: ");fflush(output);
    		fscanf(input,"%s",&Age);
    	}
    
    //******************************************************************
    	//Enter Gender
    
    	fprintf(output,"--------------------------\n"); 
    	fprintf(output,"Enter Your Gender (M/F): \n"); fflush(output);
    
    	//input Gender.
    	fscanf(input,"%s",&Gender);
    
    	// If Gender its not M/F/-,then error message should appear.
    	while (Age !=(M)||Age !=(F)||Age !=(-))
    	{
    		//This error message will appear if the number enter in not within the range.
    		fprintf(output,"--------------------------\n"); fflush(output);
    		fprintf(output,"Gender must be M(male)/F(female)/-: ");fflush(output)
    		fscanf(input,"%d",&Gender);
    	}
    
    //******************************************************************
    	// Enter Mass in Kilograms.
    	fprintf(output,"--------------------------\n"); fflush(output);
    	fprintf(output,"Enter weight in kilograms: "); fflush(output);
    
    	// input Mass Kilograms. 
    	fscanf(input,"%lf",&Kilograms);
    
    	//Reasonable value for Weight.
    	while ((Kilograms < 25) || (Kilograms > 230))
    	{
    		//This error message will appear if the number enter in not within the range.
    		fprintf(output,"--------------------------\n"); fflush(output);
    		fprintf(output,"Error\nYour Weight must be in between 25 to 230 ",Kilograms);
    		fscanf(input,"%lf",&Kilograms);
    	}
    
    
    //******************************************************************
    	// Enter height in CentiMetres.
    	fprintf(output,"--------------------------\n"); fflush(output);
    	fprintf(output,"Enter Height in Centimetres: "); fflush(output);
    
    	// input height CentiMetres.
    	fscanf(input,"%d",&CentiMetres);
    {
    	//Reasonable Value for Height.
    	while ((CentiMetres < 70) || (CentiMetres > 220))
    	{
    		//This error message will appear if the number enter in not within the range.
    		fprintf(output,"--------------------------\n"); fflush(output);
    		fprintf(output,"Error\nYour Height must be in between 70 to 220 Cm: ",CentiMetres);
    		fscanf(input,"%d",&CentiMetres);
    	}
    }
    
    //******************************************************************
    	// Calculation to get BMI.
    	fprintf(output,"--------------------------\n"); fflush(output);
    	BMI = ( Kilograms  / ( CentiMetres * CentiMetres ) * 10000);
    
    //******************************************************************
    	// output results.
    	fprintf(output,"%s",FName);
    	fprintf(output," %s",LName);
    	fprintf(output," is %c",Gender);
    	fprintf(output," and is age %d",Age);
    	fprintf(output," and weight %lf",Kilograms);
    	fprintf(output," Kg and %d",CentiMetres);
    	fprintf(output," Cm Tall and has BMI of %.2f",BMI);
    
    //******************************************************************
    	if (BMI<18.50)
    	{	
    		fprintf(output," This is a Weight status of Underweight\n",BMI);
    	}
    	else if (BMI<24.99)
    	{	
    		fprintf(output," This is a Weight status of Normal weight\n",BMI);
    	}
    	else if (BMI<29.99)
    	{	
    		fprintf(output," This is a Weight status of Overwright\n",BMI);
    	}
    	else
    	{	
    		fprintf(output," This is a Weight status of Obese\n",BMI);
    	}
    }

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by j4k50n View Post
    well i was thinking of declaring all my variable name to false then every time some one enter the menu the eg if i enter 1, the the false become true and i won't be able to re-enter 1 on the number menu again....thats how its suppose to work..but i'm missing some thing and i m not sure...
    Menu's may be written with endless loops (either while(1) or for(; ), which will have a break statement inside them.

    Code:
    while (1)  {
       print your menu selections
       then get you user's choice
       
       switch (choice) {
           case 0: break (out of the menu loop
           case 1: if (!done[choice]) {
                          handle choice #1. Usually by calling the choice1 function to keep your switch
                          statement something reasonably small.
                       } else
                            print "Already Chosen".
                        break (unless you want case 1 control to fall down into case 2)
           case 2: handle choice #2 same as #1, basically.
                       break       
           Ditto for the rest of your choices on the menu.
    }
    You might also have a small int array called "done[# of choices on your menu]". If number 1 on your menu has been chosen, you set done[1] = 1; Which is very handy, because after you set done[all] = 0, now you know which menu choices have already been chosen by:

    Code:
    if (!done[choice]) 
        do your regular case stuff, it hasn't been chosen yet.
    else 
        just print "Done" next to that menu choice.
    It's just like a boolean value, but you don't have to muck around with bools. 0 == not done, 1 == done.

    Adak

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > fprintf(output,"2 = Enter Your Last Name?\n") fflush(output);
    Does this even compile?
    There's all sorts of broken syntax in this post.

    Read this post and build your code up in a methodical manner, then post when you first get stuck.

    > fscanf(input,"%d",&Age);
    > ...
    > fscanf(input,"%s",&Age);
    printf and scanf do not tolerate sloppy parameter passing at all. What is worse, very few compilers can check this (gcc is one of them), so you only get to find out about your mistake when you're staring at a crash.

    All your declarations for reading a string are wrong as well.

    If you're using a gcc port, then use these flags
    gcc -W -Wall -ansi -pedantic -O2 prog.c
    They will (amongst other things) tell you when you've made a mess of calling printf/scanf type functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14
    some of them does not compile because i haven't finished with it, like the first four fprintf is just starting to work on it...but haven't finished,and the gender part is also not finished, but coming to finishing, but other than that, the rest work fine...thx for the help so far, but if you want to add more idea then please do so...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number Guessing
    By blacknapalm in forum C Programming
    Replies: 2
    Last Post: 10-01-2008, 01:48 AM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21