The world is doomed when computers take over!...

This is a discussion on The world is doomed when computers take over!... within the C++ Programming forums, part of the General Programming Boards category; Well I did what you told me I used a double int thing. Well ok double var_name. And well I ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    The world is doomed when computers take over!...

    Well I did what you told me I used a double int thing. Well ok double var_name. And well I had a user put in 2 numbers (the user was me) and then pick a operation. I only had + sence it was a test. But anyways I added to small numbers 2+2. And guess what it equalled. NOT 4! it was over 2000! I will give the code I used...


    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <windows.h>
    int main()
    {
        char operation;
        
        long double number_1;
        long double number_2;
        long double number_3;
        long double number_4;
        long double resault_1;
        long double resault_2;
        
        number_1 = 0;
        number_2 = 0;
        number_3 = 0;
        number_4 = 0;
        
        
        cout<<"Number 1: ";
        cin>>number_1;
        
        cout<<"Operator: ";
        cin>>operation;
        
        cout<<"Number 2: ";
        cin>>number_2;
        
        
        if(operation==1)
            resault_1=number_1+number_2;
           
        
        
        cout<<resault_1;
        
        
        
        
        
        getch();
        return 0;
    }

    I know how I pick the operation is wierd but like I said this is a test. You can try it and see the resaults if you want.


    Edit: Why does it do that?

    Edit Edit: It also does that if I take away the longs before the doubles.

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    I did this

    Code:
    #include <iostream>
    
    int main ( void ) 
    {
    
        long double number_1;
        long double number_2;
        long double result;
    
        std::cout << "Number 1: ";
        std::cin  >> number_1;
    
        std::cout << std::endl;
    	
        std::cout << "Number 2: ";
        std::cin >> number_2;
    
        std::cout << std::endl;
    
        result = number_1 + number_2;
    
        std::cout << result;
    
        return 0;
    
    }
    And I did

    Code:
    
    Number 1: 2
    Number 2: 2
    4
    
    [edit]
    Ah, try operator == '1';

    When you input 1 the numeric value is atcually 0x31.
    You need the quotes since you declared operator as a char.
    Last edited by Vicious; 09-15-2004 at 02:24 PM.
    What is C++?

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    This never happened...


    But when I put quotes on the 1 it says ISO forbids the delcoration of pointer to integer or something like that.

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    When it asks you to input the operation... do you type 1?
    What is C++?

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    You know how they say you learn something every day...

    Well I learned 2 things...


    edit I thought I had to use " instead of '. BUt I found out you have to use '.

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Here I fixed it all for you

    Code:
    // Standard Header
    #include <iostream>
    using namespace std;
    
    int main ( void )
    {
        
        char operation;
        
        long double number_1;
        long double number_2;
    
        // Corrected a small typo :)
        long double result_1;
        
        number_1 = 0;
        number_2 = 0;    
        
        cout << "Number 1: ";
        cin  >> number_1;
        
        cout << "Operator: ";
        cin  >> operation;
    
        cout << "Number 2: ";
        cin  >> number_2;
    
        cout << endl;
        
        
        if ( operation == '+' )
            result_1 = number_1 + number_2;
           
        
        cout << result_1;  
        
        // Replaced getch() with a standard function
        cin.get();
        return 0;
    
    }
    There, now it is all standard and pretty

    [edit]
    Oh, your problem was caused because since operation didnt actually equal numeric 1, result was never initialized. So the result was undefined.

    [edit2]
    Notice I put '+' , so when it asks for operation you literally put +...
    Last edited by Vicious; 09-15-2004 at 02:40 PM.
    What is C++?

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    can't I use getch()? I like becuase it stands out and I first leanred that and cin.get and getch() do the same thing and no one will know but you guys so why does it matter so much?


    But anyways...

    Is there a way to prevent the number from going into scientific notaion or what this pic shows. Becuase I want it to show a plain flat out number.

    Edit: The number below the 111111111+111111 is what it equals.


    Oh and thanks for your help again!
    Attached Images Attached Images  

  8. #8
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    The only way I know of would be to use printf () to format it, because cout will automatically use scientific notation if needed.

    BUT, the number would be so large that you wouldnt get an acurate number..

    It would be like -53545252 or something...

    So im not really sure how to do really large numbers without Scientific Notation.

    [edit]
    Yeah you can use getch().. just dont tell anybody
    As long as you understand that its not standard and a really bad habbit.
    What is C++?

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You can format the output of cout using manipulators in <iomanip>.

    And in the code above, I would initialize result_1 to 0 when it is defined because if the operation is wrong you will get whatever the unitialized value was. Since your code allows for this possibilty, then you should intialize the variable. I would also wait to declare/define it until just before it is used - e.g. before the if statement that checks the operation.

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    nope printf() gives me this error

    33 C:\Documents and Settings\Alex\Desktop\Math Helper\main.cpp cannot convert `long double' to `const char*' for argument `1' to `int printf(const


    and the peice of code is

    printf(resault_1);

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    If you use printf correctly it won't give you an error.

    But use cout anyway as long as you are using it for the rest of your input/output. Just look up io manipulators in your book or tutorial and learn about the different options available to you. Chances are that several of them will help you get the output you want.

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    I'm googleling it right now...

  13. #13
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Well the best I got so far is...

    I extended the number by about half more. So it shows about half more then it did before. I am still going for more tho.

  14. #14
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Yeah sorry about suggesting printf ();

    I didnt even think about iomanip. I dont know why
    What is C++?

  15. #15
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    lol oh well. I am still looking into other ways. I already tried a .txt file. didn't work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Numeric addresses for computers
    By great in forum C Programming
    Replies: 4
    Last Post: 08-23-2010, 11:53 AM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 04:39 PM
  3. Computers as authors
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-22-2004, 07:55 PM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  5. Too much to ask ?
    By deflamol in forum C Programming
    Replies: 2
    Last Post: 05-06-2004, 04:30 PM

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