Archived

This topic is now archived and is closed to further replies.

HELP with Strings!!

This topic is 5024 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I'm a bigenner C++ programmer, and I'm trying to learn about strings.. I have the following problim: 1. The program asks you to enter a string.. 2. if the string given by the user is "Hello!", it gives you back "Hello! How are you this day?" 3. If the string given by the user is anything else, it gives you "You're reply is worng!" Here's my code:
#include <iostream.h>
#include <stdlib.h>
using namespace std;

int main()
{
char string[7];
cout << "Start The Conversation: ";
cin.get(string, 7, '\n');
if (string = Hello!)
cout << "\nHello! How are you this day?" << endl;
else
 cout << "Your reply is WRONG!!" << endl;

      system("PAUSE");
      return 0;
}
  
[edited by - nectron101 on April 15, 2004 7:43:29 AM]

Share this post


Link to post
Share on other sites
Hi;
For a beginner in C you have to always be careful in conditionals. use == instead of = . The former checks for equality but the latter assigns a value.

Anyway, even if you had gotten that right the code would still fail because you are not testing for string equality, but pointer equality. use the strcmp() function instead of == or better still, use sdt::string.

Peace Out!

Share this post


Link to post
Share on other sites
Here's your problem

if (string = Hello!)

it should be

if (strcmp(string, "Hello!") == 0)


// Last Attacker \\---=( LA )=---// LA Extreme \\

ICQ Number : 120585863

E-mail: laextr@icqmail.com

[edited by - Last Attacker on April 15, 2004 7:51:19 AM]

Share this post


Link to post
Share on other sites
#include <iostream>
#include <string>

int main()
{
std::cout << "start the conversation: ";
std::string line;
getline(std::cin, line);
if (line == "Hello!")
{
}
else
{
}
}


Share this post


Link to post
Share on other sites
Its a pity that you cannot do this:


bool operator == (char* str1, char* str2)
{
if(strcmp(str1, str2) != 0)
return false;

return true;
}


// Last Attacker \\---=( LA )=---// LA Extreme \\

ICQ Number : 120585863

E-mail: laextr@icqmail.com

Share this post


Link to post
Share on other sites
Okay, I rewrote your code. I tried to put comments explaining why I made the changes.

// In C++, you include standard headers like this:

#include <iostream> // not iostream.h

#include <string>
// If you want to include standard C headers, use

// #include <cstdlib>

// instead of

// #include <stdlib.h>


// Not everyone likes this. I think it's okay to use in source

// (.c/.cc/.cpp) files (as long as no name conflicts occur) but

// NOT in headers!

using namespace std;


int main()
{
// Using character arrays as strings is not a good idea in C++

// because you have the standard string class which is much

// easier and safer to use:

string str;

cout << "Start The Conversation: ";

// Now you can input strings like this:

cin >> str;

// In C/C++ '=' is the assignment operator and '==' is the

// comparison operator. Also, you forgot to quote your Hello!:

if (str == "Hello!")
{ // Even in a simple case like this it's good to use brackets

// '{' and '}' as you'll less likely to forget them in more

// complicated situations later.

cout << "\nHello! How are you this day?" << endl;
}
else
{
cout << "Your reply is WRONG!!" << endl;
}

system("PAUSE");
return 0;
}

EDIT: Some formatting stuff
EDIT2: And, BTW, you didn't tell what your problem was. It was quite clear this time (didn't compile) but you should be more specific in the future.

[edited by - nonpop on April 15, 2004 8:06:48 AM]

[edited by - nonpop on April 15, 2004 8:09:51 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Last Attacker
Its a pity that you cannot do this:


bool operator == (char* str1, char* str2)
{
if(strcmp(str1, str2) != 0)
return false;

return true;
}




But if you could do that, how would you compare the pointers themselves?

Share this post


Link to post
Share on other sites
Some extra credit... I got this in a preinterview test from a game company a few weeks ago. I figured it would be amusing to share.

quote:
It is your first day at |name of studio omitted|, and you learn that you won't be able to use the STL, and if you even mention the <string>, you'll be forced to code with Unix's 'vi' editor. Why do we react this way?


Have fun.

[edited by - bobstevens on April 15, 2004 11:17:29 PM]

Share this post


Link to post
Share on other sites
If that really is the attitude that they have at "name of studio omitted", then they... well, they deserve the kind of programmers that they end up with after they filter out all the ones that are sane enough to use the STL where it''s appropriate.

Share this post


Link to post
Share on other sites
quote:
Original post by Doc
quote:
Original post by Last Attacker
Its a pity that you cannot do this:


bool operator == (char* str1, char* str2)
{
if(strcmp(str1, str2) != 0)
return false;

return true;
}




But if you could do that, how would you compare the pointers themselves?


Yes, well...

// Last Attacker \\---=( LA )=---// LA Extreme \\

ICQ Number : 120585863

E-mail: laextr@icqmail.com

[edited by - Last Attacker on April 16, 2004 2:51:03 AM]

[edited by - Last Attacker on April 16, 2004 2:52:24 AM]

Share this post


Link to post
Share on other sites
quote:
It is your first day at |name of studio omitted|, and you learn that you won't be able to use the STL, and if you even mention the <string>, you'll be forced to code with Unix's 'vi' editor. Why do we react this way?
Because you're morons.

[Edit: Because you don't grok custom allocators.]

Besides, I'm comfortable in vi. So I'll say it: "<string>".

[edited by - Oluseyi on April 16, 2004 5:28:09 AM]

Share this post


Link to post
Share on other sites
Assuming your goal is to get hired at the studio, perhaps you''d do well to try to see it their way.

My thoughts? Well I prefer std::string but I have heard many people in the game industry express reservations about it. It can be misused as badly as char pointers. But really, if you can''t use char pointers effectively, you can''t use any other kind of pointer effectively and are not a good hire as a result.

I found their stance on STL to be overly cautious but that''s really irrelevant. Anyone in a junior programming job will be asked to do something they would prefer not to do, and it''s unrealistic to expect otherwise.

And I use vim daily.

Share this post


Link to post
Share on other sites