Thread: printing a string problem

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    7

    printing a string problem

    Hello All,

    I've been working on a program but one very "simple" part has turned into a stumpper. I am trying to take input from the user and assign it to a variable. Then I put in a print date statement to see if it was working right and it's not. Here's my code, any help would be great!

    Code:
    #include <stdio.h>
    
    char DATE;
    
    
    void main(void)
    {
    
    	printf("test program...PRINT THE DATE!!!\n\n");
    
    	printf("Please enter the DATE the last Monday (MM/DD/YY): ");
    	scanf("%c", &DATE);
    
    	printf("\n");
    	printf("----------------------------------------------------------------\n");
    
            printf("%c", DATE);
    
    	fflush(stdin);
    	getchar();
    
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >char DATE;
    There's no need to make your variable global, and names with all capital letters are generally only used for global constants and macros.

    >void main(void)
    Big no-no, main only returns int, anything else is incorrect. At the end of the function you can return 0 to indicate success.

    >scanf("%c", &DATE);
    You'll only read the first character of the date entered, you probably want a string.

    >fflush(stdin);
    flushing input streams is undefined, don't be surprised if anything under the sun happens when you run your program.

    Try this instead:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char date[9];
      
      printf ( "test program...PRINT THE DATE!!!\n\n" );
      printf ( "Please enter the DATE the last Monday (MM/DD/YY): " );
      scanf ( "%8s", date );
      printf ( "\n--------\n" );
      printf ( "%s\n", date );
      /* Discard unread input */
      while ( getchar() != '\n' )
        ;
      getchar();
      
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    7
    Oh cool! Thanks for your help Prelude!

    Explaining why I used void main (void): I just started programming C and I am taking a class at the local college to learn it (college classes are free for highschool students...w00t). The teacher of the class told us to use the above form until later on (I think the second half of the year). So, thats why I did it but I will definetly start using int main(void). As for the variable being global and in uppercase letters...it's just my preference since I am not writing 3 million line codes yet.

    Thanks again for the help!

    Ross

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The teacher of the class told us to use the above form until later on
    Not the brightest move on your teacher's part, but I can understand more or less the reasoning for it. Do what your teacher says, but keep in mind that void main is quite wrong and always has been.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. string problem
    By INeedSleep in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2007, 11:52 PM
  3. convert double to string problem
    By gandalf_bar in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2004, 05:14 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM