Thread: Scanf doesn't take in entire string

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    2

    Scanf doesn't take in entire string

    Hey guys, I was wondering if you guys could take a look at my code. Basically, it's trying to simulate Linux's Cal function, and takes in the input, "MonthName Year", where monthname is a string, and year is a number.

    The problem here is, getting the program to recoqnize monthname, so that I can turn it into the month's numberical value. (ie september = 9) But when I try to input a value, the program will not take in the entire string. It takes in part of it, 1 character, as seen by the following output:

    F\uffff-1075508756 hello
    January
    S M T W T F S
    1 2 3 4 5 6 7
    8 9 10 11 12 13 14
    15 16 17 18 19 20 21
    22 23 24 25 26 27 28
    29 30

    I am completely baffled by it, so I was wondering if you guys had any suggestions? Here's the code I am using, you can pretty much ignore everything after the section where I get the time information.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    
    int main (void)
    {	
    
    	time_t then;
    	struct tm *timeinfo;
    	struct tm *endofmonth;
    	int year, mon ,day, firstwday, count = 0, weekcount, lastday, daycount = 1;
    	char monthinput;
    	const char *months[] = {"January", "February", "March", "April", 
    				"May", "June", "July", "August", 
    				"September", "October", "November", "December"};
    
    	/* prompts user for date */
    	scanf("%s %d", &monthinput, &year);
    	
    	/*Converts user month input into an integer*/
    	while (count < 12) {
    
    		if (strcmp(months[count], &monthinput) == 0) {
    			mon = count + 1;
    			printf("%s bye \n", &monthinput);
    		}
    		count++;
    	}
    
    	printf("%s", &monthinput);
    	printf("%d hello  \n", mon);
    
    	/* gets current timeinfo and modify it to user's choice */
    	time (&then);
    	timeinfo = localtime(&then);
    	timeinfo->tm_year = year - 1900;
    	timeinfo->tm_mon = mon - 1;
    	timeinfo->tm_mday = 1;
    
    	/* call mktime: timeinfo->tm_wday will be set */
    	mktime(timeinfo);
    	firstwday = timeinfo->tm_wday;
    	weekcount = firstwday;
    
    	/*gets current timeinfo and modify it to user's choice*/
    	endofmonth = localtime(&then);
    	endofmonth->tm_year = year - 1900;
    	endofmonth->tm_mon = mon;
    	endofmonth->tm_mday = 0;
    
    	/* call mktime: timeinfo->tm_wday will be set */
    	mktime (endofmonth);
    	lastday = endofmonth->tm_mday;
    	
    	/*Prints the month*/
    	printf("%s\n", months[timeinfo->tm_mon]);
    	
    	/*Prints the calender*/
    	printf(" S  M  T  W  T  F  S\n");
    
    
    	/*Change with for loop, better integration, less declarations!*/
    	while (firstwday > 0) {
    		printf("   ");
    		firstwday--;
    	}
    
    	while (daycount <= lastday) {
    		if (weekcount == 7){
    			printf(" \n");
    			weekcount = 0;
    		} else {
    			if (daycount <= 9) {
    				printf(" %d ", daycount);
    				daycount++;
    				weekcount++;
    			} else {
    				printf("%d ", daycount);
    				daycount++;
    				weekcount++;
    			}
    		}
    	}
    	
    	printf("\n\n");
    
    }
    Last edited by DragonX2n; 01-30-2006 at 09:03 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Use a two-dimensional array.
    Sent from my iPadŽ

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Declare a string into which to read a line of text.
    Code:
    char text[BUFSIZ];
    Declare a string for the monthinput.
    Code:
    char monthinput[15];
    Read a line of data using fgets.
    Code:
    fgets(line, sizeof line, stdin);
    Parse the line of text with sscanf.
    Code:
    sscanf(line, "%14s %d", monthinput, &year);
    Checking return values of fgets and sscanf is beneficial. Look up these functions in the manual and/or help pages.

    [edit]And then remove the unnecessary &'s in the strcmp and printfs where the compiler complains.
    Last edited by Dave_Sinkula; 01-30-2006 at 11:06 PM. Reason: D'oh! Distractions, distractions...!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    See the FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    scanf() considers whitespace to be a separater, so entering a string like "Hello World" will only retrieve "Hello" (as you've found out by now).
    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.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    2
    Quote Originally Posted by Dave_Sinkula
    Declare a string into which to read a line of text.
    Code:
    char text[BUFSIZ];
    Declare a string for the monthinput.
    Code:
    char monthinput[15];
    Read a line of data using fgets.
    Code:
    fgets(line, sizeof line, stdin);
    Parse the line of text with sscanf.
    Code:
    sscanf(line, "%14s %d", monthinput, &year);
    Checking return values of fgets and sscanf is beneficial. Look up these functions in the manual and/or help pages.

    [edit]And then remove the unnecessary &'s in the strcmp and printfs where the compiler complains.
    This solution worked great! Thank you very much. Was the source of error because I did not allot enough space?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    char monthinput;
    /* ... */
    scanf("%s %d", &monthinput, &year);
    A non-empty string will always be more than one character. So if monthinput is only a single character, using a string specifier to read input will be a problem. In short, yes; but there is more to it as well.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM