2

I have probably a very simple problem, but as a newbie in programming I just can't find solution even with multiple tries and errors. I'm training what I've learned so far on a simple text based adventure game, which I'm having problems with. For the first I'm using strings and I can't make it to use two strings in if to cout a proper situation.

#include<iostream>
#include <string>
using namespace std;

int main ()
{
    string podpalam = "podpalam";
    string papier = "papier";
    cout << "Stoisz w wielkim ciemny pokoju. W reku trzymasz kawalek PAPIERU, \n a w kieszeni spoczywa pudelko ZAPALEK. Co robisz?" << endl;
    cin >> podpalam >> papier;
    if ( podpalam == "podpalam")
        cout << "Pokoj rozswietla sie od plomieni palacej sie kartki.";
    else
        cout << "Pomysl przez chwile. To niekoniecznie prawidlowe wyjscie." << endl;
    cout << " \nKoniec" << endl;
    return 0;
}

So as you can see I have two strings, both called differently, but only one is added to if, but still you need to cin another string to move forward with the adventure, but the second string can be just a simple letter or symbol. I tried adding two strings to if, but I just can't. Maybe it's impossible and I'm fighting with air, but I've seen games like Zork that used two words and worked perfect.

The other thing is that I don't want my program to end when you make mistake, I want it to restart again. Like you made a mistake, you died and now you can start again. Don't really know the command for it and couldn't find the answer in Google or Youtube.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    *"I tried adding two strings to if, but I just cant..."* Do you mean something like `if ( string1 == "something" && string2 == "somethingelse")` ? Please reword your question to make it easier to understand what you're trying. – Hatted Rooster Nov 28 '16 at 14:42
  • You are reading two words from `cin` so you need to provide two words in the input in either one line or two separate lines. – πάντα ῥεῖ Nov 28 '16 at 14:43
  • Thank you @GillBates I was adding the wrong symbol for adding together. I used + instead of &&, but || sum works at this point the same so thank you once more :) – Siemieniaka Nov 28 '16 at 14:56
  • Addition is an OR operation because the result is non-zero (true) if either input is non-zero (true). Multiplication is an AND operation because the result is non-zero (true) only if both inputs are non-zero (true). So don't use `+`, use `*` (or, better yet, `&&`). – David Schwartz Nov 28 '16 at 23:24
  • To make it start again, you need to use a loop construct such as a `while` statement. – Michael Dorgan Nov 29 '16 at 00:48

3 Answers3

0

For starters, especially when you're learning programming, it will be a lot easier to manage your code blocks if you use braces. They make it much easier to see which lines go with which block (you have good indenting though). Ex:

cin >> podpalam >> papier;
if ( podpalam == "podpalam"){
    cout << "Pokoj rozswietla sie od plomieni palacej sie kartki." << endl;
}
else{
    cout << "Pomysl przez chwile. To niekoniecznie prawidlowe wyjscie." << endl;
}

cout << " \nKoniec" << endl;

Secondly, part of your problem could be that you have an endl in the else but not in the if. Adding that to the if should help because end of line characters force the console to flush the output. Without one, it could be that your string was being printed but it was held in a buffer that had not been displayed on the screen yet.

Andrew
  • 518
  • 2
  • 9
  • Thank you. this endl doesn't do anything I guess, but the problem with adding two words in if is gone, but the restart problem is still out there. I mean, I want the program to reset buy running it all over again by itself :) – Siemieniaka Nov 28 '16 at 14:58
0

I think you are looking for the boolean and (&&) operator. This checks that both parts of the statement are true and will return false if they are not.

For example:

if(podpalm == "podpalm" && papier== "papier") ... //this code won't run unless both variables 
//are set to what you specify

On the other hand (for future problems) you may need the OR (||) operator. This will return true if either part of the statement is true or if both halfs are true.

There are also other boolean opertors like NOT(!) and XOR(^) but you will be using && and || the most.

EDIT:

If you want the game to restart like you described, put the entire thing in a while loop to check a condition, then set that condition in the code. Here is the example:

#include<iostream>
#include <string>
using namespace std;

int main ()
{
  bool gameOver = false;
  while(gameOver == false) { //condition is met, start loop
    string podpalam = "podpalam";
    string papier = "papier";
    cout << "Stoisz w wielkim ciemny pokoju. W reku trzymasz kawalek PAPIERU, \n a w kieszeni spoczywa pudelko ZAPALEK. Co robisz?" << endl;
    cin >> podpalam >> papier;
    if ( podpalam == "podpalam") {


 cout << "Pokoj rozswietla sie od plomieni palacej sie kartki.";
        gameOver = true; //if it receives this input, end the game
    }
    else {
        cout << "Pomysl przez chwile. To niekoniecznie prawidlowe wyjscie."   << endl;
        gameOver = false; //otherwise, keep the game going.
      }
    cout << " \nKoniec" << endl;
    return 0;
}
Ashwin Gupta
  • 2,159
  • 9
  • 30
  • 60
  • Thank you for help, but I've already managed it with Gill Bates help, but still there is a problem with restarting. I want this game to reset by itself when the player makes a bad decision. I mean you play, die and the game says, you are dead and resets to square one. How to loop it or something? – Siemieniaka Nov 28 '16 at 15:11
0

Is that I don't want my program to end when you make mistake, I want it to restart again.

Just put the main code of your game into a seperate function and then you call it once the player makes a "bad decision":

#include<iostream>
#include <string>
using namespace std;

void game()
{
    string podpalam = "podpalam";
    string papier = "papier";
    cout << "Stoisz w wielkim ciemny pokoju. W reku trzymasz kawalek PAPIERU, \n a w kieszeni spoczywa pudelko ZAPALEK. Co robisz?" << endl;
    cin >> podpalam >> papier;
    if ( podpalam == "podpalam")
        cout << "Pokoj rozswietla sie od plomieni palacej sie kartki.";
    else
        cout << "Pomysl przez chwile. To niekoniecznie prawidlowe wyjscie." << endl;
    cout << " \nKoniec" << endl;

    //somewhere where the player made a bad decision:
    game(); //restart the game.
    return; // return from the function.
}

int main ()
{
  game();
  return 0;
}

Don't try to call main() inside main() because that's undefined behaviour.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • So I made this as you said and it works, thank you again, but let me get this straight. Void game is working as a function that is actually the game right? And than you say to a compiler "listen start here with int main () and run game () function where the game code is running and than the game plays from void game () code? I want to understand the build structure. btw. It work really well and now I enjoy c++ :) – Siemieniaka Nov 28 '16 at 15:42
  • Can I place this void game code in a separate file and than place it into the int main code bracket? If yes, how can I do that? – Siemieniaka Nov 29 '16 at 16:02