Thread: please help, quick question.

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    9

    please help, quick question.

    Code:
    How do I get the scanf statement to read a character from user input?.A for ascending and D for descending.....It doesn't error but its not working properly.......Is there something wrong with my if statement maybe?
    
    Thanks for any help.
    
    
            char x[1];
    	
    	int c; 
    
    
           printf("\nEnter a to sort in ascending order.\n");
    	printf("Enter d to sort in descending order.\n");
      	
    	for(c = 1; c <= 1; c++){
               scanf("%s",&x[c]);}
       
    		
    		if (x[c] == 'a'){
    		bubble(a, SIZE, ascending);
    		printf("\nNumbers in ascending order.\n");
    		}
    		else{
    		bubble(a,SIZE,descending);
    		printf( "\nNumbers in descending order.\n");
    		}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    &#37;c reads in a single character.

    Note that in your example, x[1] does not exist despite your attempts to read into it.

    Edit: Which of course then brings up the question of why you have an array at all....

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Code:
     
        I tried this also, but it didn't work......Any answers.?
    
    
    
    
    
          char x;
    	
    	int c; 
    
    
           printf("\nEnter a to sort in ascending order.\n");
    	printf("Enter d to sort in descending order.\n");
      	
    	{
               scanf("%s",&c);}
       
    		
    		if (c == 'a'){
    		bubble(a, SIZE, ascending);
    		printf("\nNumbers in ascending order.\n");
    		}
    		else{
    		bubble(a,SIZE,descending);
    		printf( "\nNumb
    ers

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Try reading the answer again.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    It goes straight to descending order when I input a..Instead of descending order.Maybe a problem further into program?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Nevermind, I got it......Thanks for your help bro.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Try reading the answer again. And also note that "int c" is not the same as "char c".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM