3 Replies - 253 Views - Last Post: 02 February 2018 - 03:53 PM Rate Topic: -----

#1 GabrieLL   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-February 18

I don't know what's wrong with my code

Posted 02 February 2018 - 02:30 PM

I'am running this program on code blocks,
I don't know whats wrong with the code it might be confusing to explain, I recommend you run it to know what I mean. when you choose the first path (1)
it goes along with what it's supposed to do. but when you make the second choice of the of the first path (1>1 or 1>2) it reads: "you stumble across a fork in the road even though that's dialogue for the second path. please help I don't know whats going on.

#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;

int main()
{

    char name[50];
    cout << "They're coming! Yells the scout." << endl;
    cout << "Hold down the gates as long as you can!" << endl;
    cout << "you see an army of white cross imperials" << endl;
    cout<< "(or locusts as they're called) advancing up the hill." << endl;
    cout << " " << endl;
    cout << "<---------------------------->" << endl;
    cout << " " << endl;
    cout << "Okay before we can move on with the story," << endl;
    cout << "I'am gonna have to kinda know your name, being the narrator and all." << endl;
    cin.getline(name, 50);
    cout << " " << endl;
    cout << "You better move fast, "<< name <<". The locusts are attacking the city." << endl;
    cout << "\n----------------------Press any key to continue----------------------" << endl;
    _getch();


system("cls");
    int choiceOne_Path;
    cout << "# What's the plan?" << endl;
    cout << "\t >>1. Defend the village" << endl;
    cout << "\t >>2. Get outta There!" << endl;
    retry:
    cout << "\nChoose 1 or 2: ";
    cin >> choiceOne_Path;
    if(choiceOne_Path == 1)
    {
        cout << "\n!!!----------------------Chapter One: Attack----------------------!!!" << endl;
        cout << "\nChief:  hey "<<name<<" get in formation!" << endl;
        cout << "You: I don't have a weapon yet!  " << endl;
        cout << "# You run behind the chief." << endl;
         cout << "#You run around the battlefield helpless and scared looking for a weapon." << endl;
    }
    else if(choiceOne_Path == 2)
    {
        cout << "\n!!!----------------------Chapter One: Escape----------------------!!!" << endl;
        cout << "\nYou: I'am outta here!" << endl;
        cout << "Chief: where are ya going your'e gonna get killed out there!" << endl;
        cout << "You: I know my way out." << endl;
        cout << "Chief: Hey "<<name<<" come back!" << endl;
        cout << "#You climb over a broken wall and run off towards the woods." << endl;
         cout << "<--------------------------------> " << endl;
        cout << "You manage to get out of there alive which is great except for the fact that you betrayed your village. " << endl;
    }
    else
    {
        cout << "You're doing it wrong! Press either '1' or '2', nothing else!" << endl;
        goto retry;
    }



    cout << "\n----------------------Press any key to continue----------------------" << endl;
    _getch();

    system("cls");
    switch(choiceOne_Path)
    {
        case 1:  system("cls");
    int choiceOne_Path;
    cout << "# You find two corpses one with a bow and one with a sword" << endl;
    cout << "\t >>1. Take the bow" << endl;
    cout << "\t >>2. Take the sword" << endl;
    cout << "\nChoose 1 or 2: ";
    cin >> choiceOne_Path;
    if(choiceOne_Path == 1)
    {
        cout << "\nChief:  hey "<<name<<" I said get in formation!" << endl;
        cout << "# you take aim and shoot" << endl;
         cout << "#you managed to kill 3 locusts" <<endl;
    }
    else if(choiceOne_Path == 2)
    {
         cout << "\nChief:  hey "<<name<<" I said get in formation!" << endl;
        cout << "#  You charge into battle" << endl;
        cout << "# you managed to kill 5 locusts before getting stabbed in the ribs" <<endl;
        cout << "\nChief: hey "<<name<<" are you okay?"<<endl;
        cout << "\nYou it's okay, it's just a flesh wound" <<endl;
    }
    else
    {
        cout << "You're doing it wrong! Press either '1' or '2', nothing else!" << endl;
        goto retry;
    }

            break;
        case 2: cout << "#You walk deeper into the forest (possibly lost)" << endl;
            break;
    }
    system("cls");
    cout << "#you keep walking until you come across a fork in the road" << endl;
    cout << "\t >>1. go left" << endl;
    cout << "\t >>2. go right" << endl;
    cout << "\nChoose 1 or 2: ";
    cin >> choiceOne_Path;
    if(choiceOne_Path == 1)
    {
         cout << "# You keep walking on the left path until you see a" << endl;
         cout << " dwarf village further up the road." << endl;
    }
    else if(choiceOne_Path == 2)
    {
        cout << "# You keep walking on the left path until you realize that you" << endl;
        cout << "are extremely thirsty and might collapse if you don't get something to drink" << endl;
    }
    else
    {
        cout << "You're doing it wrong! Press either '1' or '2', nothing else!" << endl;
        goto retry;
    }

    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: I don't know what's wrong with my code

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14091
  • View blog
  • Posts: 56,456
  • Joined: 12-June 08

Re: I don't know what's wrong with my code

Posted 02 February 2018 - 02:42 PM

Why is 'choiceOne_Path' declared in different scopes and places?

027    int choiceOne_Path;
068    int choiceOne_Path;


Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,452
  • Joined: 05-May 12

Re: I don't know what's wrong with my code

Posted 02 February 2018 - 02:52 PM

And as for why you are getting to line 99, the reason is because you exit the switch statement when you hit the break on line 94. What were you expecting to happen?

As an aside, why are you using goto's ? Modern C++ writing style abhors the use of goto's. I recommend restructuring your code rather than getting sucked in the habit of writing code that will eventually become spaghetti code.
Was This Post Helpful? 0
  • +
  • -

#4 tony jay   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 50
  • Joined: 31-January 18

Re: I don't know what's wrong with my code

Posted 02 February 2018 - 03:53 PM

To enlarge on Skydivers comments, goto, is not only bad style, but exceedingly poor engineering.
Also it is entirely unnecessary.

Simply put - a combination of sequence, selection, and iteration can create any flow of logic required without the need of goto.

Some people argue that goto may provide performance benefits, but, a much larger argument is in play here. Any program employing goto with any regularity rapidly becomes impossible to maintain or extend because the logic flow becomes impossible to predict.

As with anything in software engineering to tell someone to never or always do something is invalid. There are always rare edge cases that might contradict more generally accepted wisdom.

However, your program can, and should, be re-organised without any goto's. And it can be redone pretty easily.
And, furthermore, if you can really find program logic going forward that can only work by using goto write it up and get it published...
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1