Compiling problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #16
    When you say doesn't output anything, do you mean it just stops doing anything, or the program stops without printing your last line?

    Comment

    • Trev17
      New Member
      • Feb 2007
      • 22

      #17
      Originally posted by DeMan
      When you say doesn't output anything, do you mean it just stops doing anything, or the program stops without printing your last line?
      The program itself doesn't stop, but it just goes to the next line without printing anything

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #18
        for a start try changing
        Code:
        while (num>=0)
        which causes your program to loop forever when num = 0
        to
        Code:
        while (num>0)
        also
        Code:
        if (true)
        {
                              will always execute this code 
        }

        Comment

        • Trev17
          New Member
          • Feb 2007
          • 22

          #19
          Originally posted by horace1
          Code:
          if (true)
          {
                                will always execute this code 
          }
          what if the int typed in is negative? wont it execute the else?

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #20
            No - you are not performing any test on the number.

            An if statement (and loops) require boolean logic to be executed. Usually this consists of a check such as num > 0, which evaluates the true when num is greater than 0. But what you have is if (true) - true always evaluates to true. True is never false. You should try something like this:

            Code:
            if (num is positive) {
               // Reverse using positive method
            } else { // num isn't positive, so it must be negative
               // Reverse using negative method
            }
            with your loops being the Reverse portion.

            Comment

            • Trev17
              New Member
              • Feb 2007
              • 22

              #21
              ahhh, that makes sense. so if i put the if statement like this:

              if ((num>0) == true)

              the else would make it false and the if statement would not come out true if i did input a negative. right?

              With the above code in it still returns 0 for negative numbers

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #22
                Originally posted by Trev17
                ahhh, that makes sense. so if i put the if statement like this:

                if ((num>0) == true)

                the else would make it false and the if statement would not come out true if i did input a negative. right?

                With the above code in it still returns 0 for negative numbers
                Correct. However, your test is redundant. Let's break it down:

                First, the inside parentheses will be evaluated. Is num > 0? Let's assume num is positive. Then num > 0 evaluates to true, so your if statement is essentially if (true == true). Well, true == true, so true == true evaluates to true. This final, single true determines if the statement will execute.

                But you can cut this short by just having num > 0 - if num is positive, this statement evaluates to true, and the if statement is satisfies.

                Again, if you keep it as is, it will work, but it takes more room for no gained productivity.

                Comment

                • Trev17
                  New Member
                  • Feb 2007
                  • 22

                  #23
                  theres still a problem somewhere in my reverseDigits function. I think it has to do with what i'm declaring in the while statement but it keeps giving me 0 as the output for a negative reverse function.

                  int reverseDigit(in t rev)
                  {
                  int num = rev, rnum = 0, digit;


                  while (num>0)
                  {
                  if (num>0)
                  {
                  rnum *= 10;
                  digit = num % 10;
                  rnum += digit;
                  num /= 10;
                  }
                  else
                  {
                  rnum *= 10;
                  digit = num % 10;
                  rnum += digit;
                  num /= 10;
                  rnum *= -1;
                  }
                  }
                  return rnum;
                  }

                  Comment

                  • Trev17
                    New Member
                    • Feb 2007
                    • 22

                    #24
                    i might be wrong on this but is it because there is no way for the else to ever work because its in the while which is greater than 0 to begin with.

                    If so, is it possible to just get rid of the while statement and just use the if statement?

                    Comment

                    • Trev17
                      New Member
                      • Feb 2007
                      • 22

                      #25
                      I fixed it, i changed while(num>0) to while(num) and i took out the rnum*= -1 at then end of the else statement. It seems to be outputting everything ok now.

                      I do have one more question, im suppost to have a slip of code in where it checks if you are putting in an integer amount. I have added code for this but it doesn't seem to be working properly, I have used this code for trying this:

                      int _tmain(int argc, _TCHAR* argv[])
                      {
                      int num;

                      cout << "Please enter an integer to be reversed: ";
                      cin >> num;
                      cout << endl;

                      if (num>INT_MAX || num<INT_MIN)
                      {
                      cout << "Error: Please use different integer" << endl;
                      cout << "Please enter an integer to be reversed: ";
                      cin >> num;
                      cout << endl;
                      }
                      cout << "The reverse order is: " << reverseDigit(nu m) << endl;

                      system("PAUSE") ;
                      return 0;

                      i have used #include <climits> as well. So i know thats not the problem.

                      Comment

                      Working...