Thread: Program error question??

  1. #1
    Unregistered
    Guest

    Program error question??

    what is wrong with this is keeps on crashing????


    #include <iostream>
    using namespace std;
    int main()

    {
    int answer;
    int answer3;
    char* answer2 = "terd";
    beg:
    cout <<"1= yes, 2= no, 0= quit"<< endl;
    cout <<"So do u like punk rock music?"<< endl;

    cin >> answer;
    switch (answer)
    {

    case 1:
    cout <<"That is good because that is a good kinda music!!"<< endl;
    goto pt2;
    break;
    case 2:
    cout <<"Well that is too bad, but oh well it is only music!!"<< endl;
    return 0;
    break;
    case 0:
    return 0;
    break;
    default:
    cout <<"You are a freaking idiot! That isn't even an option!! Try again!"<< endl;

    goto beg;
    }
    pt2:
    cout <<"So who is your favorite punk band?"<< endl;
    cin >> answer2;
    if (answer2=="anti-flag"||"Anti-Flag"||"antiflag"||"AntiFlag"||"Anti Flag"||"anti flag")

    {
    cout <<"Wow! " <<answer2<< " is my favorite band too!!!"<< endl;


    }

    else
    {
    cout <<"Yeah, " <<answer2<< " is pretty good. My favorite is anti-flag"<< endl;
    }
    beg2:
    cout <<"1= yes, 2= no, 0= quit"<< endl;
    cout <<"Do u play and instrument?"<< endl;

    cin >> answer3;

    switch (answer3)
    {
    case 1:
    cout <<"Oh that's cool what do u play?"<< endl;
    break;
    case 2:
    cout <<"Oh well i do so i am better than you!!!!!!!!"<< endl;
    return 0;
    break;
    case 0:
    return 0;
    break;
    default:
    cout <<"You ignorant *****!!! Try again you silly slut!!"<< endl;
    goto beg2;
    break;

    }







    //return 0;
    //if (answer==0)
    return 0;
    }

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    /me cries at sight of goto in c++ code

    first of all, if you want to use goto, get basic, not c++.

    secondly, use code tags in your post to make things easier to read.

    third, you cant compare strings with

    Code:
    if (answer2 == "antiflag")
    so use strncmp.
    hello, internet!

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    Yeah all of that was really hard to understand. You should try using more formal loops, like for() or something. And like moi said, you can't do this:

    Code:
    char * answer2=" ";
    
    ...
    
    if (answer2 == "blah")
    It just won't work. You can, however, include "string.h" into your program and then use strcmp() like moi also said:

    Code:
    #include <string.h>
    
    int main()
    {
         char * answer2=" ";
    
         ...
    
         if (strcmp(answer2,"blah")
              cout << "Wow, I like blah too!\n";
    Brendan
    Draco dormiens nunquam titillandus

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    char* answer2 = "terd";


    That is illegal. You should use strcpy or strncpy to do that. Those functions are included in string.h or cstring if you are using namespace std.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by golfinguy4
    char* answer2 = "terd";


    That is illegal. You should use strcpy or strncpy to do that. Those functions are included in string.h or cstring if you are using namespace std.
    Don't think so. The compiler will generate a const character array with size 5.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Another bug:
    Code:
    char* answer2 = "terd";
    ....
    cin >> answer2;
    You cannot safely assign new values to a string literal.

    See here: http://www.eskimo.com/~scs/C-faq/q1.32.html
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    cin >> answer2;

    This is not going to work. Like I explained in the previous post, answer2 is a const character array with size 5. You can not change this array. You need to allocate enough space for this array:
    Code:
    char answer2[100] = "terd";
    Or, like golfinguy4 told you:
    Code:
    char answer2[100];
    strcpy(answer2, "terd");

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>answer2 is a const character array
    Slight correction, answer2 is a char pointer, but it is initialised to point to a const char array (string literal). answer2 itself is not a const char array.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Talking

    Err.. yeah, sorry! It's 6:26pm, time to go home, and I'm also getting lazy

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Err.. yeah, sorry! It's 6:26pm, time to go home, and I'm also getting lazy
    Does everyone get lazy near the end of the work day? I tend to become more productive as the day wears on.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Does everyone get lazy near the end of the work day? I tend to become more productive as the day wears on.
    "Everyone"?

    Is this a question, or a blanket condemnation? Little hard to tell...

    Coming from someone who I believe would be a superb moderator, I hope it's a question.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is this a question, or a blanket condemnation?
    I consciously meant it as a question. Sub-consciously I have no idea, but the deep bowels of my mind are irrelevant, so it was a question.

    -Prelude
    My best code is written with the delete key.

  13. #13
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    I improve until a certain time. Once I reach that time, it is a straight drop and I suck (evidence is my earlier post which was posted late at night).

  14. #14
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    smooshy brain-hour is the best time to debug software IMO

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM