whats wrong with this

This is a discussion on whats wrong with this within the C++ Programming forums, part of the General Programming Boards category; Hello, The goto statement was once widely used. Unfortunately code that used goto extensively was poorly structured and could easily ...

  1. #16
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    The goto statement was once widely used. Unfortunately code that used goto extensively was poorly structured and could easily become unmanageable, and is known as "spaghetti code". Today, programming languages provide other ways to structure code so goto is rarely needed. In C++ it is always possible to use something other than goto to achieve what you need.

    Note that the use of goto is a religious issue and has provoked a great deal of debate. In C++ there is always a better alternative for writing loops, so you don't need to use goto at all. There are other circumstances where goto may be more appropriate than the alternatives, though that is a seperate subject. Keep in mind when you use it to exit a block which contains aggregates requiring destructors, the destructors will run before the goto transfers control. The compiler still forbids using goto to enter a scope that requires constructors.

    The indiscriminate use of goto statements has caused tangled, miserable, impossible-to-read programs. To avoid the use of goto, more sophisticated, tightly controlled looping commands have been introduced: for, while, and do-while. Using these makes programs more easily understood, and goto is generally avoided, but one might argue that the case has been a bit overstated. Like any tool, carefully used and in the right hands, goto can be a useful construct, and the ANSI committee decided to keep it in the language because it has its legitimate uses.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  2. #17
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    funny how people correct people then miss one thing so they get corrected and so on and so forth. i fixed it so it works but i have a few probs...
    o and i know i miss spelled most of the words (i did it by memory and did it very fast and did not recheck it sry ^^; )
    Code:
    #include <iostream> // first guy used <iostream.h> which my compiler did not understand
            
    using namespace std;   //  first guy also left this out
            
    int main()
    {
        int game, halo, cs, cod;
    
    error:
        cout<<"Please input your game to be rated: \n 1 for halo 2 for CS 3 for CoD \n";
        cin>> game;
        switch (game) {
        case 1:
            cout<<"please enter kills in halo:\n";
            cin>> halo;
            if ( halo <= 1 ) {
                cout<<"god damn you suck!!!\n";
            }
            else if ( halo <= 10 ) {
                cout<<"new arent you!?\n";
            }
            else if ( halo >= 11 ) {
                cout<<"getting good.\n";
            }
            else if ( halo > 23 ) {
                cout<<"lets play i need a good player! \n";
            }
            else if ( halo >= 55 ) {
                cout<<"CAMPER!!! stop camping!! \n";
            }
            cin.get();
            break;
        case 2:
            cout<<"input kills in cs: \n";
            cin>> cs;
            if ( cs <= 1 ) {
                cout<<" newbie. \n";
            }
            else if ( cs >= 8 ) {
                cout<<"getting better! \n";
            }
            else if ( cs >= 15 ) {
                cout<<"i'd play with you but i would win. \n";
            }
            else if ( cs >= 25 ) {
                cout<<"could be cheating at this point but not sure how... \n";
            }
            else if ( cs >= 40 ) {
                cout<<"total hacker! stop killing bots! they're people too. \n";
            }
            cin.get();
            break;
        case 3:
            cout<<"please input number of kills: \n";
            cin>> cod;
            if ( cod <= 2 ) {
                cout<<"try shooting in the other direction. \n";
            }
            else if ( cod >= 10 ) {
                cout<<"not bad \n";
            }
            else if ( cod >= 20 ) {
                cout<<"sharp shot. \n";
            }
            else if ( cod >= 30 ) {
                cout<<"war machine!! \n";
            }
            else if ( cod >= 40 ) {
                cout<<"hitler himself. \n";
            }
            else if ( cod >= 70 ) {
                cout<<"somehow you are cheating or playing with noobs \n";
            }
            cin.get();
            break;    // removed the goto cus it did not work, also the 
                          // program shuts down right after the text is shown
         }
    }
    thx to this thread i understand switch case!! yay, this will make things easyer!
    ok well like i said i dont know why it closes right after the text is shown and im kinda in the dark at this point...

    thx for all the help so far!! B]

  3. #18
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    If it is closing right after the text is shown it is probably because the input buffer still has a character in it - at least that's how I've been made to see it.

    Try putting cin.ignore() before cin.get();

    Or you could even include <conio.h> and then use getch();

    goto is a deprecated control structure. S.O. explained it perfectly. It's NOT used in C++ because it is old and far inferior to the loops we have in C++.

    If you want to use goto so badly, take up C

  4. #19
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    Do keep in mind that getch() is a C function, and non-portable. conio.h is not in the ANSI standard library and does not appear on the Sun or Linux machines. This applies to all functions of conio.h, such as clrscr(), getch(), and getche().


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #20
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    thx so far! it will in fact run but will not loop how would i do that?
    i cant seem to get a clear answer and the tutorial does not have a good example. i want an exit option so ill make a 4th case, then just like this:
    Code:
    case: 4
    cout<<"thanks for useing game rater!\n";
    cin.get()
    break;
    right? it wont run like this and like i said this is kinda going over my head in the tutorials becouse they dont use it in a program slightly related to each other and im not sure where to put the code and what veriables to use. i'll look around to see if any FAQ has the answer.
    thx again!!

  6. #21
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Once you learn some more stuff you should try to build on this program. Do stuff like write the results to a file.
    My computer is awesome.

  7. #22
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    What i did when i wanted my program to redo, (without using loops), i just put in sum swich cases in each int and had y / n to restart program


    here is a part where i used it
    Code:
     char input3;
      cout<<"Start over y / n :\n";
      cin>> input3;
      switch ( input3 ) {
      case 'y':            
        main();
        break;
      case 'n':            
        cout:"/n";
        break;
      default:            
        cout<<"Error\n";
        break;
      }
     }
    notice char input3; (not int input3; )
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  8. #23
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    thx for posting an answer but im not sure how to use it in my code cus o the fact im new to c++ so maybe some comments would help if you would.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21