CrazyEngineers
  • Structure Problem

    Neohacker

    Neohacker

    @neohacker-FP4O5e
    Updated: Oct 27, 2024
    Views: 1.3K
    Here is my code :

    #include<iostream>
    using namespace std;

    struct student {
    char name[20];
    int age;
    long roll;
    };

    int main() {
    student jimmy = {
    "Jimmy Green",
    22,
    06415603113
    };

    cout<<"Name : "<<jimmy.name<<"\nAge : "<<jimmy.age<<"\nRoll No. : "<<jimmy.roll;

    return 0;
    }


    And here is the output :

    Name : Jimmy Green
    Age : 22
    Roll No. : 876021323


    Why i am getting this randon no. in roll no. ??????? input is different from output .
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • [Prototype]

    MemberSep 26, 2014

    Your roll number is much above the range of long. So it rolled over like 3 times before setting up to that value.

    Its better you make roll number as char array as integer data types will strip off any leading zeros as well.
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberSep 28, 2014

    Neohacker
    Here is my code :

    #include<iostream>
    using namespace std;

    struct student {
    char name[20];
    int age;
    long roll;
    };

    int main() {
    student jimmy = {
    "Jimmy Green",
    22,
    06415603113
    };

    cout<<"Name : "<<jimmy.name<<"\nAge : "<<jimmy.age<<"\nRoll No. : "<<jimmy.roll;

    return 0;
    }


    And here is the output :

    Name : Jimmy Green
    Age : 22
    Roll No. : 876021323


    Why i am getting this randon no. in roll no. ??????? input is different from output .

    I disagree with this explanation provided by @[Prototype] .

    In languages such as C/C++, Java the octal numbers begin with 0.(hex numbers begin with 0x).
    By octal number, I mean the numbers are expressed in base-8 format.
    Similarly, hex numbers(hexadecimal numbers) are expressed in base-16 format.

    In this case, the roll-number is given as 06415603113
    which the compiler takes in form of octal number.

    When you print the values, it is printed in form of decimal number.

    Thus when 06415603113 is converted in form of decimal number you get the

    876021323

    Now comes the second part.
    I would take a wild guess that you wanted to enter a decimal number(base-10) as roll number, however, one must always be careful regarding the maximum and the minimum and signed and unsigned value limits supported by each data type, and for the input 6415603113 is out of bounds for a 32-bit signed integer.

    Here are few values which can help you out :
    <a href="https://en.wikipedia.org/wiki/Integer_%28computer_science%29#Common_short_integer_sizes" target="_blank" rel="nofollow noopener noreferrer">Integer %28Computer Science%29 Common Short Integer Sizes</a>


    For further explanations, you can refer a tutorial written for beginners here :
    <a href="https://www.crazyengineers.com/threads/beginners-tutorial-the-c-language.46520">Beginners Tutorial : The C-language.</a>
    Are you sure? This action cannot be undone.
    Cancel
  • Neohacker

    MemberSep 29, 2014

    #-Link-Snipped-#
    1. When i try to assign "long a = 6415603113" instead of 06415603113 and print "a" , still i am getting some another value .

    2. You said that
    simplycoder
    for the input 6415603113 is out of bounds for a 32-bit signed integer.
    but i am using "long" here not "int"
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberSep 29, 2014

    simplycoder


    I disagree with this explanation provided by @[Prototype] .

    In languages such as C/C++, Java the octal numbers begin with 0.(hex numbers begin with 0x).
    By octal number, I mean the numbers are expressed in base-8 format.
    Similarly, hex numbers(hexadecimal numbers) are expressed in base-16 format.

    In this case, the roll-number is given as 06415603113
    which the compiler takes in form of octal number.

    When you print the values, it is printed in form of decimal number.

    Thus when 06415603113 is converted in form of decimal number you get the

    876021323

    Now comes the second part.
    I would take a wild guess that you wanted to enter a decimal number(base-10) as roll number, however, one must always be careful regarding the maximum and the minimum and signed and unsigned value limits supported by each data type, and for the input 6415603113 is out of bounds for a 32-bit signed integer.

    Here are few values which can help you out :
    <a href="https://en.wikipedia.org/wiki/Integer_%28computer_science%29#Common_short_integer_sizes" target="_blank" rel="nofollow noopener noreferrer">Integer %28Computer Science%29 Common Short Integer Sizes</a>


    For further explanations, you can refer a tutorial written for beginners here :
    <a href="https://www.crazyengineers.com/threads/beginners-tutorial-the-c-language.46520">Beginners Tutorial : The C-language.</a>
    Neohacker
    #-Link-Snipped-#
    1. When i try to assign "long a = 6415603113" instead of 06415603113 and print "a" , still i am getting some another value .

    2. You said that


    but i am using "long" here not "int"
    Why don't you find out what is the maximum allowed number in signed long, on your system.This would give you a fair picture.
    Once you do that,try long long.
    Are you sure? This action cannot be undone.
    Cancel
  • Neohacker

    MemberSep 29, 2014

    Yeah with long long, it worked fine.... But still it is converting the number starting with 0.
    So how can i store a number that is starting with a 0?
    Also thanks #-Link-Snipped-# for information...
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberSep 29, 2014

    from top of my head, I can say that re-convert to decimal
    on other hand, if you are not going to perform any numerical operations on roll number then there is no need to store it as an numeric data type.
    store it as a string.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register