problems with scanf/if statement plz help

This is a discussion on problems with scanf/if statement plz help within the C Programming forums, part of the General Programming Boards category; Hey Im new to programming and Im trying to creat a program, you should be able to see what Im ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    53

    Smile problems with scanf/if statement plz help

    Hey Im new to programming and Im trying to creat a program, you should be able to see what Im trying to do with this sample code I have included. I know theres something wrong with my if statement but I cant figure it out... would appreciate the help guys thanx.

    Heres my code
    insert
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char choice;
    	
    	printf("Type your choice, either A, B or C\n");
    	scanf("%c", &choice);
    	if (choice = "%c", A)
    		printf("Your choice was A");
    	if (choice = "%c", B)
    		printf("Your choice was B");
    	if (choice = "%c", C)
    		printf("Your choice was C");
    	if (currency != A, B, C)
    		printf("You did not choose A, B or C");
    
    	return 0;
    }
    Thanx again everyone

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Probably want
    Code:
    if (choice == 'A') ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    The syntax for IF is:
    if (expression [comparison] condition)
    Expression is typically a variable, in your case "choice".
    Comparison is an operator for comparison, such as == (equal to), != (not equal to), >, <, and so on.
    And condition is what you want it to be compared against (the contents of the variable).
    Characters must be surrounded with '' (single quotes) and strings must be surrounded with "" (double quotes).

    = is used for assigning and not comparing. Your code should be:

    Code:
    	if (choice == 'A')
    		printf("Your choice was A");
    	else if (choice == 'B')
    		printf("Your choice was B");
    	else if (choice == 'C')
    		printf("Your choice was C");
    	else
    		printf("You did not choose A, B or C");
    And since you only want one condition to be true, it's better to use if and else if. That means it won't try the rest of the conditions if the one above is true. Basically if "choice equals A, if not, then if it equals B", and so on.
    The last one can simple be "else" to indicate "if none of the above conditions were true".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    62
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    int main()
    {
    	char choice;
    	
    	printf("Type your choice, either A, B or C\n");
    	scanf("&#37;c", &choice);
    	if (choice == 'A')
    		printf("Your choice was A");
    	else if (choice ==  'B')
    		printf("Your choice was B");
    	else if (choice == 'C')
    		printf("Your choice was C");
    	
    	else 
    		printf("You did not choose A, B or C");
    
        getch();
    	return 0;
    }
    Last edited by joker_tony; 03-21-2008 at 04:46 PM.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    you should use code tags when posting
    and - better remove conio.h and getch as they are not standard
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    hi dezz101

    you can do it simply by using Switch statement.


    Code:
    #include <stdio.h>
    
    int main()
    {
    	char choice;
    	
    	printf("Type your choice, either A, B or C\n");
    	scanf("&#37;c", &choice);
    
    
    	switch(choice){
    	case 'A':
    	printf("Your choice was A");
    	break;
    
    	case 'B':
    	printf("Your choice was B");
    	break;
    	
    	case 'C':
    	printf("Your choice was C");
    	break;
    
    	default:
    	printf("You did not choose A, B or C");
    	break;
    }
    	
    
    	return 0;
    }
    Last edited by Anuradh_a; 03-21-2008 at 10:58 AM.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    Thank you all!, it worked fine much appreciative
    especially Elysia you explained it beautifully

    One last querie though, at the end of my program I need it to say "Press [Enter] to exit..." and then the user presses enter andit exits but I cant figure out how to do it iv tried":

    printf("Press [Enter] to exit");
    fflush(stdin);
    scanf("\n");
    Return 0;
    }

    the \n is all I can think of to get it to work but is obviously doesnt

    Thanks again for the help and not mocking me lol cheers.
    Last edited by dezz101; 03-25-2008 at 06:31 PM. Reason: just needed to add something

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Hi how it ip possible to do this
    Code:
    char choice;
    scanf("%c",&choice);
    i think it is not the correct way to use scanf with character data type...

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    fflush(stdin);
    do not flush stdin - read FAQ

    also read FAQ "How can I stop my console window from disapearing"

    do not forget that the previous scanf("&#37;c") leaves the \n char in the input stream

    Hi how it ip possible to do this
    as it is written. What is your problem with it?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    I know how to make it pause and wait for a character, but I need it to print: "Press [Enter] to exit" then pause and the user only needs to press enter to exit can you show me how plz. heres my new code

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char choice;
    	
    	printf("Type your choice, either A, B or C\n");
    	scanf("%c", &choice);
    	if (choice == 'A')
    		printf("Your choice was A");
    	else if (choice == 'B')
    		printf("Your choice was B");
    	else if (choice == 'C')
    		printf("Your choice was C");
    	else
    		printf("You did not choose A, B or C");
    	printf("Press [Enter] to exit");
    	scanf("\n");
    	return 0;
    }
    That bloody last scanf is really annoying me lol. I just want to press enter and the program will exit!!!!! please can someone copy my code and then fix it the show me thanx again.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    what do you think the scanf should do?
    I know how to make it pause and wait for a character
    So do it instead of your strange scanf
    you may want to add fflush(stdout) after printf
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dezz101 View Post
    Thank you all!, it worked fine much appreciative
    especially Elysia you explained it beautifully

    One last querie though, at the end of my program I need it to say "Press [Enter] to exit..." and then the user presses enter andit exits but I cant figure out how to do it iv tried":

    printf("Press [Enter] to exit");
    fflush(stdin);
    scanf("\n");
    Return 0;
    }

    the \n is all I can think of to get it to work but is obviously doesnt

    Thanks again for the help and not mocking me lol cheers.
    fflush(stdin) is guaranteed to NOT work in most compilers. Check the FAQ for "Why I should not use fflush(stdin)" and "How to discard unwanted input".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    53

    last problem

    OK all is working fine I did a lil research and the getchar () function has allowed me to wait for the user to press enter b4 exiting. However I think the program would be better if, if the user did not enter a valid choice it would still printf("You did not choose A, B or C"); but then it would loop back to line 7 (printf("Type your choice, either A, B or C\n")

    I know I could do it by just copy and pasting a heap of the same block of code but Im sure theres a better way to do it, plz could someone show me a easy way to do it and explain it thanx.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char choice;
    	
    	printf("Type your choice, either A, B or C\n");
    	scanf("%c", &choice);
    	if (choice == 'A')
    		printf("Your choice was A");
    	else if (choice == 'B')
    		printf("Your choice was B");
    	else if (choice == 'C')
    		printf("Your choice was C");
    	else
    		printf("You did not choose A, B or C");
    	fflush(stdin);
    	getchar ();
    
    	return 0;
    }

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Don't use fflush(stdin)! That's what everyone is telling you, so maybe it's true, right? Here are some links that you should read (some of which, I should mention, have already been mentioned):
    Last edited by dwks; 03-30-2008 at 03:00 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    Alright I didnt need that and it still works, its just that thats all I been tought so far Im sure Il be tought why I shouldnt use it later on but it works with my real program.

    but could we plz consentrate on the looping as i described before that is what I really need. thanx

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char choice;
    	
    	printf("Type your choice, either A, B or C\n");
    	scanf("&#37;c", &choice);
    	if (choice == 'A')
    		printf("Your choice was A");
    	else if (choice == 'B')
    		printf("Your choice was B");
    	else if (choice == 'C')
    		printf("Your choice was C");
    	else
    		printf("You did not choose A, B or C");
    	getchar ();
    
    	return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 08:13 AM
  2. Problems with GetAsyncKeyState
    By blurrymadness in forum C++ Programming
    Replies: 13
    Last Post: 04-21-2007, 06:13 PM
  3. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  4. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  5. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM

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