Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: C++ Switch Help :/  (Read 1097 times)

0 Members and 1 Guest are viewing this topic.
C++ Switch Help :/
« on: August 22, 2007, 08:16:52 pm »
  • All Hail Crappy Sprites!
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 34
Well I'm writing a text adventure in C++ (Cause they are easy to do, to a point) and I'm having a problem, I have used goto (yes, its a bad habit I know, just using it till I find an easier way) to make my code reverse itself so it automatically displays one of the sections that's needed to advandce the story right after a section is shown. This leads to using goto to et out, but it always messes up, it automatically closes. Heres my rather messy code for the section that it screws up on:

Code: [Select]
    switch ( inn1 ){
           case 1:
                inn11:
                cout << "\nAfter waiting for the receptionist, eventually an old hag appears. Sho appears  ";
                cout << "to have burn marks on her body. She says to you in an old crackling voice:\n";
                cout << "'Yes sir? Would you like a room? It's completely free.'\n";
                cout << "Do you reply:\n1.Yes, I would like a room\n2.No, i would not like a room\n>";
                cin >> speak1;
                goto speak1;
           case 2:
                cout << "\nThe newspaper is dated a few years back. It shows a report of a fire that only  ";
                cout << "happened... yesterday?! How could it be? This is an old newspaper, yet it shows ";
                cout << "a fire from yesterday? Even the date on the article shows yesterday! You throw  ";
                cout << "the newspaper on the desk and start waiting for the receptionist.\n";
                goto inn11;
           case 3:
                cout << "\nYou look in the box and find a Revolver with 6 bullets. You take it and load it ";
                cout << "before looking around in the box. After a short while you find a holster and    ";
                cout << "attach it to your belt. You place the gun in your holster and wait for the      ";
                cout << "receptionist to come.\nWeapon Obtained: Revolver\nRevolver Ammo +6\n";
                revolver = 1;
                revolverammo = 6;
                goto inn11;
           default:
                   cout << "That isn't a choice.Pick again";
                   cin >> inn1;
           }
    speak1:
    switch ( speak1 ){
           case 1:
                cout << "\nYou get the key to your room and instructions on how to get it. You head upstairs";
                cout << "and enter it. you close the door and suddenly you feel a strange drowsyness come";
                cout << "upon you. You suddenly topple over onto the bed and fall asleep, however, you   ";
                cout << "don't wake up. did you ever think to read the number on your key, 13...";
                cout << "\nYou have died, Game Over...";
                goto end;
           case 2:
                cout << "\nYou refuse her offer and she suddenly grabs a key from the shelf and tries to ram";
                cout << "into your hand. After a short struggle you shove her backwards and she topples  ";
                cout << "over into a cupboard which drops on her. You hear a crack as her skull is split ";
                cout << "open. You go to walk out but you hear groaning behind you. You turn to see the  ";
                cout << "woman stood behind the desk, she slowly shambles towards you, groaning madly\n";
                cin.ignore();
                if ( revolver = 1 ){
                   cout << "You quickly move your hand to the revolver in your holster and draw it out. You  ";
                   cout << "aim at her head and attempt to steady your hand. Do you wish to fire at her, even";
                   cout << "with your hand shaking slightly? [Y]es or [N]o?\n";
                   cin >> fight1;
                }
           default:
                   cout << "That isn't a choice.Pick again";
                   cin >> speak1;
           }
    end:
    cin.ignore();
    return EXIT_SUCCESS;
The section that meses up is the first 'case' of inn1, whenever you enter 1 for your choice, the next section doesn't appear, even though there is an cin.ignore() at the end of the code. I just tested it with a cin.ignore() in the main code, after 'You have died...' and it works fine, but taking away the 'goto end' and the cin.ignore in the end makes it show case 2 aswell.

What I want to know is why it messes up, it shouldn't mess up, should it? Or is it all goto's fault *waves fist at goto*
« Last Edit: December 25, 2007, 03:48:02 am by 4Sword »
Logged
~Project List~
-Untitled RPG - Barely Started!
Re: C++ Switch Help :/
« Reply #1 on: August 22, 2007, 08:28:42 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
Quote
cin >> speak1;
goto speak1;

You can't have speak1 as a variable and with goto, something must be wrong in there.

And BTW you shouldn't use goto very much. C++ enables you to use it but it's considered bad (I really don't know very well why, but everyone says it's a bad habit);
Logged
Re: C++ Switch Help :/
« Reply #2 on: August 22, 2007, 11:03:27 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
Quote
(I really don't know very well why, but everyone says it's a bad habit);

it's bad because in a complex program, it can lead the program anywhere.
Logged



i love big weenies and i cannot lie
Re: C++ Switch Help :/
« Reply #3 on: August 23, 2007, 06:19:17 am »
  • All Hail Crappy Sprites!
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 34
Ah k, well until I find a better solution, I'm gonna use it. KNowing my luck I'm gonna need to change it sometime :/

But anyway that goto speak1 is referencing to:

Code: [Select]
    speak1:
    switch ( speak1 ){
           case 1:

because it gets to that point, what I don't get is why it shows:

You get the key to your room and instructions on how to get it. You head upstairs
and enter it. you close the door and suddenly you feel a strange drowsyness come
upon you. You suddenly topple over onto the bed and fall asleep, however, you
don't wake up. did you ever think to read the number on your key, 13...
You have died, Game Over...
You refuse her offer and she suddenly grabs a key from the shelf and tries to ram
into your hand. After a short struggle you shove her backwards and she topples
over into a cupboard which drops on her. You hear a crack as her skull is split
open. You go to walk out but you hear groaning behind you. You turn to see the
woman stood behind the desk, she slowly shambles towards you, groaning madly

whenever I place a cin.ignore(); between these two lines of code:
Code: [Select]
                cout << "\nYou have died, Game Over...";
                goto end;
with no cin.ignore(); at the end.

I also don't get why it automatically shuts down if my code is as it is now, and why there is two cin.ignore();'s (between the code points and at the end of ALL the code) it functins fine! Urgh! Right now I'm thinking off messing around in VB, seeing if I can find a better tutorial than the microsoft one (which I have example projects saved, so i can reference them) so I can make this better looking when I finish it, unless I can learn graphics coding quickly :/
Logged
~Project List~
-Untitled RPG - Barely Started!
Re: C++ Switch Help :/
« Reply #4 on: August 23, 2007, 03:16:10 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
Quote
Ah k, well until I find a better solution, I'm gonna use it. KNowing my luck I'm gonna need to change it sometime :/

just use a while loop
Logged



i love big weenies and i cannot lie
Re: C++ Switch Help :/
« Reply #5 on: August 23, 2007, 07:07:38 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
try to change the line "goto end;" by "break;"
that might work...
Logged
Re: C++ Switch Help :/
« Reply #6 on: August 26, 2007, 05:00:55 pm »
  • 笑い男
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 2124
Code: [Select]
case 2:
                {
                cout << "\nYou refuse her offer and she suddenly grabs a key from the shelf and tries to ram";
                cout << "into your hand. After a short struggle you shove her backwards and she topples  ";
                cout << "over into a cupboard which drops on her. You hear a crack as her skull is split ";
                cout << "open. You go to walk out but you hear groaning behind you. You turn to see the  ";
                cout << "woman stood behind the desk, she slowly shambles towards you, groaning madly\n";
                cin.ignore();
                if ( revolver = 1 ){
                   cout << "You quickly move your hand to the revolver in your holster and draw it out. You  ";
                   cout << "aim at her head and attempt to steady your hand. Do you wish to fire at her, even";
                   cout << "with your hand shaking slightly? [Y]es or [N]o?\n";
                   cin >> fight1;
                }
                }

try that too, as if you try play about with variables or whatever in a case, you need to put {} around the lot
although im not sure if that is a problem in your case though and i may even be telling you stuff you already know >_<
Logged

この世に悪があるとすれば、それは人の心だ
  • .hack//The World
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.226 seconds with 49 queries.