Stack Overflow Asked by A4n on December 23, 2021
The idea of my program was to print individual digits of n.
But instead it prints all the digits at once in the first line and a bunch of zeroes or garbage values in subsequent lines.
I want to access each individual number as we can do with arrays.
Suppose the input is 1234, why doesn’t it print 1n2n3n4?
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
int main()
{
int* n=(int*)malloc(1000*sizeof(int));
cin>>*n;
int len;
len = log10(*n) + 1;
for(int i=0;i<len;i++)
{
cout<<n[i]<<endl;
}
}
For some reason you think that
int* n=(int*)malloc(1000*sizeof(int));
cin>>*n;
will read a number and put each digit in a different element of the dynamic array n
. If that happened then the rest of your code would work (kind of). But of course it doesn't. Instead the number read is put into *n
(or, same thing, n[0]
) and the rest of the dynamic array elements are uninitialised, which explains the garbage values you see.
I'm struggling to understand why you thought your code might behave in the way you wanted. I guess you are just an optimistic person and think that if you wish hard enough the compiler will understand. This seems to be quite a common attitude among beginners. Unfortunately programming isn't like that.
Answered by john on December 23, 2021
n
is declared as a pointer to a memory location which can store 1000
integer entities. When you use cin>>*n;
, an integer value is read as input and store at the first memory block among the 1000
blocks. The individual digits of the integer are not stored in separate blocks, hence you can't print them separately.
For example, if the input is 123
,
n[0]
stores 123
, n[1],n[2],...n[999]
stores junk values.
To store a value in n[1]
, you will have to use cin
again.
Answered by Anuneet Anand on December 23, 2021
When you cin >> *n
, you don't read the number digit by digit, but read in as a whole.
So when you cin >> *n
and type in 1234
, *n
becomes 1234
.
If you want to print all the individual digits, like 1n2n3n4
, you need to separate the digits for yourself:
int pos = 1;
while (*n != 0)
{
n[pos] = n % 10;
n /= 10;
++pos;
}
for (--pos; pos > 0; --pos)
{
cout << n[pos] << endl;
}
However, the easiest approach is to read in the number as a string, not a number, then print out the characters, that is the digits, one by one.
char str[1000];
cin >> str;
for (char *s = str; *s; ++s)
{
cout << *s << endl;
}
You can also convert the number into a string, and do the same:
#include <cstring>
using namespace std;
...
char str[1000];
sprintf(str, "%d", *n);
for (char *s = str; *s; ++s)
{
cout << *s << endl;
}
------- Original Answer:
If you want to print the first element of n
:
cout << *n;
or
cout << n[0];
Your code
for(int i=0;i<len;i++)
{
cout<<n[i]<<endl;
}
means
cout << n[0] << endl;
cout << n[1] << endl;
cout << n[2] << endl;
...
cout << n[len-1] << endl;
Answered by user26742873 on December 23, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP