-
October 4th, 2004, 11:14 PM
#1
Member
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
01001001001000000100110001101111011101100110010100100000010000100110010101110100011101000111100100100001
-
October 4th, 2004, 11:52 PM
#2
Junior Member
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.
-
October 5th, 2004, 12:06 AM
#3
Member
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
01001001001000000100110001101111011101100110010100100000010000100110010101110100011101000111100100100001
-
October 5th, 2004, 12:28 AM
#4
Junior Member
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?
-
October 5th, 2004, 04:32 AM
#5
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.
-
October 5th, 2004, 11:35 AM
#6
Junior Member
yeo it doesnt, take a look here for an example custom string class:
http://oopweb.com/CPP/Documents/CPPH...g-HOWTO-6.html
-
October 5th, 2004, 02:25 PM
#7
Member
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
01001001001000000100110001101111011101100110010100100000010000100110010101110100011101000111100100100001
-
October 5th, 2004, 03:25 PM
#8
Junior Member
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)
-
October 7th, 2004, 05:15 AM
#9
Member
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
01001001001000000100110001101111011101100110010100100000010000100110010101110100011101000111100100100001
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|