|
-
Jan 23rd, 2002, 11:35 AM
#1
Thread Starter
Hyperactive Member
user input, what's wrong with this?
I'm using the following bit of code to get user input. the command
/q should make the program exit but it's not working correctly.
what am i doing wrong?
Code:
const unsigned int MAX_SIZE = 256;
char pszCmd[MAX_SIZE] = "";
do
{
cin.getline(pszCmd, MAX_SIZE, '\n');
cout << pszCmd << endl;
}while(pszCmd != "/q");
Bababooey
Tatatoothy
Mamamonkey
-
Jan 23rd, 2002, 12:41 PM
#2
Frenzied Member
This is one of those things about C that makes you appreciate high-level programming languages 
As far as the compiler is concerned, any string literal that you put in your code will become a pointer to a sequence of bytes in the executable that your compiler produces from your code. So whenever you write "blah blah blah" the compiler is treating it as a variable of type char*, just a simple pointer.
What this means is that something like this doesn't work:
Code:
char *szString = "Hello";
... //some more code might be in-between
if ( szString == "Hello" )
{
//blah blah some more code
}
//etc
When you check string equality using '==' between two char* expressions, it checks the pointer, not the content of the string. So in this case you actually have two instances of a string "Hello" in your compiled code, and each will have their own address in memory when the code is loaded up. "Hello" == "Hello" will always be false unless you have some weird compiler that decides to optimise by only having one instance of identical strings.
Hey, I just thought, maybe that's what string tables are for... hmm... I never did see the point in them 
Anyway, that's the explanation, probably a little more than you needed but what the hey. Now for the solution:
a) use strcmp() (available in string.h) to compare the two strings properly
or
b) if you're coding in C++, use a string class like the STL std::string class to make things much more high-level and easier to manage, at the expense of a little overhead.
Harry.
"From one thing, know ten thousand things."
-
Jan 23rd, 2002, 12:56 PM
#3
Thread Starter
Hyperactive Member
thx, i'm using strcmp(), it works great although it's weird how
cin.getline interrupts a carriage return.
for some reason, you have to hit return twice for getline to stop
getting data from the user after the "/q" is reached.
any way to remedy this?
Bababooey
Tatatoothy
Mamamonkey
-
Jan 23rd, 2002, 01:43 PM
#4
Thread Starter
Hyperactive Member
interprets not interrupts
Bababooey
Tatatoothy
Mamamonkey
-
Jan 23rd, 2002, 03:30 PM
#5
PHP Code:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const unsigned int MAX = 256;
char pszCmd[MAX];
do
{
cin.getline(pszCmd, MAX, '\n');
cout << pszCmd << endl;
} while(strcmp(pszCmd, "/q"));
return 0;
}
Works without problems, no "hit enter twice" or such.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
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
|
Click Here to Expand Forum to Full Width
|