Thread: Beginner at C and not sure what I'm doing wrong here

  1. #1
    Registered User
    Join Date
    Sep 2021
    Posts
    2

    Beginner at C and not sure what I'm doing wrong here

    Hi I'm a beginner at coding c and I'm trying to complete an assignment, but I'm not sure what I'm doing wrong here. I've followed what the book said but it's not working. My scanf are working for the double and the integer in my code but not for the character. Any help would be great appreciated.
    Code:
    int main(void) {
       int    userInt ;
       double userDouble;
       // FIXME: Define char and string variables
       char userChar;
       //string userString;
       printf( "Enter integer: \n");
       scanf("%d", &userInt);
       printf( "Enter double: \n");
       scanf("%lf", &userDouble);
       printf( "Enter character: \n");
       scanf ("%c", &userChar);
       // FIXME (1): Finish reading other items into variables, then output the four values on a single line separated by a space
          printf("%d %lf %c \n", userInt, userDouble, userChar);
    
    
       // FIXME (2): Output the four values in reverse
       printf("%c %lf %d\n", userChar, userDouble,userInt);
       
       // FIXME (3): Cast the double to an integer, and output that integer
       printf("%lf cast to an integer is %lf\n", userDouble, userDouble);
    
    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2021
    Posts
    2
    Here's a screen shot of one of the issues with the output
    Beginner at C and not sure what I'm doing wrong here-issue-char-jpg
    Attached Images Attached Images Beginner at C and not sure what I'm doing wrong here-issue-char-jpg 

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,734
    To read the next non-space character, put a leading space in the scanf format.
    Like so.
    Code:
    //      v here!
    scanf (" %c", &userChar);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,351
    The problem is that when you read the double with the previous scanf call, the newline from entering the line of input remains in the input buffer, and this is then read by the scanf call with "%c". A possible workaround in your case is to change "%c" to " %c" so that the leading space will match the leftover whitespace in the input buffer. A more general solution would be to read each line of input into a string (e.g., by using fgets), then parse the string with sscanf (as in always do this instead of using scanf).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-04-2020, 11:39 AM
  2. Beginner: Is the code of the professor really wrong?
    By Placebo in forum C Programming
    Replies: 14
    Last Post: 12-30-2018, 10:37 AM
  3. Beginner: What is wrong with my code? It seems fine!
    By shivdude in forum C Programming
    Replies: 10
    Last Post: 08-03-2009, 03:36 PM
  4. Replies: 2
    Last Post: 02-21-2009, 12:11 AM
  5. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM

Tags for this Thread