If-else statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • socondc22
    New Member
    • Sep 2007
    • 67

    If-else statement

    I'm trying to use if-else statements to write a program that will print out sum of positive and negative numbers that are input into the program. It prints out extremely huge number when i run the program. Any idea why?
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    There could be a few different reasons for this, but I'm guessing that a variable you use is undefined, or goes through undefined behavior. Can we see the code inside your main statement?

    Comment

    • socondc22
      New Member
      • Sep 2007
      • 67

      #3
      i figured id just post the whole code....

      [CODE=cpp]#include <iostream>
      using namespace std;

      int main()
      {
      double num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, sum, sumpositive=0, sumpositive1, sumpositive2, sumpositive3, sumpositive4, sumpositive5, sumpositive6, sumpositive7, sumpositive8, sumpositive9, sumpositive10, sumnegative=0, sumnegative1, sumnegative2, sumnegative3, sumnegative4, sumnegative5, sumnegative6, sumnegative7, sumnegative8, sumnegative9, sumnegative10, mean;
      cout<< "Enter 10 Numbers:";
      cin>> num1;
      cin>> num2;
      cin>> num3;
      cin>> num4;
      cin>> num5;
      cin>> num6;
      cin>> num7;
      cin>> num8;
      cin>> num9;
      cin>> num10;
      sum=num1+num2+n um3+num4+num5+n um6+num7+num8+n um9+num10;
      cout<< "The sum of the numbers is: "<<sum<<".\ n";
      mean=sum/10.000;
      cout.setf(ios:: fixed);
      cout.setf(ios:: showpoint);
      cout.precision( 3);
      cout<<"The mean of the sum is: "<<mean<<". \n";
      {
      if (num1 >= 0)
      sumpositive1=su mpositive+num1;
      else
      sumnegative1=su mnegative+num1;
      if (num2 >= 0)
      sumpositive2=su mpositive1+num2 ;
      else
      sumnegative2=su mnegative1+num2 ;
      if (num3 >= 0)
      sumpositive3=su mpositive2+num3 ;
      else
      sumnegative3=su mnegative2+num3 ;
      if (num4 >= 0)
      sumpositive4=su mpositive3+num4 ;
      else
      sumnegative4=su mnegative3+num4 ;
      if (num5 >= 0)
      sumpositive5=su mpositive4+num5 ;
      else
      sumnegative5=su mnegative4+num5 ;
      if (num6 >= 0)
      sumpositive6=su mpositive5+num6 ;
      else
      sumnegative6=su mnegative5+num6 ;
      if (num7 >= 0)
      sumpositive7=su mpositive6+num7 ;
      else
      sumnegative7=su mnegative6+num7 ;
      if (num8 >= 0)
      sumpositive8=su mpositive7+num8 ;
      else
      sumnegative8=su mnegative7+num8 ;
      if (num9 >= 0)
      sumpositive9=su mpositive8+num9 ;
      else
      sumnegative9=su mnegative8+num9 ;
      if (num10 >= 0)
      sumpositive10=s umpositive9+num 10;
      else
      sumnegative10=s umnegative9+num 10;

      cout<<"The sum of the positive numbers is: "<<sumpositive1 0<<".\n";

      cout<<"The sum of the negative numbers is: "<<sumnegative1 0<<".\n";

      }

      return 0;
      }[/CODE]
      Last edited by Ganon11; Sep 13 '07, 07:51 PM. Reason: Please use the [CODE] tags provided.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Have you learned about looping yet? This is, to be honest, unnecessarily long, and could be simplified greatly with a loop.

        Comment

        • socondc22
          New Member
          • Sep 2007
          • 67

          #5
          our teacher started talking about looping but not enough so i could use it for this.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by socondc22
            our teacher started talking about looping but not enough so i could use it for this.
            Well, I was correct in my assumption at the beginning, you have undefined variables.

            for instance, run this program
            [code=cpp]
            #include <iostream>
            using namespace std;

            int main( )
            {
            int i_myInt;
            cout << i_myInt;
            return 0;
            }[/code]

            i_myInt will return a value, even though you never assigned it one. This is because when you declare it, it is given memory space, but it is not initialized (given a value). Therefore it takes on the value of whatever is in the memory space that was just allocated to it.

            So when you do

            sumnegative=sum negative+num1

            you are saying "put in 'sumpositive' the sum of what is already in 'sumpositive' and 'num1' which is giving you undefined behavior (which manifests itself this time as abnormally large numbers).

            I'm not sure if that's your only issue (you don't need the braces around the if-else chain, you can remove them with no effect), but that should get you going again.
            Last edited by sicarie; Sep 13 '07, 08:22 PM. Reason: Tying it to the program given, bolding for emphasis

            Comment

            • socondc22
              New Member
              • Sep 2007
              • 67

              #7
              im not quite sure what u meant.

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by socondc22
                im not quite sure what u meant.
                Did you run the little example program that was posted?

                Comment

                • socondc22
                  New Member
                  • Sep 2007
                  • 67

                  #9
                  the program didnt work when i ran it

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    Originally posted by socondc22
                    the program didnt work when i ran it
                    Did you get an error?

                    You can do it with your program too. Just print out any of the variables you didn't set to anything. (there were two or three you actually did set to 0)

                    Comment

                    • ilikepython
                      Recognized Expert Contributor
                      • Feb 2007
                      • 844

                      #11
                      Originally posted by socondc22
                      i figured id just post the whole code....

                      #include <iostream>
                      using namespace std;

                      int main()
                      {
                      double num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, sum, sumpositive=0, sumpositive1, sumpositive2, sumpositive3, sumpositive4, sumpositive5, sumpositive6, sumpositive7, sumpositive8, sumpositive9, sumpositive10, sumnegative=0, sumnegative1, sumnegative2, sumnegative3, sumnegative4, sumnegative5, sumnegative6, sumnegative7, sumnegative8, sumnegative9, sumnegative10, mean;
                      cout<< "Enter 10 Numbers:";
                      cin>> num1;
                      cin>> num2;
                      cin>> num3;
                      cin>> num4;
                      cin>> num5;
                      cin>> num6;
                      cin>> num7;
                      cin>> num8;
                      cin>> num9;
                      cin>> num10;
                      sum=num1+num2+n um3+num4+num5+n um6+num7+num8+n um9+num10;
                      cout<< "The sum of the numbers is: "<<sum<<".\ n";
                      mean=sum/10.000;
                      cout.setf(ios:: fixed);
                      cout.setf(ios:: showpoint);
                      cout.precision( 3);
                      cout<<"The mean of the sum is: "<<mean<<". \n";
                      {
                      if (num1 >= 0)
                      sumpositive1=su mpositive+num1;
                      else
                      sumnegative1=su mnegative+num1;
                      if (num2 >= 0)
                      sumpositive2=su mpositive1+num2 ;
                      else
                      sumnegative2=su mnegative1+num2 ;
                      if (num3 >= 0)
                      sumpositive3=su mpositive2+num3 ;
                      else
                      sumnegative3=su mnegative2+num3 ;
                      if (num4 >= 0)
                      sumpositive4=su mpositive3+num4 ;
                      else
                      sumnegative4=su mnegative3+num4 ;
                      if (num5 >= 0)
                      sumpositive5=su mpositive4+num5 ;
                      else
                      sumnegative5=su mnegative4+num5 ;
                      if (num6 >= 0)
                      sumpositive6=su mpositive5+num6 ;
                      else
                      sumnegative6=su mnegative5+num6 ;
                      if (num7 >= 0)
                      sumpositive7=su mpositive6+num7 ;
                      else
                      sumnegative7=su mnegative6+num7 ;
                      if (num8 >= 0)
                      sumpositive8=su mpositive7+num8 ;
                      else
                      sumnegative8=su mnegative7+num8 ;
                      if (num9 >= 0)
                      sumpositive9=su mpositive8+num9 ;
                      else
                      sumnegative9=su mnegative8+num9 ;
                      if (num10 >= 0)
                      sumpositive10=s umpositive9+num 10;
                      else
                      sumnegative10=s umnegative9+num 10;

                      cout<<"The sum of the positive numbers is: "<<sumpositive1 0<<".\n";

                      cout<<"The sum of the negative numbers is: "<<sumnegative1 0<<".\n";

                      }

                      return 0;
                      }
                      Ok, anytime you declare a lot of variables with mostly the same name it can probably be done with an array:
                      [code=cpp]
                      double nums[10];
                      int sumpositives[10];
                      int sumnegatives[10];
                      [/code]
                      To initialize to 0:
                      [code=cpp]
                      double nums[10] = {0.0};
                      int sumpositives[10] = {0};
                      int sumnegatives[10] = {0};
                      [/code]
                      Also, like Ganon said, it will be much easier if you use loops.
                      An example:
                      [code=cpp]
                      cout << "Enter 10 numbers: ";
                      for (int x = 0; x < 10; x++)
                      {
                      cin >> nums[x];
                      }
                      [/code]
                      Last edited by ilikepython; Sep 13 '07, 08:30 PM. Reason: added example

                      Comment

                      • socondc22
                        New Member
                        • Sep 2007
                        • 67

                        #12
                        Originally posted by sicarie
                        Did you get an error?

                        You can do it with your program too. Just print out any of the variables you didn't set to anything. (there were two or three you actually did set to 0)

                        What other variables do i need to set to 0? I'm just not understanding anything about this.

                        Comment

                        • socondc22
                          New Member
                          • Sep 2007
                          • 67

                          #13
                          my program is still coming out with really high numbers. What did you mean by print my other variables that aren't set to 0.

                          Comment

                          • Ganon11
                            Recognized Expert Specialist
                            • Oct 2006
                            • 3651

                            #14
                            Really, you should set ALL of your variables to 0, just to make sure they don't have any garbage values in them. Basically, what's happening is this:

                            Your computer has memory. There can be a lot of random stuff in memory - something like "01010101001001 000111000100110 010010100100" is probably not uncommon in memory locations identified as de-allocated (that is, not in use).

                            Now, when your program initializes a variable (let's call it sum), that variable has to be stored in memory somewhere. So the computer looks for a place in memory that is de-allocated (that is, not in use) and says, "I declare this space allocated! This space will be used for sum!" For our sake, let's assume sum is an int, and this computer stores ints with 8 bits. Now, when the computer allocates this memory (that is, marks it as being used), it doesn't change the bits inside. So the memory could look like "0110 1110" (which equals 110 in decimal), even though you haven't set anything to sum yet. If you try and use sum before you initialize it to 0, you will get 110, which obviously isn't what you want.

                            This is all what happens when you type

                            [CODE=cpp]int sum;[/CODE]

                            But, if you instead type

                            [code=cpp]int sum = 0;[/code]

                            Now the computer allocatres memory AND sets it to 0, so that the memory looks like "0000 0000" (which is, of course, 0 in decimal). Now you have a value that you expect in sum.

                            This problem is happening multiple times in your own program, causing many values to be used that you had no control over. Also, in reality, computers use more than 8 bits to store an int (usually 16 to 32 bits), which means there is a possibility of a very large number being represented there before initialization. This is likely what is giving you your strange results.

                            Comment

                            • socondc22
                              New Member
                              • Sep 2007
                              • 67

                              #15
                              that worked. But my negative number is coming out as 0 everytime i put a negative number in.

                              Comment

                              Working...