Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Error in returning array of int as reference

Error in returning array of int as reference

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestion
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Korowai
    wrote on last edited by
    #1

    Hi, i have to ask a question about some codelines, where i have a mistake, because the array of int always returns 1 for every instance and i spent a lot of hours and still cannot find the fail i have maid. I simply post the necessary codelines in hope that someone can put away the wood infront of my head. Inside a modal dialogue, these are the calls

    for (int y=0;y<5;y++)
    {
    Schadensnummer[y]=KlendautKlendaut_Gegner[0].Basistrefferermittlung();
    }

    The class "KlendautKlendaut_Gegner0" - function Basistrefferermittlung:

    int CBasis::Basistrefferermittlung()
    {
    int Zufallstreffer;
    Zufallstreffer=rand()%99+1;
    int Trefferchance;
    Trefferchance=rand()%99+1;
    static int Einschlagsnummer;

    if (Trefferchance>80)
    {
        if (Zufallstreffer>95)
        {
        if (m\_Bodyguard>1)
        {
        Einschlagsnummer=20;
        m\_Bodyguard=m\_Bodyguard-1;
        return (Einschlagsnummer);
        }
        else if (m\_Bodyguard<1&&m\_Palast>1)
        {
        Einschlagsnummer=21;
        m\_Palast=m\_Palast-1;
        return (Einschlagsnummer);
        }
        }
        else if (Zufallstreffer>90&&Zufallstreffer<95)
        {
            if (m\_Fabrik>1)
        {
        Einschlagsnummer=16;
        m\_Fabrik=m\_Fabrik-1;
        return (Einschlagsnummer);
        }
        }
        else if (Zufallstreffer>85&&Zufallstreffer<90)
        {
        if (m\_Scanner>1)
        {
        Einschlagsnummer=17;
        m\_Scanner=m\_Scanner-1;
        return (Einschlagsnummer);
        }
        }
        else if (Zufallstreffer>80&&Zufallstreffer<85)
        {
        if (m\_Kraftwerk>1)
        {
        Einschlagsnummer=18;
        m\_Kraftwerk=m\_Kraftwerk-1;
        return (Einschlagsnummer);
        }
        }
        else if (Zufallstreffer>75&&Zufallstreffer<80)
        {
        if (m\_Zentrale>1)
        {
        Einschlagsnummer=19;
        m\_Zentrale=m\_Zentrale-1;
        return (Einschlagsnummer);
        }
        }
    
    }
    if (m\_Schutzwall>1)
    {
        Einschlagsnummer=1;
        m\_Schutzwall=m\_Schutzwall-1;
        return (Einschlagsnummer);
    }
    else if (m\_Gefechtsstand>1)
    {
        Einschlagsnummer=2;
        m\_Gefechtsstand=m\_Gefechtsstand-1;
        return (Einschlagsnummer);
    }
    else if (m\_Geschuetzturm>1)
    {
        Einschlagsnummer=3;
        m\_Geschuetzturm=m\_Geschuetzturm-1;
        return (Einschlag
    
    G D J 3 Replies Last reply
    0
    • K Korowai

      Hi, i have to ask a question about some codelines, where i have a mistake, because the array of int always returns 1 for every instance and i spent a lot of hours and still cannot find the fail i have maid. I simply post the necessary codelines in hope that someone can put away the wood infront of my head. Inside a modal dialogue, these are the calls

      for (int y=0;y<5;y++)
      {
      Schadensnummer[y]=KlendautKlendaut_Gegner[0].Basistrefferermittlung();
      }

      The class "KlendautKlendaut_Gegner0" - function Basistrefferermittlung:

      int CBasis::Basistrefferermittlung()
      {
      int Zufallstreffer;
      Zufallstreffer=rand()%99+1;
      int Trefferchance;
      Trefferchance=rand()%99+1;
      static int Einschlagsnummer;

      if (Trefferchance>80)
      {
          if (Zufallstreffer>95)
          {
          if (m\_Bodyguard>1)
          {
          Einschlagsnummer=20;
          m\_Bodyguard=m\_Bodyguard-1;
          return (Einschlagsnummer);
          }
          else if (m\_Bodyguard<1&&m\_Palast>1)
          {
          Einschlagsnummer=21;
          m\_Palast=m\_Palast-1;
          return (Einschlagsnummer);
          }
          }
          else if (Zufallstreffer>90&&Zufallstreffer<95)
          {
              if (m\_Fabrik>1)
          {
          Einschlagsnummer=16;
          m\_Fabrik=m\_Fabrik-1;
          return (Einschlagsnummer);
          }
          }
          else if (Zufallstreffer>85&&Zufallstreffer<90)
          {
          if (m\_Scanner>1)
          {
          Einschlagsnummer=17;
          m\_Scanner=m\_Scanner-1;
          return (Einschlagsnummer);
          }
          }
          else if (Zufallstreffer>80&&Zufallstreffer<85)
          {
          if (m\_Kraftwerk>1)
          {
          Einschlagsnummer=18;
          m\_Kraftwerk=m\_Kraftwerk-1;
          return (Einschlagsnummer);
          }
          }
          else if (Zufallstreffer>75&&Zufallstreffer<80)
          {
          if (m\_Zentrale>1)
          {
          Einschlagsnummer=19;
          m\_Zentrale=m\_Zentrale-1;
          return (Einschlagsnummer);
          }
          }
      
      }
      if (m\_Schutzwall>1)
      {
          Einschlagsnummer=1;
          m\_Schutzwall=m\_Schutzwall-1;
          return (Einschlagsnummer);
      }
      else if (m\_Gefechtsstand>1)
      {
          Einschlagsnummer=2;
          m\_Gefechtsstand=m\_Gefechtsstand-1;
          return (Einschlagsnummer);
      }
      else if (m\_Geschuetzturm>1)
      {
          Einschlagsnummer=3;
          m\_Geschuetzturm=m\_Geschuetzturm-1;
          return (Einschlag
      
      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      Use a debugger and step through the function. If a variable's value is not what you are expecting it is a lot easier to find out why by watching when it changes than by peering through the code looking for clues.

      K 1 Reply Last reply
      0
      • G Graham Breach

        Use a debugger and step through the function. If a variable's value is not what you are expecting it is a lot easier to find out why by watching when it changes than by peering through the code looking for clues.

        K Offline
        K Offline
        Korowai
        wrote on last edited by
        #3

        I did that. Debugger s showing the array is filled with "1" in each of the 5 instances.

        M 1 Reply Last reply
        0
        • K Korowai

          I did that. Debugger s showing the array is filled with "1" in each of the 5 instances.

          M Offline
          M Offline
          Midi_Mick
          wrote on last edited by
          #4

          You need to delve deeper in the debugger - look at where the array elements are being set to 1, and you should see why (Use F11 to step instead of F10).

          Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.

          K 1 Reply Last reply
          0
          • M Midi_Mick

            You need to delve deeper in the debugger - look at where the array elements are being set to 1, and you should see why (Use F11 to step instead of F10).

            Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.

            K Offline
            K Offline
            Korowai
            wrote on last edited by
            #5

            Hi, i made a step per step view on the code. It runs through "Schadensnummer[y]=RestonaGuldon_Gegner[0].Basistrefferermittlung();" and returns 1 after it doesn't enter the line it should

            //...
            if (m_Schutzwall>1)
            {
            Einschlagsnummer=1;
            m_Schutzwall=m_Schutzwall-1;
            return (Einschlagsnummer);
            }
            //...

            it should enter there, because the class array object has .m_Schutzwall>1. But it neither enters and sets Einschlagsnummer to 1 or subtracts the m_Schutzwall by 1. There must be something wrong with my declaration of the array of the class "RestonaGuldon_Gegner[0]" oder initialization of the function "Basistrefferermittlung()". But i can't see what it is. I'm blindfolded. I just did everything how i am used to do and it worked fine till now. I am stunned.

            L 1 Reply Last reply
            0
            • K Korowai

              Hi, i made a step per step view on the code. It runs through "Schadensnummer[y]=RestonaGuldon_Gegner[0].Basistrefferermittlung();" and returns 1 after it doesn't enter the line it should

              //...
              if (m_Schutzwall>1)
              {
              Einschlagsnummer=1;
              m_Schutzwall=m_Schutzwall-1;
              return (Einschlagsnummer);
              }
              //...

              it should enter there, because the class array object has .m_Schutzwall>1. But it neither enters and sets Einschlagsnummer to 1 or subtracts the m_Schutzwall by 1. There must be something wrong with my declaration of the array of the class "RestonaGuldon_Gegner[0]" oder initialization of the function "Basistrefferermittlung()". But i can't see what it is. I'm blindfolded. I just did everything how i am used to do and it worked fine till now. I am stunned.

              L Offline
              L Offline
              leon de boer
              wrote on last edited by
              #6

              Nowhere in your code can we see the defintion of "m_Schutzwall" it would be useful Are you sure you don't have two, m_Schutzwall's it's quite common to that and easy to miss. If you turn the compiler up to W4 maximum warning it will tell you if m_Schutzwall is hidden by a local copy. It's dead simple to do something silly with scope like this .. try it

              int m_Schutzwall = 1;
              int j;
              for (j = 1; j < 10; j++){
              int m_Schutzwall = j + 2;

              // Now at this point there actually two m\_Schutzwall's  and that is legal
              // The last m\_Schutzwall only exists in this scope 
              
              // You are in for a shock if you expect  m\_Schutzwall to be 1 in here
              

              }

              Do a search for in files for "m_Schutzwall" and look at all the uses.

              In vino veritas

              K 1 Reply Last reply
              0
              • L leon de boer

                Nowhere in your code can we see the defintion of "m_Schutzwall" it would be useful Are you sure you don't have two, m_Schutzwall's it's quite common to that and easy to miss. If you turn the compiler up to W4 maximum warning it will tell you if m_Schutzwall is hidden by a local copy. It's dead simple to do something silly with scope like this .. try it

                int m_Schutzwall = 1;
                int j;
                for (j = 1; j < 10; j++){
                int m_Schutzwall = j + 2;

                // Now at this point there actually two m\_Schutzwall's  and that is legal
                // The last m\_Schutzwall only exists in this scope 
                
                // You are in for a shock if you expect  m\_Schutzwall to be 1 in here
                

                }

                Do a search for in files for "m_Schutzwall" and look at all the uses.

                In vino veritas

                K Offline
                K Offline
                Korowai
                wrote on last edited by
                #7

                Is it possible that the array int Schadensnummer[] establishs a copy of the member variable of the class RestonaGuldon_Gegner[0]? The class is global, the variable m_Schutzwall is public. The class is capsulated in an array. I dont see a solution so far. it couldnt be that i have to give the array of the class in its own function. i think i forgot something, but i still cant see it. :(

                K 1 Reply Last reply
                0
                • K Korowai

                  Is it possible that the array int Schadensnummer[] establishs a copy of the member variable of the class RestonaGuldon_Gegner[0]? The class is global, the variable m_Schutzwall is public. The class is capsulated in an array. I dont see a solution so far. it couldnt be that i have to give the array of the class in its own function. i think i forgot something, but i still cant see it. :(

                  K Offline
                  K Offline
                  Korowai
                  wrote on last edited by
                  #8

                  addition: i feel like an idiot. Maybe i am one. There must be everything allright. It just misses a ">=" in the function instead of a ">", because the variable is equal 1. So the entrypoint is never beeing reached. I ll change it and then we ll see. Thank you so far, i hope i just gave you an evening with something to smile about. Thanks.

                  1 Reply Last reply
                  0
                  • K Korowai

                    Hi, i have to ask a question about some codelines, where i have a mistake, because the array of int always returns 1 for every instance and i spent a lot of hours and still cannot find the fail i have maid. I simply post the necessary codelines in hope that someone can put away the wood infront of my head. Inside a modal dialogue, these are the calls

                    for (int y=0;y<5;y++)
                    {
                    Schadensnummer[y]=KlendautKlendaut_Gegner[0].Basistrefferermittlung();
                    }

                    The class "KlendautKlendaut_Gegner0" - function Basistrefferermittlung:

                    int CBasis::Basistrefferermittlung()
                    {
                    int Zufallstreffer;
                    Zufallstreffer=rand()%99+1;
                    int Trefferchance;
                    Trefferchance=rand()%99+1;
                    static int Einschlagsnummer;

                    if (Trefferchance>80)
                    {
                        if (Zufallstreffer>95)
                        {
                        if (m\_Bodyguard>1)
                        {
                        Einschlagsnummer=20;
                        m\_Bodyguard=m\_Bodyguard-1;
                        return (Einschlagsnummer);
                        }
                        else if (m\_Bodyguard<1&&m\_Palast>1)
                        {
                        Einschlagsnummer=21;
                        m\_Palast=m\_Palast-1;
                        return (Einschlagsnummer);
                        }
                        }
                        else if (Zufallstreffer>90&&Zufallstreffer<95)
                        {
                            if (m\_Fabrik>1)
                        {
                        Einschlagsnummer=16;
                        m\_Fabrik=m\_Fabrik-1;
                        return (Einschlagsnummer);
                        }
                        }
                        else if (Zufallstreffer>85&&Zufallstreffer<90)
                        {
                        if (m\_Scanner>1)
                        {
                        Einschlagsnummer=17;
                        m\_Scanner=m\_Scanner-1;
                        return (Einschlagsnummer);
                        }
                        }
                        else if (Zufallstreffer>80&&Zufallstreffer<85)
                        {
                        if (m\_Kraftwerk>1)
                        {
                        Einschlagsnummer=18;
                        m\_Kraftwerk=m\_Kraftwerk-1;
                        return (Einschlagsnummer);
                        }
                        }
                        else if (Zufallstreffer>75&&Zufallstreffer<80)
                        {
                        if (m\_Zentrale>1)
                        {
                        Einschlagsnummer=19;
                        m\_Zentrale=m\_Zentrale-1;
                        return (Einschlagsnummer);
                        }
                        }
                    
                    }
                    if (m\_Schutzwall>1)
                    {
                        Einschlagsnummer=1;
                        m\_Schutzwall=m\_Schutzwall-1;
                        return (Einschlagsnummer);
                    }
                    else if (m\_Gefechtsstand>1)
                    {
                        Einschlagsnummer=2;
                        m\_Gefechtsstand=m\_Gefechtsstand-1;
                        return (Einschlagsnummer);
                    }
                    else if (m\_Geschuetzturm>1)
                    {
                        Einschlagsnummer=3;
                        m\_Geschuetzturm=m\_Geschuetzturm-1;
                        return (Einschlag
                    
                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    The title of this thread suggests you are dealing with some function that is returning an array of ints, yet your Basistrefferermittlung() function is simply returning a single int. Is that intentional?

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                    1 Reply Last reply
                    0
                    • K Korowai

                      Hi, i have to ask a question about some codelines, where i have a mistake, because the array of int always returns 1 for every instance and i spent a lot of hours and still cannot find the fail i have maid. I simply post the necessary codelines in hope that someone can put away the wood infront of my head. Inside a modal dialogue, these are the calls

                      for (int y=0;y<5;y++)
                      {
                      Schadensnummer[y]=KlendautKlendaut_Gegner[0].Basistrefferermittlung();
                      }

                      The class "KlendautKlendaut_Gegner0" - function Basistrefferermittlung:

                      int CBasis::Basistrefferermittlung()
                      {
                      int Zufallstreffer;
                      Zufallstreffer=rand()%99+1;
                      int Trefferchance;
                      Trefferchance=rand()%99+1;
                      static int Einschlagsnummer;

                      if (Trefferchance>80)
                      {
                          if (Zufallstreffer>95)
                          {
                          if (m\_Bodyguard>1)
                          {
                          Einschlagsnummer=20;
                          m\_Bodyguard=m\_Bodyguard-1;
                          return (Einschlagsnummer);
                          }
                          else if (m\_Bodyguard<1&&m\_Palast>1)
                          {
                          Einschlagsnummer=21;
                          m\_Palast=m\_Palast-1;
                          return (Einschlagsnummer);
                          }
                          }
                          else if (Zufallstreffer>90&&Zufallstreffer<95)
                          {
                              if (m\_Fabrik>1)
                          {
                          Einschlagsnummer=16;
                          m\_Fabrik=m\_Fabrik-1;
                          return (Einschlagsnummer);
                          }
                          }
                          else if (Zufallstreffer>85&&Zufallstreffer<90)
                          {
                          if (m\_Scanner>1)
                          {
                          Einschlagsnummer=17;
                          m\_Scanner=m\_Scanner-1;
                          return (Einschlagsnummer);
                          }
                          }
                          else if (Zufallstreffer>80&&Zufallstreffer<85)
                          {
                          if (m\_Kraftwerk>1)
                          {
                          Einschlagsnummer=18;
                          m\_Kraftwerk=m\_Kraftwerk-1;
                          return (Einschlagsnummer);
                          }
                          }
                          else if (Zufallstreffer>75&&Zufallstreffer<80)
                          {
                          if (m\_Zentrale>1)
                          {
                          Einschlagsnummer=19;
                          m\_Zentrale=m\_Zentrale-1;
                          return (Einschlagsnummer);
                          }
                          }
                      
                      }
                      if (m\_Schutzwall>1)
                      {
                          Einschlagsnummer=1;
                          m\_Schutzwall=m\_Schutzwall-1;
                          return (Einschlagsnummer);
                      }
                      else if (m\_Gefechtsstand>1)
                      {
                          Einschlagsnummer=2;
                          m\_Gefechtsstand=m\_Gefechtsstand-1;
                          return (Einschlagsnummer);
                      }
                      else if (m\_Geschuetzturm>1)
                      {
                          Einschlagsnummer=3;
                          m\_Geschuetzturm=m\_Geschuetzturm-1;
                          return (Einschlag
                      
                      J Offline
                      J Offline
                      Joe Woodbury
                      wrote on last edited by
                      #10

                      Some issues:

                      static int Einschlagsnummer;

                      Is never default initialized. Some code paths return nothing so whatever is in [e]ax gets returned, which may be 1. If Trefferchance is <= 80 and m_Schutzwall > 5, it will return 1 for each iteration up to five. However, you stated that m_Schutzwall is 1 which means it's never decremented to zero since the test is for m_Schutzwall > 1, not m_Schutzwall >= 1. This suggests that the return value is likely what happens to be in [e]ax. now nitpicking:

                      int Zufallstreffer;
                      Zufallstreffer=rand()%99+1;
                      int Trefferchance;
                      Trefferchance=rand()%99+1;

                      Why not initialize the variables in the declaration of them? Why

                      m_Schutzwall=m_Schutzwall-1;

                      instead of --m_Schutzwall;

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups