variable char array

when you dont know the size of the array

Page 1 of 1

3 Replies - 2719 Views - Last Post: 09 November 2008 - 09:47 AM Rate Topic: -----

#1 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 260
  • View blog
  • Posts: 1,419
  • Joined: 07-April 08

variable char array

Posted 09 November 2008 - 08:09 AM

this is probably very simple, but i'm hitting a major block. I've got a method which takes in a char array. The array will be variable in length based off what the user inputs. They can either input a Time in the form of passing the pointer of an array or in the form of "1:23AM" I Then need to check then input to make sure its a valid Time starting with if it has a colon. I know that i can use a bunch of IF statements, but not sure if there is an easier way. Like using something along the lines of ARRAY.Length() to find the exact length of the inputted array.

Here is what i have so far:

EDIT to add in the code to check the size

bool MyTime::setTime(char timearray[])
	{
		int size = sizeof(timearray)/sizeof(*timearray);
		cout << size << " " << timearray[1] << " " << timearray[2] << " ";
		
		switch(size)
		{
			case 5:
					  timearray[3] = toupper(timearray[3]);
					  timearray[4] = toupper(timearray[4]);

					  if(timearray[1] == ':')
					  {
						  if((timearray[3] == 'A' || timearray[3] == 'P') && timearray[4] == 'M')
						  {
							  if(timearray[0] > 0 && timearray[0] <= 12 && timearray[2] >= 0 && timearray[2] < 60)
							  {
									iHour = timearray[0];
									iMinutes = timearray[2];
									if (timearray[3] == 'P' && iHour != 12)
									{
										iHour += 12;
									}
									if (timearray[3] == 'A' && iHour == 12)
									{
										iHour = 0;
									}
									cout << iHour << timearray[1] << iMinutes << AMPM[0] << AMPM[1] << " ";
									return true;
								}
								else 
								{
									iHour = 0;
									iMinutes = 0;
									timearray[3] = 'E';
									timearray[4] = 'R';

									cout << "HAHA " << iHour << iMinutes << AMPM[0] << AMPM[1] << " ";
									return false;
								}
							}
							else 
							{
								iHour = 0;
								iMinutes = 0;
								timearray[3] = 'E';
								timearray[4] = 'R';

								cout << "HAHA " << iHour << iMinutes << AMPM[0] << AMPM[1] << " ";
								return false;
							}

							
						}
					  break;
			case 7:
					 return true;
					 break;
		}
	}



I was wondering If i use Sizeof(timearray)/Sizeof(*timearray) will that produce an accurate count of the size of the array if the paramiter passed to setTime was a pointer to an array?

I.E.

//will this work? if i use the Sizeof(timearray)/Sizeof(*timearray) to find the size of the array?

char time[5] = {12, ":" , 45, "A", "M"};

MyTime t;

t.SetTime(time);


This post has been edited by rgfirefly24: 09 November 2008 - 08:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: variable char array

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5959
  • View blog
  • Posts: 23,229
  • Joined: 23-August 08

Re: variable char array

Posted 09 November 2008 - 08:32 AM

You should just use a char *...it can be an array that's passed in. Or, seeing as you're using C++, just use the std::string class.
bool MyTime::setTime(const char *timeArray)
{
    char *colon = strchr(timeArray, ':');
    if (colon)
    {
         // Has a colon
    }
}


or

bool MyTime::setTime(const std::string &timeString)
{
    size_t colonPos = timeString.find(':');
    if (colonPos != std::npos)
    {
        // Has a colon
    }
}

Was This Post Helpful? 0
  • +
  • -

#3 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 260
  • View blog
  • Posts: 1,419
  • Joined: 07-April 08

Re: variable char array

Posted 09 November 2008 - 09:02 AM

The above would work, but due to the teacher being extremely picky on what we use, i cannot use the above. I have to use a parameter which is an array of characters and can accept an input in string form or array form. The way i worded my first post probably was a little confusing. Let me post exactly what her instructions say.

2. a setTime method which returns a bool and has one parameter which is an array of characters. The parameter indicates the time on a standard clock using am and pm. If the parameter is valid, the method should assign the times in military format to the private properties and should return a bool of true. If the parameter contains any invalid part( hour, minute, and/or indicator are invalid), default both the hour and minute to zeros and return a bool of false. The method should allow for input in the following forms:

##:##XX I.E: 10:25am
#:##XX I.E: 5:05PM

Where XX is upper or lower case ~ AM or PM
The hour portion of the string must be 1-12. the minute portion of the string must be 0-59. The character portion of the string must be AM or PM(either upper or lower case). A colon must separate the hour and minute portion.

Now this was unclear so i asked her about it. She basically said she will either pass a String time or a char array to the method and we should account for either of those variable arguments.


EDIT: Also using the above method, how do i tell where in the array i found the colon? The real reason for needing to find the size of the input is because the argument can be anywhere between 5 and 7 in length. Based off the length tells me where the placement of the colon is and the AM/PM.

This post has been edited by rgfirefly24: 09 November 2008 - 09:42 AM

Was This Post Helpful? 0
  • +
  • -

#4 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 260
  • View blog
  • Posts: 1,419
  • Joined: 07-April 08

Re: variable char array

Posted 09 November 2008 - 09:47 AM

OK, it seems that in doing some more digging on Google, i found out that I am attempting to find the size of a char array by its pointer which is invalid.

I guess i have no choice but to create extremely long compound if statements or use a lot of if statements.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1