Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Geekibz
    New Member
    • Jan 2008
    • 12

    #1

    Validation

    Hi.Am Using switch -case statements (see below)and i have cases 1-5 .If the user inputs a character I want an error message printed out prompting the user to re enter his\her choice.how do i get that done?pls help

    int choice;
    .......
    ........
    scanf("%d",&cho ice)
    switch(choice){
    case 1:printf(....
    ......
    break;
    case 2:.....
  • IanWright
    New Member
    • Jan 2008
    • 179

    #2
    Originally posted by Geekibz
    Hi.Am Using switch -case statements (see below)and i have cases 1-5 .If the user inputs a character I want an error message printed out prompting the user to re enter his\her choice.how do i get that done?pls help

    int choice;
    .......
    ........
    scanf("%d",&cho ice)
    switch(choice){
    case 1:printf(....
    ......
    break;
    case 2:.....
    You need to set a default:

    [CODE=cpp]

    switch(choice)
    {
    case 1: ...
    break;

    case 2: ...
    break;

    default:
    // printf or cout to display error message
    break;
    }
    [/CODE]

    Then I'd suggest using a flag, and stick it in a while loop... So create a boolean (validCharacter ), set it to true if they are in case 1-5 and false if the default is reached. Then your while loop needs to be based upon this condition.. eg..

    [CODE=cpp]
    while(!validCha racter)
    {

    }
    [/CODE]

    Comment

    Working...