-
Comparing Chars
I'm using Borland C++. How would I go about having a space when using if(!strcmp(chc1,getkys))? Here is a sample of my code:
//The Highlight Problem Is The getkys variable
//Filename: lv1.cpp
//Function: Level 1
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
int keyamt=0;
int lp=1;
unsigned char chc1[20];
unsigned char getkys[]="GET KEYS";
unsigned char getchc[20];
unsigned char inv[]="INVENTORY";
cout << "LEVEL ONE" << endl;
cout << "ESCAPE FROM THE DUNGEON" << endl;
cout << endl << "YOUR IN A DARK DUNGEON CELL." << endl;
cout << "THE WALLS ARE COVERED WITH MOSS AND COBWEBS." << endl;
cout << "THE SLEEPING GUARD SEEMS EXTREMELY VULNERABLE" << endl;
cout << "A PAIR OF KEYS ARE ATTACHED TO HIS BELT." << endl;
while(lp!=0)
{
cout < ">";
cin >> chc1;
if(!strcmp(chc1,getkys))
{
cout << "YOU NOW HAVE THE KEYS." << endl;
}
if(strcmp(chc1,getkys))
{
cout << "YOU HAVE NOTHING." << endl;
}
}
}
-
You could use getline().
>cin >> chc1;
Change to:
cin.getline(chc1,20);
-
Understanding
I don't specifically understand that... Can you put it in more specific words? Maybe a more specific example? And what header file do I need?
-
Change this:
cin >> chc1;
To this:
cin.getline(chc1,20);
-
Unsigned char?
Exactly what does it mean when you declare a character to be "unsigned"?
I thought that only had to do with variables of type int.
-
Change
It doesn't seem to work using the change this: to this: in my compiler... I don't know why... again, I'm using borland c++.
-
Can you please post your latest code and what the error message is?
-
Reply
//Filename: lv1.cpp
//Function: Level 1
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
int keyamt=0;
int lp=1;
unsigned char chc1[20];
unsigned char getkys[]="GETKEYS";
unsigned char getchc[20];
unsigned char inv[]="INVENTORY";
cout << "LEVEL ONE" << endl;
cout << "ESCAPE FROM THE DUNGEON" << endl;
cout << endl << "YOUR IN A DARK DUNGEON CELL." << endl;
cout << "THE WALLS ARE COVERED WITH MOSS AND COBWEBS." << endl;
cout << "THE SLEEPING GUARD SEEMS EXTREMELY VULNERABLE" << endl;
cout << "A PAIR OF KEYS ARE ATTACHED TO HIS BELT." << endl;
while(lp!=0)
{
cout < ">";
cin >> chc1;
if(!strcmp(chc1,getkys))
{
Change this:
cin >> chc1;
To this:
cin.getline(chc1,20);
cout << "YOU NOW HAVE THE KEYS." << endl;
}
else if(strcmp(chc1,getkys))
{
cout << "YOU HAVE NOTHING.";
}
}
}
//errors:
//undefined symbol "change"
//statement missing ; in function main
//undefined symbol "to"
//statement missing ; in function main
-
use some delimeter in the getline()
cin.getline(chc1,20,'\n');
-
It still doesn't work...
Can you show me some full sample code of how this might work?
-
Code:
//Filename: lv1.cpp
//Function: Level 1
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
int keyamt=0;
int lp=1;
unsigned char chc1[20];
unsigned char getkys[]="GETKEYS";
unsigned char getchc[20];
unsigned char inv[]="INVENTORY";
cout << "LEVEL ONE" << endl;
cout << "ESCAPE FROM THE DUNGEON" << endl;
cout << endl << "YOUR IN A DARK DUNGEON CELL." << endl;
cout << "THE WALLS ARE COVERED WITH MOSS AND COBWEBS." << endl;
cout << "THE SLEEPING GUARD SEEMS EXTREMELY VULNERABLE" << endl;
cout << "A PAIR OF KEYS ARE ATTACHED TO HIS BELT." << endl;
while(lp!=0)
{
cout < ">";
cin.getline(chc1,20);
if(!strcmp(chc1,getkys))
{
cout << "YOU NOW HAVE THE KEYS." << endl;
}
else if(strcmp(chc1,getkys))
{
cout << "YOU HAVE NOTHING.";
}
}
}
-
Thank You, Swoopy
Thank You,
It worked. Now I can conitinue with the game I'm creating.
-
Whew ... I was starting to get worried there!:)