Sign in to follow this  
Followers 0
Sharpienero

[c] Using scanf, ignore seperating characters

9 posts in this topic

Hey everyone,

 

I'm curios as to if there is a way to make scanf check for two variables. For instance:

scanf("%d/%d/%d", &m,&day,&year);

checks for the date in only mm/dd/yy. If someone puts in 10/10/2015, this will return October 10th, 2015.

 

However, if I use mm-dd-yyyy, and enter 10-10-2015, it returns October 0, 0

 

Is there a work around using scanf?

 

@@BraXi, @

0

Share this post


Link to post
Share on other sites

disclaimer: i should be fookin sleeping now and i cba to test it myself

disclaimer2: i didn't even read your 1st post and appears i have coded an useless snippet :okay:

 

edit3: :dave:

1

Share this post


Link to post
Share on other sites

edit:

tthis will hopefuly generate very little errors and do the job

7yqoJfc.png

 

This was my initial test.  :okay:

 

Thank you for your assistance. I'll try to work out the bugs.

1

Share this post


Link to post
Share on other sites

Thank you for your assistance. I'll try to work out the bugs.

No need to! :awesome:

 

> had to wake up and pee

> got tempted to see how shitty my code was

> it was shitty shit, indeed

> couldnt sleep

> fixed my shit

:davesir:

 

here's the fixed and tested code :dave:

#include <string>
#include <stdio.h>

void todate(char *line)
{
        
	static const char *months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

	int date[3]; // day, month, year
	char tokens[3][32];
	int i, x = 0;
	int token = 0;

	memset(&tokens, 0, sizeof(*tokens)); //initialize char arrays as they're most likely populated with trash

	for (i = 0; i != strlen(line)+1; i++) //we're iterating through the whole line char by char to find delimeters, either - or /
	{
                if( token >= 3 ) break; // we're out of dd/mm/yy
		char ch = line[i];
		if (ch == '-' || ch == '/') // found delimeter
		{
			tokens[token][x] = NULL; //null termination is a good practise :>
			token++;
			x = 0;
			continue;
		}
                if( x >= 32 ) continue; // make sure we don't cause segfault due to OOB len
		tokens[token][x] = ch; 
		x++;
	}

	for (i = 0; i != 3; i++) // go through the raw date[]
	{
		char *str = tokens[i];
		date[i] = atoi(str); // convert string to integer

		if (i != 1) continue; //not a month so go back
		for (int j = 0; j != 12; j++) // loop through 12 months to see if month was written with letters
		{
			if (_stricmp(str, months[j]) == 0) //match, strcasecmp() on unix, we assign integer for a given month name
			{
				date[1] = j+1;
				break;
			}
		}
		if (date[1] > 12 || date[1] < 0) //make sure we've got correct month
			date[1] = 1;
	}

	printf("input: %s\n", line);
	printf("day: %i\nmonth: %s\nyear: %i\n\n", date[0], months[date[1]-1], date[2]);
}

int main()
{
	todate("12/12/2015");
	todate("12-5-2015");
	todate("12/8-2015");
	todate("12/august/2015");

	system("pause");
	return 0;
}

and here's the noice output:

 

input: 12/12/2015

day: 12

month: December

year: 2015

input: 12-5-2015

day: 12

month: May

year: 2015

input: 12/8-2015

day: 12

month: August

year: 2015

input: 12/august/2015

day: 12

month: August

year: 2015

 

ps. I've left you a few small bugs and one perf issue, that's an exercise for you to find them :dave: bear the nub will bitch about them as soon as he reads this post :troll:

2

Share this post


Link to post
Share on other sites

I don't know what the errors were. I assume they were something to do with atoi.. But I fixed that rather quickly. Thanks again for your help!

 

Edit: Here is my final code (thanks to BraXi):

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
	char word[50];
	puts("Please enter the date to convert:");
	fgets(word, sizeof(word), stdin);
	toDate(word);
}

void toDate(char *line) {
        
	static const char *months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

	int date[3]; // day, month, year
	char tokens[3][32];
	int i = 0, j = 0, x = 0;
	int token = 0;

	memset(&tokens, 0, sizeof(*tokens)); //initialize char arrays as they're most likely populated with trash

	for (i = 0; i != strlen(line)+1; i++) //we're iterating through the whole line char by char to find delimeters, either - or /
	{
                if( token >= 3 ) break; // we're out of dd/mm/yy
		char ch = line[i];
		if (ch == '-' || ch == '/') // find delimeter
		{
			tokens[token][x] = NULL;
			token++;
			x = 0;
			continue;
		}
                if( x >= 32 ) continue; // make sure we don't cause segfault due to OOB [out of bounds] len
		tokens[token][x] = ch; 
		x++;
	}

	for (i = 0; i != 3; i++) // go through the raw date[]
	{
		char *str = tokens[i];
		date[i] = atoi(str); // convert string to integer

		if (i != 1) continue; //not a month so go back
		for (j = 0; j != 12; j++) // loop through 12 months to see if month was written with letters
		{
			if (strcasecmp(str, months[j]) == 0) //match
			{
				date[1] = j+1;
				break;
			}
		}
		if (date[1] > 12 || date[1] < 0) //make sure we've got correct month
			date[1] = 1;
	}

	printf("Input: %s", line);
	printf("Converted: %s %i, %i\n", months[date[1]-1], date[0], date[2]);
}

And output: http://i.imgur.com/XizZPo6.gifv

0

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  
Followers 0