#include <iostream.h>
int main(int argc, char* argv[])
{
char* input;
cout << "welcome" << endl;
cout << "USER:>";
while(1)
{
cin >> input;
if( input=="start" )
{
cout << "starting" << endl;
}
else if( input=="quit" )
{
cout << "Authorize Exit" << endl;
cout << "EXIT:>";
cin >> input;
if( input=="ark" )
{
cout << "Authorization Confirmed, exiting" << endl;
return 0;
}
else
{
cout << "Invalid Authorization Code, unable to exit";
}
}
else
{
cout << "Invalid Command" << endl;
}
cout << "USER:>";
}
return 0;
}
Something that seems strange
Well, i got kinda bored today so i made a simple program that prompts for input and does something, kinda like the dos console(i was bored).
Anyway, it compiled fine, but when i type in a command it crashed, in both linux(using kdevelop) and windows xp(using vc++ 6 intro)(i run both). i got a segmentation fault in linux and in xp i simply get this program has encountered a problem and needs to close(real helpful)
if i changed char* input to char input[20] (or some other number) it didn''t crash but it kept giving me my Invalid Command line, i''ve done input like this before and it''s worked. I just can''t get it to work, though it isn''t important.
thx, in advance for any help.
just change input to use a std::string instead of a char*
[edited by - daerid on April 29, 2002 1:53:20 AM]
[edited by - daerid on April 29, 2002 1:53:20 AM]
thx for the help.
i also had the change the char* to char input[some number] or it still crashed
i also had the change the char* to char input[some number] or it still crashed
quote:Original post by dead_man_walking
i also had the change the char* to char input[some number] or it still crashed
Dynamic memory allocation. char * is just a pointer to a block of memory; you have to actually ask for the block. char [] is a fixed size block of memory ( &(char [0]) is a char *, to illustrate).
You might also want to look into the STL std::string class if you''re using C++, which has much more natural syntax (like str1 == str2, etc) among other benefits.
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
I think you cannot compare the variable input with a constant string that way... (input is a pointer to char, so you are comparing a memory address with a constant string!). Of course, this explanations works well in C, but I''m not sure if in C++ you can do it (please, point me at this point).
And one observation/comment (not related directly with your question): If you replace "while (1)" by "for (;" you avoid one comparision. It isn''t very relevant; I just thought about it when I saw the code.
theNestruo
Syntax error in 2410
Ok
And one observation/comment (not related directly with your question): If you replace "while (1)" by "for (;" you avoid one comparision. It isn''t very relevant; I just thought about it when I saw the code.
theNestruo
Syntax error in 2410
Ok
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement