-
C++ string/apstring
Ok, I recently have been given an assignment in my CS120 class. I wont go into the details, but our instructor gave of this bit of code to help us.
string fname("aaa");
if(fname.equals("aaa"))
...
Yeah, I have played around with this peice of code of a while now, and I cant seem to yet that command to work. I am one of the few students in my class that has taken C++ before, so I am not totally new to this stuff. I understand that .equals has to be apart of the class string. I was wondering if anyone knew about this, or any syntax errors I might be having. I think out instructor wants us to use fname.equals("aaa") in out program.
I gave up after playing with it for a while and got it to work another way. Heres my code, that does compile. Any input on this topic would be greatly helpful.
Code:
//Project 4, test
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string fname("aaa");
cout<<fname<<'\n';
if(fname == ("aaa"))
cout<<"Works\n";
else
cout<<"Doesnt work\n";
system("PAUSE");
return 0;
}
Output:
aaa
Works
Press any key to continue . . .
Again, thanks for any help.
-Ep
-
do you use a different compiler / development environment than your teacher?
the standard c++ string class doesn't include equals() function. java, visual basic and net framework do.
-
Well, at school and when I telnet to my CS class, we use g++. I believe that is a compiler used only by Linux. Im not sure tho, cuz I am very new to Linux. I also use Dev C++ at my house, when I am not connected to the schools network. And neither one of those allowed this program to run.
By string declaration, do you mean string fname("aaa") or fname.equals("aaa"). I am wondering, cuz I have never seen either one before either. When, declareing a string, I always just wrote string fname = "aaa". And I have never seen fname.equals("aaa"). So, yeah, I am still a little confused as to how this is to work.
And about viewing the header file. Im not sure about that. I could always look, but the last time I had a question and lookedin teh header file for my answer. All the header file provided me was alot jumping around and confusing variable names. Maybe that was because it was the math.h file, but eh. Ill give it a look and see what I find.
-Ep
-
sorry - i was still reading and editing my first post...
with declaration i meant string fname("aaa"), didn't look familiar, but its ok. i recommend anyway that you use the same environment as in class and make sure you got the same header files, specially when you're new to all this. spares you from a lot of confusion :)
or perhaps your teacher wants you to modify the string class?
-
I don't remember C++ since I've spent the last year and a half doing Java and not touching it... but I don't think C++ has a predefined equals() method.. you have to create one in your class and then you can call it up.
-
yeo it doesnt, take a look here for an example custom string class:
http://oopweb.com/CPP/Documents/CPPH...g-HOWTO-6.html
-
Thanks for all of your help. I will be going to my CS Lab in a couple of hours. I will see what my lab assisstent has to say about it. Again, thanks.
-Ep
-
welcome :) if you cant wait, check
http://www.flipcode.com/tutorials/tut_strings02.shtml
but mostly theCString.h file in the download section. just include it and check the Compare method for the parameters (it's got case sensitive yes/no switch)
-
Justas and update, my lab assisstent had never heard of that at all. And he uses the same compiler my instructor does. He told us to just use fname == ("aaa"). So, I have no idea where the fname.equals("aaa") came from, but yeah. I think you might have to have a specail header file. I guess I just figured since most of my class doesnt even know what a header file is yet, I figured it would be in one of the predefined ones. Owell,
Thanks for all of your guys help.
-Ep