Splitting char arrays

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

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

Splitting char arrays

Posted 03 April 2013 - 06:51 PM

I need to split a date into its various parts (month/day/year).

I'm used to using substr on strings, can I use it on char[]? Or will it give me odd results/errors?

I've read a little bit about strtok.. and memcopy.. don't really understand either one fully, but if you guys can point me in the direction I need to go, I will do the research.

Thank you, as always :)

Is This A Good Question/Topic? 0
  • +

Replies To: Splitting char arrays

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4019
  • View blog
  • Posts: 12,408
  • Joined: 25-December 09

Re: Splitting char arrays

Posted 03 April 2013 - 07:45 PM

I recommend using a string, if possible, and then using a stringstream to process that string.

If you must use a C-string then you'll need to use strtok() (yuck).

Jim
Was This Post Helpful? 0
  • +
  • -

#3 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: Splitting char arrays

Posted 04 April 2013 - 04:18 AM

Yuck Indeed.

I have to use a char array.

I am trying to work through this today, I'm using a lot of techniques I haven't had to use before. These C strings are the bane of my existence.

Here is my algorithm..
1. Get date as string, pass to validDate
2. Test for '/' at position 2 and 5 (date MUST be in format 04\04\2013 if it has zeros in month/day).
3. Test for leading zeros or one in position 0 and 3
4. Split the string into month, day, and year.
5. Cast each to int
6. Check for leap year and such.
7. After date is fully validated, pass the ints back to my date struct, which looks like this:
struct dueDate
	{
		int month;
		int day;
		int year;
	};


SO.. here is what I have so far:

void getDate()
{
	char testDate[20];
	bool valid = false;

	while(!valid)
	{
		getString("Please enter the date as XX/XX/XXXX:", testDate, 20); 
		validDate(testDate);
	}

}



bool validDate(char testDate[])
{
	int month = 0;
	int day = 0;
	int year = 0;
	
	if(testDate[0] != '1' || testDate[0] != '0')
	{
		printf("Invalid month");
		return false;
	}

	if(testDate[2] != '/' && testDate[5] != '/')
	{
		printf("Months and days need 2 digits");
		return false;
	}


}


I cannot use strcmp, because I am just testing portions of the string, is that correct?

I did some reading on strtok but I'm just not there yet.. I'm going to keep looking at examples.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: Splitting char arrays

Posted 04 April 2013 - 05:29 AM

Your test:
testDate[0] != '1' || testDate[0] != '0'


is always going to be true. You'll need to fix the logic because consider the following:
if testDate[0] == '1', then testDate[0] != '0' is going to be true.
if testDate[0] == '0', then testDate[0] != '1' is going to be true.
if testDate[0] == 'x', then both testDate[0] != '0' and testDate[1] != '1' are both going to be true.

As an aside, I know that you are rushing the graduate, but the rate your questions are coming in and range of questions you are asking about makes it feel like you either decide to take all your programming classes in the last quarter/semester of your college career; or you managed to postpone handing in all your assignments from earlier in the quarter/semester and trying to do all of them now.
Was This Post Helpful? 0
  • +
  • -

#5 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: Splitting char arrays

Posted 04 April 2013 - 05:46 AM

View PostSkydiver, on 04 April 2013 - 07:29 AM, said:

Your test:
testDate[0] != '1' || testDate[0] != '0'


is always going to be true. You'll need to fix the logic because consider the following:
if testDate[0] == '1', then testDate[0] != '0' is going to be true.
if testDate[0] == '0', then testDate[0] != '1' is going to be true.
if testDate[0] == 'x', then both testDate[0] != '0' and testDate[1] != '1' are both going to be true.

As an aside, I know that you are rushing the graduate, but the rate your questions are coming in and range of questions you are asking about makes it feel like you either decide to take all your programming classes in the last quarter/semester of your college career; or you managed to postpone handing in all your assignments from earlier in the quarter/semester and trying to do all of them now.


No, no, not at all. I have a 4.0 GPA. This is my first C class, and it's very confusing because my instructor is.. much older. He makes us do things in very odd ways. And, all of the questions I am asking are related to one program. I have no real textbook, we're told to look up everything online.. so here I am LOL.

I did great in my Java and regular C++ classes, and all of my SQL classes. This class is my first introduction to pointers and C strings, and I'm struggling.

Just to help everyone understand, I've been a member here for almost 2 years. I never ask gimme teh codez, I try to learn and grow as a programmer.
This particular program is complex.. the problem statement is 2 pages long. I've been working on it for.. hell I don't even know how long. It was due Tuesday, but I couldn't get it running, so I started over. I have to have it in Friday, and I will take a 10 point deduction.

I have to "create an array of fully edited data and print it, open a text file and use fprintf to write only the IDs".
There are specs for what each piece of data in the array has to be, there are validation rules for each piece of data, and then:
"All data must be entered as strings in separate functions. Once accpeted it should be stored in one struct of an array of structs... and so on and so forth, for another page and a half.

I knew this would happen when I started asking a million questions. I own the fact that I'm struggling, but I'm also trying really, really hard to understand the concepts. It's just a LOT that he threw at us at one time, with no lecture, no book. Just the Internet.

This post has been edited by synlight: 04 April 2013 - 05:53 AM

Was This Post Helpful? 0
  • +
  • -

#6 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: Splitting char arrays

Posted 04 April 2013 - 11:54 AM

I have a question about buffers.. I was told to use way more space than you need to, probably because my professor uses gets for everything.

So.. if I want to have the user input 9 characters for a date (XX/XX/XXXX), then since I'm using fgets,, I only need an 11 element character array, correct?

9 for the date, 1 element for the \n, and 1 for the \0?

I'm trying to get a good handle on this so I can stop using huge buffers if I don't need them.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4019
  • View blog
  • Posts: 12,408
  • Joined: 25-December 09

Re: Splitting char arrays

Posted 04 April 2013 - 12:08 PM

Quote

9 for the date,

Count again "xx/xx/xxxx\n\0", I get 10 characters + end of line + end of string == 12.

If you use an array size of 11 you will have in your string the following "xx/xx/xxxx\0" The end of line character will be left in the input buffer, and will probably cause problems later.



Jim

This post has been edited by jimblumberg: 04 April 2013 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

#8 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: Splitting char arrays

Posted 04 April 2013 - 12:14 PM

View Postjimblumberg, on 04 April 2013 - 02:08 PM, said:

Quote

9 for the date,

Count again "xx/xx/xxxx\n\0", I get 10 characters + end of line + end of string == 12.

Jim

Oops. I was using an array drawing I made this morning for reference so I started with 0 LOL.

So.. do I only need 12 elements in the array? I don't need some huge array if I'm using fgets, because I can control fgets better than gets, right?

View Postjimblumberg, on 04 April 2013 - 02:08 PM, said:

Quote

9 for the date,

Count again "xx/xx/xxxx\n\0", I get 10 characters + end of line + end of string == 12.

If you use an array size of 11 you will have in your string the following "xx/xx/xxxx\0" The end of line character will be left in the input buffer, and will probably cause problems later.



Jim

Okay, thanks Jim.

I'm taking baby steps here because I want to understand it.

SO I have my date, with a 0 and a \n at the end.

Do I need to remove the \n to test the String, does it cause problems?
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4019
  • View blog
  • Posts: 12,408
  • Joined: 25-December 09

Re: Splitting char arrays

Posted 04 April 2013 - 12:18 PM

Quote

Oops. I was using an array drawing I made this morning for reference so I started with 0 LOL.


What? Arrays are zero based but the size will be the total number of characters. So an array of size three will have elements 0, 1, 2, which equals 3 characters. Arrays start at zero and stop at size - 1.

It's a good goal to try to limit the sizes of your arrays, but with your great instructor you may want to consider just using the same sized arrays for everything. Get the project turned in then you can start fresh, if you want, and do things the correct way for you're own education.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: Splitting char arrays

Posted 04 April 2013 - 12:31 PM

haha no I had a drawing on my desk like this:

XX/XX/XXXX
0123456789

to help me this morning when I was writing a function to test the dates.

At least I DO know a little about arrays.

Okay. So I got my states working, so I'm ready to move on to my due date.

Here is where I call the function to get the date:
void getDate()
{
	char testDate[20];
	const char length = 20;
	bool valid = false;
	getString("Please enter the date as XX/XX/XXXX:", testDate, length); 
	validDate(testDate);

	if(!valid)
		getString("Invalid date. Please enter the date as XX/XX/XXXX:", testDate, length); 
}


Oops I was reading some functions, didn't realize I had posted already.

I'm struggling with the logic. I have to test for so many conditions.

This post has been edited by synlight: 04 April 2013 - 12:37 PM

Was This Post Helpful? 0
  • +
  • -

#11 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: Splitting char arrays

Posted 04 April 2013 - 12:41 PM

bool validDate(const char testDate[])
{
	int month = 0;
	int day = 0;
	int year = 0;
	bool valid = true;
	
	printf("%c",testDate[2]);


	if(testDate[2] == '/' && testDate[5] == '/')
		printf("true");
	else
		printf("false");

	return valid;
}


That is testing correctly..

But my getDate function is showing the invalid message even when valid returns true.

void getDate()
{
	char testDate[11];
	const char length = 11;
	bool valid = false;
	getString("Please enter the date as XX/XX/XXXX:", testDate, length); 
	validDate(testDate);

	if(valid)
		valid = true;
	else
		getString("Invalid date. Please enter the date as XX/XX/XXXX:", testDate, length); 

}


SO.. there is a logic error... I don't see it.

This post has been edited by synlight: 04 April 2013 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7645
  • View blog
  • Posts: 12,898
  • Joined: 19-March 11

Re: Splitting char arrays

Posted 04 April 2013 - 01:22 PM

validDate(testDate);



Again, you have to capture the value you're returning, or your work is all in vain.

valid = validDate(testDate);





And this:

    if(valid)
        valid = true;



Hrm?

This post has been edited by jon.kiparsky: 04 April 2013 - 01:22 PM

Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4019
  • View blog
  • Posts: 12,408
  • Joined: 25-December 09

Re: Splitting char arrays

Posted 04 April 2013 - 01:29 PM

What is the purpose of getDate()? I ask because this function is not returning any information. You don't have any parameters or a return value. So unless you're using global variables it can't return anything.

Jim
Was This Post Helpful? 0
  • +
  • -

#14 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: Splitting char arrays

Posted 04 April 2013 - 02:08 PM

Have you guys ever just lost faith in your ability? I feel like that right now. And you know, the funniest part is that I'm taking another C++ class right now, designing video games. I've had 4 programs for that class this semester(with a great professor), and I haven't had to ask a single question about them here. And my average is a 98.

getDate is called in main. When I am done with it, it will say something like
customer[i].date = getDate
I'm just trying to do it in very small steps so I don't get stuck on something simple.


So.. I took a 1 hour break and relaxed for the first time in days.

As soon as I opened the program I was able to fix it in about 90 seconds.

void getDate()
{
	char testDate[20];
	const char length = 20;
	bool valid = false;

	getString("Please enter the date as XX/XX/XXXX:", testDate, length); 
	valid = validDate(testDate);

	while(!valid)
	{
		getString("Invalid date. Please enter the date as XX/XX/XXXX:", testDate, length); 
		valid = validDate(testDate);
	}
}//END GETDATE

/***********************************************
* Check valid date*
************************************************/
bool validDate(const char testDate[])
{
	int month = 0;
	int day = 0;
	int year = 0;
	bool valid = false;
	
	

	if(testDate[2] == '/' && testDate[5] == '/')
		valid = true;
	else
	{
		printf("Invalid date. Please enter /s");
		valid = false;
	}

	return valid;
}

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

Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon


Reputation: 4019
  • View blog
  • Posts: 12,408
  • Joined: 25-December 09

Re: Splitting char arrays

Posted 04 April 2013 - 02:23 PM

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
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2