i thought conio.h was a borland specific library
Printable View
i thought conio.h was a borland specific library
sand_man, hmm, not sure about conio.h but it does work with only conio. :)
They're not talking about including a header file, they're talking about including a .c file. A poor way to do it, but I suppose if one feels the need to mix input types... :cringe:Quote:
Originally Posted by sand_man
Quzah.
>getche(); can also be used, if your program ends as soon as the functions are over.
Let's not introduce nonstandard things when the OP is so confused with the standard ones, kay? ;)
>#include <conio> (on borland)
That would be <conio.h>.
>#include <conio.c> (on dev and others)
That's generally a bad idea. ;)
Because the idea is to compile your .c / .cpp files, and include header files. It's what I like to call "bad form". It's ugly.
Quzah.
>anybody care to tell me why including .c files is a bad idea?
.c files routinely define things, and multiple definitions are illegal. Including a .c file will probably work if you only do so once and don't define any names that it defines, but doing so is tricky and error prone and considered very bad form.
>except if you place your code inside #ifndef right?
That's akin to putting a Band-Aid over a gunshot wound. It might work, but is it really addressing the right problem?
Thank you guys. I understand what went wrong. I really appreciate all of the help and when i code some more I'll put it up if i have any problems. One problem im havin right now is this. Can if/else statements handle things such as names.
example:
If I wanted to code something that asked for somones name and have it check whether it registers with the program or not. If it does register u can continue, otherwise the program ends. I tried writing one of these but I wasnt able to get it to work at all. So I was wondering if if/else was the right thing to use. Thanks!
They could have also put the prototype in main.Quote:
Originally Posted by XSquared
Quzah.
A Switch statement would be a lot better function for you to use for this task.Quote:
Originally Posted by Siggy
The most common mistake for noobies to make here is this:
The problem with the above-marked line is that you are not comparing the content of 'InputName' with the string "Bennyandthejets". You are actually comparing the location of each element. They will never be in the same location, so the code will output "I don't know you". In order to compare the elements correctly, you need the function lstrcmpi(). It compares two strings and returns 0 if they are the same. So, this is how you rewrite the above code:Code:char InputName[64];
cin >> InputName;
if (InputName=="Bennyandthejets") //This line is incorrect
cout << "You are bennyandthejets!" << endl;
else
cout << "I don't know you" << endl;
So a simple way to complete your problem would be to compare the inputted string with a number of constant strings like so:Code:if (lstrcmpi(InputName,"Bennyandthejets")==0)
cout << "You are Bennyandthejets!" << endl;
else
// etc etc
So the name entered must be 'benny' or 'siggy' for the program to continue. That is a simple way of doing it. There are nicer and more complicated ways of doing it too, but you'd best save that for later.Code:if (lstrcmpi(InputName,"benny")==0)
GoIntoProgram();
if (lstrcmpi(InputName,"siggy")==0)
GoIntoProgram();
ExitProgram();
What do you base this statement on? Strings are messy with switch statements. This is how I do it:Quote:
A Switch statement would be a lot better function for you to use for this task.
See what I mean?Code:#define IDS_LOAD 101
#define IDS_SAVE 102
#define IDS_QUIT 103
#define IDS_UNKNOWN 104
int ReturnStringNumber(char *TheString)
{
if (!lstrcmpi(TheString,"load"))
return IDS_LOAD;
if (!lstrcmpi(TheString,"save"))
return IDS_SAVE;
if (!lstrcmpi(TheString,"quit"))
return IDS_QUIT;
return IDS_UNKNOWN;
}
int main()
{
char InputString[64];
cin >> InputString;
switch (ReturnStringNumber((char *)InputString))
{
case IDS_LOAD:
case IDS_SAVE:
case IDS_QUIT:
case IDS_UNKNOWN:
}
return 0;
}
>In order to compare the elements correctly, you need the function lstrcmpi().
This isn't a standard function. As such, not all implementations are guaranteed to have it, and even if the OP's implementation does have it, the code won't be portable. On top of that, lstrcmpi has different results than might be expected due to special treatment of certain nonalphabetic characters. Better to either use strcmp, strncmp, or use the C++ string class (preferable) for now. :)