Thread: Strange error

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    Strange error

    Code:
      int fin = 0;
      while (fin == 0)
      {
        int choice;
        cout << "What do you want to do: " << endl;
        cout << "1. Gain strenght\n" << "2. Gain agility\n" << "3. Gain intelligence\n" << "4. Leave city\n";
        cout << "Choice: "; cin >> choice;
      
        switch(choice)
        {
          case 1:
            cout << "You have gained 1 strength!" << endl;
            s++;
            times++;
            break;
          case 2:
            cout << "You have gained 1 agility!" << endl;
            ag++;
            times++;
            break;
          case 3:
            cout << "You took lessons! Intelligence +1!" << endl;
            intel++;
            times++;
            break;
          case 4:
            fin = 1;
            break;
        }
      }
    This source code doesn't give compiler errors but when I type "4"
    It says critical error and the program shuts down. What is wrong?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    If I had to guess. It's whatever comes after the loop that's causing it. Looking at that, I didn't see anything wrong. I might be slipping over something, though.
    Sent from my iPadŽ

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Did you try that in isolation in a main() of it's own, or is it part of some much larger program?
    If it's the latter, the problem is likely to be elsewhere, because in isolation it works for me and seems OK.

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    int city(string c, int s, int ag, int intel)
    {
      int direction = 0;
      int times;
      int fin = 0;
      string *dirs;
      while (fin == 0)
      {
        int choice;
        cout << "What do you want to do: " << endl;
        cout << "1. Gain strenght\n" << "2. Gain agility\n" << "3. Gain intelligence\n" << "4. Leave city\n";
        cout << "Choice: "; cin >> choice;
      
        switch(choice)
        {
          case 1:
            cout << "You have gained 1 strength!" << endl;
            s++;
            times++;
            break;
          case 2:
            cout << "You have gained 1 agility!" << endl;
            ag++;
            times++;
            break;
          case 3:
            cout << "You took lessons! Intelligence +1!" << endl;
            intel++;
            times++;
            break;
          case 4:
            fin++;
            break;
          default:
            cout << "Wrong input!" << endl;
            break;
        }
      }
      
      if(c == "Rivendell")
      {
        dirs[0] = "Amon Hen";
        dirs[1] = "Ashenvale";
        dirs[2] = "Mechanica";
        dirs[3] = "Lok-tar";
      }
      cout << "In which direction do you want to leave?\n1." << dirs[0] << "\n2. " << dirs[1] << "\n3. " << dirs[2] << "\n4. " << dirs[3] << endl; 
      cin >> direction;
      return direction;
    }
    Whole function "city".
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your dirs isn't allocated any memory, so you just write to some random place.

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Problem solved!

    Code:
    string dirs[4];
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Do you know why you decided to make it a pointer in the first place?
    Sent from my iPadŽ

  8. #8
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I got an error message with something like a pointer in it, so I made it a pointer and the error was gone! :P
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if(c == "Rivendell")
    What are you expecting to do if this test fails.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM