View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12921 Members
Join this group to post and comment.
Neohacker
Neohacker • Sep 27, 2014

Structure Problem

Here is my code :

#include
using namespace std;

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

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

cout<<"Name : "<
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 .
[Prototype]
[Prototype] • Sep 27, 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.
simplycoder
simplycoder • Sep 28, 2014
Neohacker
Here is my code :

#include
using namespace std;

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

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

cout<<"Name : "<
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 :
https://en.wikipedia.org/wiki/Integer_(computer_science)#Common_short_integer_sizes


For further explanations, you can refer a tutorial written for beginners here :
https://www.crazyengineers.com/threads/beginners-tutorial-the-c-language.46520/
Neohacker
Neohacker • Sep 29, 2014
@simplycoder
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"
simplycoder
simplycoder • Sep 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 :
https://en.wikipedia.org/wiki/Integer_(computer_science)#Common_short_integer_sizes


For further explanations, you can refer a tutorial written for beginners here :
https://www.crazyengineers.com/threads/beginners-tutorial-the-c-language.46520/
Neohacker
@simplycoder
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.
Neohacker
Neohacker • Sep 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 @simplycoder for information...
simplycoder
simplycoder • Sep 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.

Share this content on your social channels -