My text based game. One problem with it[solved]

Started by
10 comments, last by brandonman 18 years ago
<html> <body> I have finally quit looking for spoonfed tutorials and got down and dirty. I made the simple game in Dev-C++, a c/C++ compiler. It runs very smooth but when it gets to a certain point, it does the imput and it is supposed to run a different scenario for three different inputs. When it gets here, it runs all three. I will try to add the exe and the code here.code- #include <cstdio> int main() { char* n; printf("welcome, type your name:");//greeting 1 scanf("%s",&n);//get name printf("hello %s", &n);//greeting specified to player printf("welcome to zombie takeover. where do you want to go:city-type 1, country-type 2:");//tell user choices { int p ,a ,b, c, d ,z;//int ("p=%d");//what p equals scanf("%d",&p); //choose area if(p=1)printf("welcome to the city."); ("a=%d"); printf("eek!!zombie. type 1 to play dead, type 2 to fight with your fists, type 3 to scream and get the final cop's attention."); scanf("%d",&a); if(a=1)printf("uhoh, he saw you breathing and forced you to join him as a zombie"); if(a=2)printf("you got lucky...He forced some bleeding but you defeated him"); if(a=3)printf("the cop beat the zombie...but got hurt bad in the process, he cannot fight anymore."); printf("please close now"); scanf("%d",&z); } } <a href="game1.html>game I think this is how html links work, been a while so I forgot. </body> </html> </html> </body> <html>
Advertisement
<html>
<body>
here's the exe
forgot how html links go, been a while. I think this is it, I'll edit if it's not.
</body>
</html>
in the last part of your code you are syaing if (a=3). that is basically assigning "3" to a. as well as 1 and 2 above it. what you want to do is use the equivalency operator. so - if (a==3)
weird, tested the link and it didn't work, I'll write a link to the actual exe. i exported to html. I'll edit instead of spam it.
oh, thanks.
That was me up there.

But once you correct that it should work just fine for you. But I only looked briefly, so there may be some other error I missed.
there we go, thank you!
wish the link to it worked, maybe it's just my comp. Did it work for anyone else?
Now I'm going to expand it!
Your main problem is that you are not actually really using c++.

You use raw char arrays/pointers and printf/scanf for input output. Thats bad, these functions aren't very friendly and can cause some problems.

Most people use strings to represent text and cin and cout for i/o.


cout is for output. We point the arrows at the cin( << ). (you will see what I mean by arrows )
cin is for input. We point its arrows at the variable( >> ).

Here is an example:

Using cout and strings:
#include <iostream>#include <string>using namespace std;int main(){    // like printf("Hello World.\n");    cout << "Hello World.\n";    string prompt = "press enter...\n";    cout << prompt;    // later we will put something here to stop window from closing instantly}


Using cin:
#include <iostream>#include <string>using namespace std;int main(){    string input;    cout << "Type something.\n";    // char someCharArray[100];    // like scanf("%s", someCharArray );    cin >> input;    cout << "You typed: " << input << ".\n";    string enter;    cout << "Press enter...\n";    cin >> enter; // wait for user to press enter}


Your code cleaned up a little:
#include <iostream>#include <string>using namespace std;int main(){   string name; // we give variables their full names   cout << "welcome, type your name:";   cin >> name;   cout << "hello " << name << "\n"; // greeting specified to player   cout << "welcome to zombie takeover. where do you want to go:city-type 1, country-type 2:";//tell user choices    int input;    cin >> input;    if( input == 1 ) // see below for why there are 2 equals signs    {       cout << "welcome to the city.\n";    }     else    {       cout << "eek!!zombie. type 1 to play dead, type 2 to fight with your fists, type 3 to scream and get the final cop's attention.\n";       cin >> input;       if( input == 1 )       {           cout << "uhoh, he saw you breathing and forced you to join him as a zombie\n";       }       if( input == 2 )       {            cout << "you got lucky...He forced some bleeding but you defeated him\n";        }        if( input == 3 )        {            cout << "the cop beat the zombie...but got hurt bad in the process, he cannot fight anymore.\n";        }    } // end else block    cout << "please close now";    string enter;    cin >> enter; // wait for user to press enter}


We use 2 equals signs together to see if 2 values are equal. When you use one equal sign, it sets the variable on the left to the value on the rigth. Even in a "if" statement.

So
if( x = 1 ){   cout << x << "\n";}

Will always print 1, whatever x was before...

This topic is closed to new replies.

Advertisement