Thread: A easy to answer question...I think...

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

    A easy to answer question...I think...

    Ok I have started fresh last night. I got throw the first 5 lessons wich I hate reading, so I'm amazed I got this far. But I have noticed that in the simple multiply 2 numbers program.

    Code:
    #include <iostream.h>
    #include <conio.h>
    int mult(int x, int y);
    int main()
    {
        int x, y;
        cout<<"please input two numbers to be multiplied: ";
        cin>>x>>y;
        cout<<"The product of your two numbers is "<<mult(x, y);    
        getch();
        return 0;
    }
    int mult(int x, int y)
    {
        return x*y;
    }
    That you can only multi so big of numbers, other wise it will mess up and say things like -123321123 or something like that.

    Why is that. And how do I fix it, if I can fix it.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    That's because when you use variables (specifically when you declare them) the compiler is allocating a certain amount of space. For example, a char is one byte in length, which means there are 256 (2^8 (base 2, 8 bits)) possible combinations - and there are 256 ASCII characters. The size of ints vary depending on the compiler, but you can use modifiers like short and long to be more specific. You could use 'long int's instead of just 'int's, and that would give you a greater range, but there are still limits. If you limit yourself to positive numbers, you can use the unsigned modifier and get double the range into positives, but no negatives. Also note that it's not just the numbers you are multiplying together than need to be within the range. To be formatted properly, your answer must be too.

    [code]
    long int x,y; // Gives you the maximum range, 2^32, I believe
    unsigned int x, y // Example of a positive-only variable
    Last edited by sean; 08-20-2004 at 12:43 PM.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Alrighty. SO could I go unsigned long int ? or is one or the other? and what does the ^ mean I forgot, or just never knew?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Alrighty. SO could I go unsigned long int ?
    I don't see why not. Try it and see.

    and what does the ^ mean I forgot, or just never knew?
    In C++ it's bitwise OR (if you don't know, just keep up with the tuts and you'll get to it eventually), but I was using it as "2 to the power of 8", as it is commonly used for that.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ahh ok now I get it. Thanks and when I compile a code with dev-C++ there are to many simple errors. Is there any way to turn the error checking down a little so it goes past the things that don't really matter? Or something like that?

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Things that don't really matter are called warnings. Things that just make your program act wierd are just mistakes. Errors are things that cause serious problem in translating your code into machine code. Often it's just a typo. If you understand what you've just been taught in a tutorial, you should be able to double click the error, have the line on which the problem is highlighted, and then read the message to try and figure out what went wrong. Sometimes if you forgot to declare a variable, the error will occur on the line it's first used, and not where you should've declared it.

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    alrighty...so what if ther is a syntax error at the end of the input and then I double click it and it brings me to the last barcket wich ends the program and doesn't highlet notin but only puts the curser to the left of the bracket.

    Whats wrong then?

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well you'd have to show me your code for that...

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation The standard specifies a minimum capacity...

    The C++ language standard says that an int must hold at least two bytes. This means that it will be two or more bytes on your system. Of course it is good practice NOT to exceed what the standard specifies, as this keeps your code portable.

    Floating point types (floats and doubles) can hold huge numbers. But, you can get weird results... something like 2+2 = 3.999999999, because the number of digits (bits, actually) is still limited... and it's done with mirrors (exponents, actually).

    Look-up sizeof() and <limits>.

    Here are the guaranteed capacities:
    char -128 to +127 (1 byte)
    unsigned char 0 to 255 (1 byte)

    int -32,768 to +32,767 (2 bytes)
    unsigned int 0 to 65535 (2 bytes)

    long -2,147,483,648 to +2,147,438,647 (4 bytes)
    unsigned long 0 to 7,294,967,295 (4 bytes)

    float +/- 2.1 E-38 to +/- 3.4 E38 (4 bytes)
    double +/- 2.2 E-308 to +/- 1.8 E308 (8 bytes)
    Last edited by DougDbug; 08-20-2004 at 02:53 PM.

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Ok here is the code. I took most of the middle sence it is very repetive and I am only using this to get used to the basics.


    Code:
    #include <iostream.h>
    #include <conio.h>
    int main()
    {
        int age;
        int show;
        {
        show=0;
        cout<<"Enter your age: ";
        cin>>age;
        if(age==1)
        ...
    
    
        ...
        if(age<13)
        if(show==0)    
        {
            cout<<"You are a pre teen";
            show=1;
        }   
        getch();
        return 0;
    }

    Ok so the error is...

    getch();
    return 0;
    (here)}

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
        int show;
        { <----------------------
        show=0;
    Take that out. Unless I need to see the rest of your code, you never have a matching }. The reason you recieve the error where you did is because that is where the compiler realizes something is wrong. This is also an example of why all errors can't be 'ignored'. Based on assumptions, there's an awful lots of ways to interpret that code with out the matching }

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    OMG you are like the C++ master! Or at least to me you are!

    Thanks so much!

    and for the int, char, and float thing I am just using float and I have tested it and it seems to be working fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy arithmetic question
    By brooksbp in forum C Programming
    Replies: 7
    Last Post: 09-13-2008, 12:39 AM
  2. Easy Math Question
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-23-2003, 10:00 AM
  3. Easy Question
    By Bryan in forum C++ Programming
    Replies: 15
    Last Post: 10-17-2002, 04:25 PM
  4. Easy Question
    By inandout in forum C++ Programming
    Replies: 3
    Last Post: 10-17-2002, 05:49 AM
  5. Easy Question needs answering
    By Prog.Patterson in forum Windows Programming
    Replies: 0
    Last Post: 05-13-2002, 09:35 AM