Sign in to follow this  
brandonman

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

Recommended Posts

<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); } }
  • Guest Anonymous Poster
    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)

    Share this post


    Link to post
    Share on other sites
    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.

    Share this post


    Link to post
    Share on other sites
    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.

    Share this post


    Link to post
    Share on other sites
    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...

    Share this post


    Link to post
    Share on other sites
    I kept my way and if you want, you can compile this source:

    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.");
    {
    int p ,a ,b, c, d ,z;//int

    ("p=%d");//what p equals
    ("a=%d");
    ("b=%d");
    ("c=%d");
    ("s=%d");
    ("o=%d");
    ("z=%d");

    ("%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 breating and forced you to join him as a zombie. please close now!");

    if(a==2)printf("you got lucky...He forced you to bleed but you defeated him");

    if(a==3)printf("the cop beat the zombie but got hurt in the process, so he cannot fight anymore.");
    printf("You hear a group of people, do you want to check it out?1=yes,2=no");

    scanf("%d",&b);
    if(b==1)printf("you have found a group of people and are going to start a colony!");
    if(b==2)printf("You were captured without a friend or buddy to protect you, close now");
    printf("where do you want to have this colony?1=current city, 2= middle of nowhere!");
    scanf("%d",&c);
    if(c==1)printf("good choice, you started in a secret old gangster hideout and found weapons:do you want to fight back?1=yes,2=no");
    if(c==2)printf("your group has been surrounded and you all were turned into zombies,please close.");
    scanf("%d",&d);

    if(d==1)printf("Your superior force formed into an army and you saved the human race! congratulations %s!",&n);
    printf("please close now.");
    if(d==2)printf("well, not a very good thing:you all were turned into zombies because you were cornered in you new HQ, please close");
    if(c==2)printf("your group has been surrounded and you all were turned into zombies,please close.");
    scanf("%d",&z);

    }
    }


    I had it typed up before reading these other posts. forgot to take out the comment for the area selection, made it one area for now.

    Share this post


    Link to post
    Share on other sites
    I'm actually kind of proud of MY first game. now I have to figure out how to do the graphics and sound for more advanced games. Please post what you think of the output(actual game) if you compiled it.

    Share this post


    Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    Sign in to follow this