Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

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?
0
macabra
Asked:
macabra
  • 3
  • 3
1 Solution
 
danielsonchrisCommented:
//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
0
 
macabraAuthor Commented:
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.
0
 
avizitCommented:
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
  }



0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
macabraAuthor Commented:
Okay the loop is working but now the double printf line is back...
0
 
avizitCommented:
paste your current code here
0
 
macabraAuthor Commented:
while(done == 0)
{
     if ((dChar != 'A') && (dChar != 'B') && (dChar != 'C'))
     {
          printf("Please enter a A, B, or C>")
          scanf("%c", dChar)
     }
     else
     {
          done = 1;
     }
}
0
 
avizitCommented:
try

scanf(" %c", &dChar) ;

note there is a "space"  in front of the %c  and a & in fromt of dChar


from http://webcourse.cs.technion.ac.il/234117/Spring2004/ar/faq_Homework.html

I'm having a problem using scanf. After I enter the username, the following scanf reads the "enter" I clicked. What should I do?


Instead of writing scanf("%c") you can write scanf(" %c") (with a space before %c), and it should work better. The space before the "%c" will read all white spaces (newline, tab and space) and the first non-white space will be read into the "%c". Using the scanf this way will prevent the scanf from reading the "enter".
            
      

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now