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 .
#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 .
Replies
-
[Prototype]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. -
simplycoderNeohackerHere 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 :
Integer %28Computer Science%29 Common Short Integer Sizes
For further explanations, you can refer a tutorial written for beginners here :
Beginners Tutorial : The C-language. -
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"simplycoderfor the input 6415603113 is out of bounds for a 32-bit signed integer. -
simplycodersimplycoder
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 :
Integer %28Computer Science%29 Common Short Integer Sizes
For further explanations, you can refer a tutorial written for beginners here :
Beginners Tutorial : The C-language.
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.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"
Once you do that,try long long. -
NeohackerYeah 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... -
simplycoderfrom 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.
You are reading an archived discussion.
Related Posts
Quote
Qmed readers so far appear to be Trekkies—with the Star Trek movies and series garnering the most votes in our survey of the top movies for medtech professionals.
Medical...
Quote:
In an age where technology continues to shrink, we constantly search for new ways to better fit devices into our purses and pockets, around our wrists and strapped to...
what are the seven basic tools of quality
control??? want to know everything about it.......
I want to take input for name of a student ...
when i do char name[20]; and take input with cin>>name; it prints only upto 1st space like if i...
Name: Neo Hacker
*Engineering Trade: Information Technology
Location: India
*Occupation: Student
*Hobbies & Interests: Only and only video games 😛
*Aim in life: Earn enough money to buy all video...