Thread: An error, what is it?

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    An error, what is it?

    OK, I'm still very new to C++ and I am making/trying to make a battle ship game.

    Everything had been going pretty well, until I added a function to check if a coordinate is already taken by another ship, my program just quit working. It still compiles and the command terminal comes up when I run it, but nothing happens.

    The function I added is this:
    Code:
    int check_taken ( string values, int num_aug )
    {
      int shipcords[5];
      int count = 0;
      while ( count < num_aug ) {
        int z;
        std::stringstream temp;
        temp << values.substr ( count,count+1 );
        temp >> z;
        shipcords[count] = z;
        count++;
      }
      int samecords = 0;
      for (int ct=0; ct<5; ct++) {
        for (int cth=ct+1; cth<5; cth++) {
          if (shipcords[ct] == shipcords[cth]) {
            ct = 5;
            cth = 5;
            samecords = 1;
          }
        }
      }
      if ( samecords = 1 ) {
        return 1;
      }
      else {
        return 0;
      }
    }
    If you don't think that this function is the problem. I can show you an instance where the function is used, or any other part of my code.

    Thanks,
    David

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    i have no idea how stringstreams work but if i were in your place...

    instead of...
    Code:
      for (int ct=0; ct<5; ct++) {
        for (int cth=ct+1; cth<5; cth++) {
          if (shipcords[ct] == shipcords[cth]) {
            ct = 5;
            cth = 5;
            samecords = 1;
          }
        }
      }
      if ( samecords = 1 ) {
        return 1;
      }
      else {
        return 0;
      }
    why not
    Code:
      for (int ct=0; ct<5; ct++) {
        for (int cth=ct+1; cth<5; cth++) {
          if (shipcords[ct] == shipcords[cth]) {
            return 1;
          }
        }
      }
      return 0;
    and do you know for sure that num_aug will always be 5 or less?

    (i see how dynamic memory is used now =D)

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by IdioticCreation
    If you don't think that this function is the problem. I can show you an instance where the function is used, or any other part of my code.
    Yes, a minimal context will help.
    Code:
    if ( samecords = 1 )
    Comparison is ==, assignment is =.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Yep, it will always be less then 5, as no ship in battle ship takes up more then 5 spaces.

    The only thing I don't understand here, how the return is used. Lets say, that the shipcords were equal to schipcords, and then it would return a 1, but wouldn't it still try and return a 0 as well.

    Does the function end right after the return?

    Well, either way it still wont work and I am starting to think I'm in over my head with this project. None the less here is a piece of code where you can see the function is used.

    Code:
        // -- Set battle ship
        while ( pobs == 1 ) {
          cout<< "\n\nSetup your board\n  Battle Ship(3) seperate coordinates with a space.\n";
          cin>> player_one_bs[0] >> player_one_bs[1] >> player_one_bs[2];
          cin.ignore();
          std::stringstream pobsstrcords;
          pobsstrcords<< player_one_bs[0] << " " << player_one_bs[1] << " " << player_one_bs[2];
          pobstaken = check_taken ( pobsstrcords.str(), 3 );
          pobsaligned = check_aligned ( pobsstrcords.str(), 3 );
          if ( pobstaken == 1 || pobsaligned == 1 ) {
            pobs = 1;
            cout<< "Oops, you eather did not pick adjacent coordinates or used a spot that was already taken\n\n\n";
          }
          else {
            pobs = 0;
          }
        }

  5. #5
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Dave, you are right. I'm not sure why my compiler didn't catch that, but I'm using howee's code now anyway, and it still isn't running.


    Oh and I was using samecords as true or false, I really should have just used boolean.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    a function end right after it encounters a return
    Last edited by h_howee; 11-27-2006 at 10:06 PM.

  7. #7
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Well, I read a little about common C++ errors, and I'm assuming that it wouldn't be a compile error, because my compiler would have alerted me (I think). So maybe it is a runtime error? Like segmentation fault, or insufficient memory.

    Anyone have any advice?
    Thanks

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    Please. Define "doesn't work". What happens ? Blue screen of death ? Segmentation fault ? Garbage output ? ..

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >Like segmentation fault, or insufficient memory.

    Insuffcient memory would mean overflow of the stack. A seg-fault in general is usually caused by invalid pointer / reference operations.

    >I'm not sure why my compiler didn't catch that

    Code:
    if ( i = 5 )
    To the compiler, this is correct by default. It assumes you are assigning the value 5
    to variable i. This is a common error, and it is up to you ( the programmer ) to be sure
    to test for equality "==" not assignment "=" when using conditions like these.

    Any good compiler, dependant on how the warnings are set up, will flag at least a warning if
    it thinks you are interpreting somthing incorrectly.
    Double Helix STL

  10. #10
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    swgh, I just said that because I thought my compiler had warned me before when I made that same mistake, but maybe I'm just going crazy

    Desolation, I don't know what a segmentation fault looks like, so I don't know if it is that. You should also know that it doesn't use any graphics/api what ever you call it. It is just in the little (usually black) terminal.

    Anyway when I run the program, which worked before the insertion of the previously mentioned function, the black terminal thing (I need to look up its name) comes up and the cursor just blinks at me.

  11. #11
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    It's probably waiting for input. Type something and press enter. BTW, a seg fault usually displays the Segmentation Fault message and terminates your programs.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    One thing I noticed, is that when you call the function, you pass it a string with 3 numbers (you should really just pass the array, and not convert it to string first and then back to array). In check_taken you parse it to an array of 5 ints, leaving last two unintialized. So they'll contain garbage, and the function's result will just be determined by chance.

    If nothing happens, may-be there's an infinite loop somewhere (can't see it though)? May-be your code doesn't reach some block at all? May-be you had some out-of-bounds problem before, but it didn't manifest it this way until you added a new function and recompiled?

  13. #13
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Ohh, that makes since. So, should I use a vector instead of an array?

    I don't know how it would work if I didn't convert then to a string, because it must be able to accept the coordinates of ships from 2 spaces long to 5 spaces long.

    Again thank you very much. I'm sure thats that problem.

  14. #14
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    no, he meant use an array of int instead of strings
    all you need are the coordinates of the two ends of the ship and you can calculate the rest from that

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  15. #15
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Wow......I feel......stupid.

    Thanks howee, you are very helpful for beginners Because you speak in terms I can understand.

    Thanks

Popular pages Recent additions subscribe to a feed