Help with text based game
Hello. I'm starting to make a text based game and apparently I'm so oblivious to strings that I can't make this simple program to run:
#include <stdio.h>
int main ()
{
char a[100];
printf ("You've started your adventure!\n");
printf ("Where do you want to go?\n");
printf (" w = up, a = west, s = south, d = east, p = quit\n");
getchar();
scanf( "%s", &a );
if (a = "w"){
printf ("You've reached an open field.\n");
getchar();
}
else if (a = "a"){
printf ("You see a sword salesman.\n");
getchar();
}
else if (a = "s"){
printf ("You're in an open field...\n");
getchar();
printf ("WAIT! The dark castle is ahead!\n");
getchar();
}
else if (a = "d"){
printf ("There's a broken bridge ahead.\n");
getchar();
printf ("You must go back.\n");
getchar();
}
else if (a = "p"){
printf ("Bye-Bye!\n");
getchar();
}
else{
printf("ERROR: Bad Input...Quitting...\n");
getchar();
return 0;
}
getchar();
}
I always get this error:
12 incompatible types in assignment
16 incompatible types in assignment
21 incompatible types in assignment
28 incompatible types in assignment
35 incompatible types in assignment
(12, 16, 21, 28, 35 are the line numbers)
Since they are 5 lines that are basically the same, and have the same
error, I feel reeeeealy stupid about not knowing what is wrong.
Please be good people and help? Please?
Sincerely,
Annoyer
P.S. I'm using the Dev-C++ IDE. I doubt that affects anything but it may be worth mentioning.
char literals go in single quotes, not double. Also, the equality operator in C/C++ is ==, not = (which is assignment).
Two other notes:
1. You can use [ source ][ /source ] tags (without the spaces) to make your code format correctly.
2. Any particular reason you're using C rather than C++?
Two other notes:
1. You can use [ source ][ /source ] tags (without the spaces) to make your code format correctly.
2. Any particular reason you're using C rather than C++?
Okay I changed = to == and " to ' but now when i type a letter nothing happens i keep pressing enter and nothing happens.
Sincerely,
Annoyer
P.S. I like C because I hate OOP.
For this reason, I know some C++ but not alot.
Sincerely,
Annoyer
P.S. I like C because I hate OOP.
For this reason, I know some C++ but not alot.
Quote:Original post by annoyer101Looking at your code more carefully I now see that a is a character array rather than a character, so your conditionals are actually comparing a pointer to an integer rather than a character to a character literal.
Okay I changed = to == and " to ' but now when i type a letter nothing happens i keep pressing enter and nothing happens.
Also, I think you may be using getchar() and scanf() incorrectly. I won't try to give an example of correct usage because it's been a long time since I've used those functions myself. In any case:
Quote:P.S. I like C because I hate OOP.To each his or her own, but I would strongly urge you to reconsider your aversion to C++ and OOP. Assuming that you intend to use one of these two languages (keep in mind that there are other options as well), using C++ will increase your productivity and decrease your development time considerably.
Might I ask what you don't like about C++ and/or object-oriented development?
I believe to compare a char array with a constant string, you have to use strcmp.
I am also wondering why you dislike OOP? A good OOP based design can save you a ton of time when it comes to a large project. Having 1000's of functions and trying to track down a specific area in a tiny section will drive you mad, however if you can track it down to a specific object it becomes much easier to fix. More then half of your development time on any project will be tracking down and fixing bugs, so anything that can help speed up this process will help you develop things faster.
I am also wondering why you dislike OOP? A good OOP based design can save you a ton of time when it comes to a large project. Having 1000's of functions and trying to track down a specific area in a tiny section will drive you mad, however if you can track it down to a specific object it becomes much easier to fix. More then half of your development time on any project will be tracking down and fixing bugs, so anything that can help speed up this process will help you develop things faster.
After making some changes, here's the new code:
#include <stdio.h>
int main ()
{
char a;
printf ("You've started your adventure!\n");
printf ("Where do you want to go?\n");
printf (" w = up, a = west, s = south, d = east, p = quit\n");
scanf( "%d", &a );
getchar();
if (a = "w"){
printf ("You've reached an open field.\n");
getchar();
}
else if (a = "a"){
printf ("You see a sword salesman.\n");
getchar();
}
else if (a = "s"){
printf ("You're in an open field...\n");
getchar();
printf ("WAIT! The dark castle is ahead!\n");
getchar();
}
else if (a = "d"){
printf ("There's a broken bridge ahead.\n");
getchar();
printf ("You must go back.\n");
getchar();
}
else if (a = "p"){
printf ("Bye-Bye!\n");
getchar();
}
else{
printf("ERROR: Bad Input...Quitting...\n");
getchar();
return 0;
}
getchar();
}
Now, no matter what I type, it says "You've reached an open field"
Why is that?
Sincerely,
Annoyer
#include <stdio.h>
int main ()
{
char a;
printf ("You've started your adventure!\n");
printf ("Where do you want to go?\n");
printf (" w = up, a = west, s = south, d = east, p = quit\n");
scanf( "%d", &a );
getchar();
if (a = "w"){
printf ("You've reached an open field.\n");
getchar();
}
else if (a = "a"){
printf ("You see a sword salesman.\n");
getchar();
}
else if (a = "s"){
printf ("You're in an open field...\n");
getchar();
printf ("WAIT! The dark castle is ahead!\n");
getchar();
}
else if (a = "d"){
printf ("There's a broken bridge ahead.\n");
getchar();
printf ("You must go back.\n");
getchar();
}
else if (a = "p"){
printf ("Bye-Bye!\n");
getchar();
}
else{
printf("ERROR: Bad Input...Quitting...\n");
getchar();
return 0;
}
getchar();
}
Now, no matter what I type, it says "You've reached an open field"
Why is that?
Sincerely,
Annoyer
Quote:
if (a = "w"){
printf ("You've reached an open field.\n");
Still assigning variable a to value "w" (in other words, you still need to change it to if(a == 'w'). So what is happening is:
(a = "w") // the value of a is being set to "w" (should use 'w' ( "=a string, or more than one letter/number, '=char, a single letter or number))
then checks the if statement, which could be read as
a = "w";
if(a) {
...
since a = "w", and not 0 (zero) the statement is true, and the branch is taken. because all other checks are else if(...) they are never taken. Try adding a second = to each if(a = , and change " to ' in each if (a = statement.
You may want to do something along these lines instead:
The while loop should allow you to continue to accept input from the player, although this code does nothing to update the player's position. You'll probably want to start using functions to handle where the player is currently located. From the look of your code and the way you post, it doesn't sound like you're ready to make a game yet, so I would recommend studying C programming some more before you dive into game making.
By the way, I would also recommend reconsidering your aversion to C++ and OOP.
#include <stdio.h>int main (void){ char a; printf ("You've started your adventure!\n"); printf ("Where do you want to go?\n"); printf (" w = up, a = west, s = south, d = east, p = quit\n"); while (scanf( "%c", &a ) && a != 'p'){ /* %d is used to read ints, while %c is used to read chars. The difference is important. */ if (a == 'w'){ printf ("You've reached an open field.\n"); } else if (a == 'a'){ printf ("You see a sword salesman.\n"); } else if (a == 's'){ printf ("You're in an open field...\n"); printf ("WAIT! The dark castle is ahead!\n"); } else if (a == 'd'){ printf ("There's a broken bridge ahead.\n"); printf ("You must go back.\n"); } else{ printf("Unrecognized character\n"); } printf ("Where do you want to go?\n"); printf (" w = up, a = west, s = south, d = east, p = quit\n"); } printf("Bye bye!\n"); return 0;}
The while loop should allow you to continue to accept input from the player, although this code does nothing to update the player's position. You'll probably want to start using functions to handle where the player is currently located. From the look of your code and the way you post, it doesn't sound like you're ready to make a game yet, so I would recommend studying C programming some more before you dive into game making.
By the way, I would also recommend reconsidering your aversion to C++ and OOP.
I would also recommend you go c++ because of the better standard libraries and more features. Even if you dont want to use OOP you can still use c++. When I first learned c++ I never even touched OOP for a long time.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement