#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]
🔒 Locked
HELP with Strings!!
Advertisement
Original Post
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:
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!
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!
Here's your problem
if (string = Hello!)
it should be
if (strcmp(string, "Hello!") == 0)
// Last Attacker \\---=( LA )=---// LA Extreme \\
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]
Its a pity that you cannot do this:
// Last Attacker \\---=( LA )=---// LA Extreme \\
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
Okay, I rewrote your code. I tried to put comments explaining why I made the changes.
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]
// 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]
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.
Have fun.
[edited by - bobstevens on April 15, 2004 11:17:29 PM]
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]
quote:
Original post by Docquote:
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]
Too much replies!!
Thanks anyway!
Thanks anyway!
quote:Because you're morons.
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?
[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]
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.
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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement