Calculator problem.

This is a discussion on Calculator problem. within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { std::cout<<"Input 2 numbers: "; int x, y; int upper, lower; std::cin ...

  1. #16
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        std::cout<<"Input 2 numbers: ";
        int x, y;
        int upper, lower;
     std::cin >> x >> y;
         if ( x < y) 
           x++;
              else 
              {
                   x > y;
                   y++;       
                      }
              std::cout <<"Range of numbers is: "<< x << " to " << y;
             std::cin.ignore();
              std::cin.get();
              
              return 0;
              }
    I got this for the code now, but i got he same problem if input is 10 1 then i get 10 2 for output and if its 1 10 i get 2 10 for output....

  2. #17
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    What do you mean by x>y? It has no meaning, it wont do anything.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #18
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    Lol, ok I've removed it. Hmm time for a visit to FAQ and tutorial haha... again...

  4. #19
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    Cant find anything, might be because im not totally sure what im looking for... hmm

  5. #20
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        std::cout<<"Input 2 numbers: ";
        int x, y;
        
     std::cin >> x >> y;
         if ( x < y) {
           x++;
           }
         
              else if
    
                  ( y < x)
                    x--;    
                      
                      else 
                      {
                           x == y;
                           std::cout <<"WTF!?!?!?";
                           
                           }
              std::cout <<"Range of numbers is: "<< x << " to " << y;
             std::cin.ignore();
              std::cin.get();
              
              return 0;
              }
    Got this now, haa, i feel good atleast half the thing is working... if i input 10 1 i get 9 1 if i input 1 10 i get 2 10. I want 2 9 or 9 2 tho..

  6. #21
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    How about
    Code:
    int x, y;
    int low, high;
    std::cin >> x >> y;
    if ( x < y ) {
      low = x; high = y;
    } else {
      low = y; high = x;
    }
    std::cout <<"Range of numbers is: "<< low << " to " << high;
    You probably need to make x == y a special case of some sort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #22
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    I am sorry, but I am confused as to what you are trying to do. This si what your code tells me:

    Code:
    1. Get two numbers from the user
        a. if x is less than y increase x by one
            i. print out range of numbers is x to y
        b if y is less than x decrease x by one a
            ii. print out range of numbers is x to y
        c. else (x must equal y)
           iii. x == y is is meaningless in this case because it has to be true, print out WTF
               then print out range of numbers is x to y
    I don't understand what you are trying to do.

    [edit] Oh, Salem just cleared it up for me. Yeah, that makes more sense. I would use his idea.

  8. #23
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    Salem, I love you. I understand it now, woot! I modified the code to
    Code:
     #include <iostream>
    using namespace std;
    int main()
    {
    int x, y;
    int low, high;
    std::cin >> x >> y;
    if ( x < y ) {
      low = x + 1; high = y - 1;
    } else {
      low = y + 1; high = x - 1;
    }
    std::cout <<"Range of numbers is: "<< low << " to " << high;
    std::cin.get();
    std::cin.ignore();
    return 0;
    }
    So now it gives me the output 2 9 if the input is 1 10 and vice versa if anyone wanted to know.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21