Link to home
Start Free TrialLog in
Avatar of macabra
macabra

asked on

scanf problem

I'm having trouble with a scanf portion of code.  The program asks for a single character A, B, or C.  If the user enters anything other than those three choices, the program is supposed to ask them to reenter the correct character.  I used an if statement to check the character entered but the segment of code isn't working right.  It prints out the code asking for the new character twice and then goes onto the next segment of code.  Here is the portion of code causing the trouble.

printf("Enter the character>");
      scanf("%c", &dcChar);
      
      dChar = toupper(dChar);
      
      if ((dChar != 'A') || (dChar != 'B') || (dChar != 'C'))
      {
            printf("Please enter in either an A, B, or C>");
            scanf("%c", &dChar);
      }
Could you tell me what the problem is please?
Avatar of danielsonchris
danielsonchris

//change the following line:
dChar = toupper(dChar);
//TO:
dChar = toupper(dcChar);

//Now remove the OR clauses in the following line:
 if ((dChar != 'A') || (dChar != 'B') || (dChar != 'C'))
//TO:
 if ((dChar != 'A') && (dChar != 'B') && (dChar != 'C'))

Peace,
Chris
Avatar of macabra

ASKER

Okay I did the change and the second line is gone but it doesn't wait for me to enter in a new dChar.  It goes right to the next line in the program.
To let you enter a new dChar you have to code the whole thinginto a while loop

something like this logic

int done = 0;

while(!done) {
   print - enter a char
   if char satisfies  
       done = 1
  }



Avatar of macabra

ASKER

Okay the loop is working but now the double printf line is back...
paste your current code here
Avatar of macabra

ASKER

while(done == 0)
{
     if ((dChar != 'A') && (dChar != 'B') && (dChar != 'C'))
     {
          printf("Please enter a A, B, or C>")
          scanf("%c", dChar)
     }
     else
     {
          done = 1;
     }
}
ASKER CERTIFIED SOLUTION
Avatar of avizit
avizit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial