strings and arrays

trying to compare two strings

Page 1 of 1

6 Replies - 736 Views - Last Post: 15 March 2008 - 03:16 PM Rate Topic: -----

#1 rizwans  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 27-October 05

strings and arrays

Posted 14 March 2008 - 04:56 PM

Hi


Allright so here's my problem. I am trying to read in characters from a file and compare two tags. A start tag and an end tag.

The tags consist of <start> and <end> where the start tag and end tags are equal. if they are not equal to each other, then the function must return false.

The function definitions are as follows:
bool start(ifstream &in, char &curr, char el_name[])
bool end(ifstream &in, char &curr, const char el_name[])


I have to compare the two el_names and see if they are equal.
I tried using strcpy to copy the strings into it's own start tag but that didn't work.
I tried using strcmp and i think it works but i'm not sure where to put it in my code.

I really need help with this. I'm almost done everything but i'm just stuck on this one part.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: strings and arrays

#2 rizwans  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 27-October 05

Re: strings and arrays

Posted 14 March 2008 - 05:39 PM

ok so i think i figured it but now i've run into another problem
when i try and call the function end(in, curr, el_name)
like so it gives me a compile error saying invalid conversion from const char* to char*
i know this has to do with pointers and such but still not sure how to call it properly
any thoughts?
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: strings and arrays

Posted 14 March 2008 - 10:49 PM

It means that you cannot manipulate a const variable. Change the const char it is referring to to a regular one and you should be all set.
Was This Post Helpful? 0
  • +
  • -

#4 rizwans  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 27-October 05

Re: strings and arrays

Posted 15 March 2008 - 01:32 PM

View PostKYA, on 14 Mar, 2008 - 11:49 PM, said:

It means that you cannot manipulate a const variable. Change the const char it is referring to to a regular one and you should be all set.


actually it has to stay there. we have to use the code that the prof gave us and can't manipulate it.
pretty dumb i think
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4313
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: strings and arrays

Posted 15 March 2008 - 01:39 PM

Mind showing us what you have put together so far? So we can see what you are doing and perhaps spot where you need to change things to make it work. Thanks a lot! :)
Was This Post Helpful? 0
  • +
  • -

#6 rizwans  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 27-October 05

Re: strings and arrays

Posted 15 March 2008 - 01:40 PM

i took out the const and it crashes
Was This Post Helpful? 0
  • +
  • -

#7 rizwans  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 27-October 05

Re: strings and arrays

Posted 15 March 2008 - 03:16 PM

bool end(ifstream &in, char &curr, const char el_name[])
{
  bool success = false;
  char ch;
 
  ch = in.peek();
  in.get(curr);

  if (curr == '<' && ch == '/')
	{
	  //	  static_cast<el_name *> (el_name);
	  name(in, curr, el_name);

	  if (success)
		{
		  name(in, curr, el_name);
		  strcmp(el_name, el_name);
		  cout << '>';
		}
	}
  return true;
}

bool name(ifstream &in, char &curr, char el_name[])
{

  in.get(curr);

  if ((curr >= 'a' && curr <= 'z') && (curr >= 'A' && curr <= 'Z'))
	{
	  return true;
	}
  else
	return false;

}




Notice that the function parameters for name is just char el_name
whereas the end function takes in const char el_name
not sure how to convert it or if i have to do that.
i can't pass in the function name cause it gives me a compile error
invalid conversion from const char* to char*
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1