Help with loop

Hi all,
I have created a program which I would like to to display the error message as specified by printf and not end with it.I would like to program to ask for the user's input again if the user inputs a invalid option or inputs an alphabet.
I would be really grateful if anyone could help me with it and give me a basic idea.thank you.



#include
int main ()
{
char c;
printf ("press 1 for breakfast.
press 2 for lunch.
press 3 for dinner.
press 4 to quit.
");
c=getchar ();
if ((c>='a') && (c<='z') || (c>='A') && (c<='Z'))
printf ("no alphabets allowed.
");
switch (c)
{
case '1':
printf ("breakfast.
");
break;
case '2':
printf ("lunch.
");
break;
case '3':
printf ("dinner.
");
break;
case '4':
printf ("thank you.
");
break;
default:
printf ("not a valid option.
");
}
return (0);
}

Comments

  • : Hi all,
    : I have created a program which I would like to to display the error
    : message as specified by printf and not end with it.I would like to
    : program to ask for the user's input again if the user inputs a
    : invalid option or inputs an alphabet.
    : I would be really grateful if anyone could help me with it and give
    : me a basic idea.thank you.
    :
    :
    :
    : #include
    : int main ()
    : {
    : char c;
    [color=Red]: int ok=0;
    : while (ok==0)
    : {[/color]
    : printf ("press 1 for breakfast.
    press 2 for lunch.
    press 3 for
    : dinner.
    press 4 to quit.
    ");
    : c=getchar ();
    : if ((c>='a') && (c<='z') || (c>='A') && (c<='Z'))
    [color=Red]: {[/color]
    : printf ("no alphabets allowed.
    ");
    [color=Red]: continue;
    : }[/color]
    : switch (c)
    : {
    : case '1':
    : printf ("breakfast.
    ");
    [color=Red]: ok=1;[/color]
    : break;
    : case '2':
    : printf ("lunch.
    ");
    [color=Red]: ok=1;[/color]
    : break;
    : case '3':
    : printf ("dinner.
    ");
    [color=Red]: ok=1;[/color]
    : break;
    : case '4':
    : printf ("thank you.
    ");
    [color=Red]: ok=1;[/color]
    : break;
    : default:
    : printf ("not a valid option.
    ");
    : }
    [color=Red]: }[/color]
    : return (0);
    : }
    :
    :
  • Thank you AsmGuru62.Your modification helped me lot.This gave me the idea how to proceed from here and gave me the insight that I still have a very long way to go.
    Thank you for your precious time.
    May you have a good life.
    GOD bless
    P.S:Would you be kind enough to recommend a good book for C basic programming and C advance level programming (would keep the advance level book to refer after getting good at basic level).I am self taught,so no master to lead the way,books are my only option.Hope you will understand.
    Thanks once again.
  • [color=Blue]This online book covers most of C language with examples.
    [link=http://www.iu.hio.no/~mark/CTutorial/CTutorial.html]http://www.iu.hio.no/~mark/CTutorial/CTutorial.html[/link]
    [/color]
  • Thank you.
    May you have a long and good life.
    GOD bless.
  • Another thing you might do to lessen the effort:
    When checking for alphabetic characters, just #include
    and then instead of
    if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')

    you can have
    if (isalpha(c))

    -- same result
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories