Splitting char arrays

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1964 Views - Last Post: 04 April 2013 - 03:08 PM Rate Topic: -----

#16 synlight  Icon User is offline

  • D.I.C Addict

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Splitting char arrays

Posted 04 April 2013 - 02:27 PM

View Postjimblumberg, on 04 April 2013 - 04:23 PM, said:

In order to do this:
customer[i].date = getDate


Are you calling a function?

If so, how do you call a function?

How is this function returning anything? Remember you defined your function to return nothing (void).

Also you know you can't use the assignment operator= with C-strings, why are you trying to assign C-strings?

Stop, think! You have solved this problem with your other strings, do something similar.

Jim


I'm sorry. that was pseudocode.. just to show you what I planned.

When I am finished, the date will be split and converted into three integers and returned to a struct as such. I'm just trying to get the string testing done before I even attempt it. In my mind, getDate is the first in a string of functions to test the data, convert it, and return it for printing. I haven't quite wrapped my mind around how that's going to flow yet.

but.. to call a function I would use getDate();

and to put a C string into my date element I would have to use strcpy.

This post has been edited by synlight: 04 April 2013 - 02:28 PM

Was This Post Helpful? 0
  • +
  • -

#17 synlight  Icon User is offline

  • D.I.C Addict

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Splitting char arrays

Posted 04 April 2013 - 02:40 PM

Okay, I have the string part tested. Now I am ready to split the string and test it some more. Here is the algoritm I wrote this morning to test it. I doubt it's perfect, I haven't had the chance to test it yet.

int numDays = 0;
	bool monthFlag = false;
	bool dayFlag = false;
	bool yearFlag = false;

	if(month > 0 && month < 13)
	{
		monthFlag = true;

		if(month == 4 || month == 9 || month == 6 || month == 11)
			numDays = 30;

		else if (month == 2)
		{
			if (year % 4 == 0 && year % 100 != 0)
				numDays = 29;
			else if(year % 4 == 0 && year % 100 == 0 && year % 400 ==0)
				numDays = 29;
			else
				numDays = 28;
		}//END LEAPYEAR

		else 
		numDays = 31;

	}//END MONTH TEST	

	else
		printf("You have entered an invalid month");

	if(day <= numDays || day >= numDays)
		printf("You have entered an invalid day.");
	else
		dayFlag = true;

	if(year > 2013)
		printf("You have entered an invalid year.");
	else
		yearFlag = true;

	if(monthFlag == true && dayFlag == true && yearFlag == true)
		valid== true;


Now, I did some reading on strtok to split up this string.. I used strtok in Java last summer but it was different (of course). I'm going to go do some more reading and try to find some good examples. But if anyone wanted to explain it like I'm 5, I wouldn't be mad LOL.


Okay.. so I have the bones ready to test.. I am getting an error that strtok can't convert from const char (my testDate).

I just want to verify my understanding of this.

I passed testDate as a const char because I didn't want to change it. But by using strtok, I am changing it? I thought it made copies of the strings.

char* token;
	token = strtok(testDate,"/");
	while (token != NULL)
	{
		token = strtok(NULL, "/");
		printf("%s\n", token);
	}


EDIT : I DID IT!!!


I broke the string up successfully! I just hope my assumption up there was correct, about the const char LOL.

Okay.. now I have to figure out how to get strtok to put the new strings into variables... I hope you guys don't mind me doing this.. it's helping me think.

ETA: I answered my own question about the const char:

The strtok() function parses the string up to the first instance of the delimiter character, replaces the character in place with a null byte ('\0'),

This post has been edited by synlight: 04 April 2013 - 03:06 PM

Was This Post Helpful? 0
  • +
  • -

#18 synlight  Icon User is offline

  • D.I.C Addict

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Splitting char arrays

Posted 04 April 2013 - 03:08 PM

Can I use strcpy on each token? Is that the answer?

HA! It worked!!

This makes me feel better about myself LOL. nobody had to hold my hand. I had to take the strtok out of the loop though. . and do 3 individual calls.

Okay guys.. here is the code.. go ahead and rip me apart LOL, I know I probably did it in the most noob way possible...

char* token;

	//GET MONTH
	token = strtok(testDate,"/");
	strcpy(tMonth, token);
	month = atoi(tMonth);

	//GET DAY
	token = strtok(NULL, "/");
	strcpy(tDay, token);
	day = atoi(tDay);

	//GET YEAR
	token = strtok(NULL, "/");
	strcpy(tYear, token);
	year = atoi(tYear);

This post has been edited by synlight: 04 April 2013 - 03:17 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2