This code compiles fine, but I can't get it to run. Can someone tell me what I'm missing?
When it complies and runs, it should look like this here: "Enter any integer in the range -2,147,483,648 to +2,147,483,647, without comma separators to see the formatted number, or EOF."
So, when I enter any number, it should separate the numbers with commas, and keeps the numbers in the given range.
Here is what I got so far:
/* Write a program to accept and display any valid int formatted with commas and leading sign.*/
#include <stdio.h>
#define SIZE 100000000
int nextDbl(double *);
int main()
{
int j, k;
double array[SIZE] = {0.0};
for (j = 0; j < SIZE && nextDbl(&array[j]) != EOF; j++)
;
for (k = 0; k < j; k++)
printf("%f ", array[k]);
printf("\n\n");
system("PAUSE");
return 0;
}
/* nextDbl: get next double from keyboard into *pd */
int nextDbl(double *pd)
{
printf("Enter any integer in the range -2,147,483,648 to +2,147,483,647,");
printf ("without comma separators to see the formatted number, or EOF.> ");
return scanf("%lf", pd); /* scanf returns # successful assignments
or EOF if encountered
*/
}
Either a.) it is compiling/running fine, it's just sitting waiting for 800MB of RAM/memory to be allocated for it, or you are getting an error (I believe on VC++ it gives you an error if you have an array over a certain size)
It was obvious to me that your program at best would only output the numbers --unformatted--that were entered by the user, and I assumed that the was the problem you were having, in which case I further assumed that psault's code did the trick, though it seemed more complicated than necessary.
O.k guys, jsmith, firedraco, Mythios....when I compile and the run the program. The executable should say:
"Enter any integer in the range -2,147,483,648 to +2,147,483,647,
without comma separators to see the formatted number, or EOF."
So, I inter, for example, 123456789, and I press "Enter" "123,456,789" should display separated by commas. So, if you can guys can help me with that, I'll greatly appreciate it. Let me see how you guys would write this program using Devc++, c programming only.
The problem actually is the huge array. When you create an array like that, it is declaring the array on the stack. Unfortunately, the stack size is not that large - even if you have enough RAM.
There are a couple possible solutions:
1) Assuming you are using C++, use a vector instead, not an array. Vectors are dynamically sized, meaning they start with 0 size, and increase in size as you put more things into them. This is the best solution.
2) The next best solution is to dynamically allocate your array using malloc(). This will allocate the array on the heap, not the stack, so you can make it much bigger than you could on the stack. I would still chop a '0' off though!
3) The worst solution. Make your array size smaller. I believe in Dev-C++ when I had the exact same problem, I had to make the size either 75,000 or 750,000 or something. Is your user really going to enter more than 75,000 numbers anyway?
Thanks...Nah, the user won't enter that many numers. Let me play with it to see what happens. How would you write it mahlerfive with the dev++ compiler?