As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/

Help with C program

TwistedJesterTwistedJester Registered User regular
edited December 2007 in Help / Advice Forum
Could someone please tell me why the final print statement in this program isn't printing anything aside from a newline? I'm supposed to use this pi function, which adds the digits of pi in increments of 4 to the buffer, to get a specific # of pi. The buffer prints fine, but when I try to access a specific element in it, I don't get anything.
main()
{
	char *buffer[100];
	char digit;
	int number;
	printf("Enter a digit of pi find\n");
	scanf("%d", &number);
	if (!(number%4==0))
	{
		int ceiling = (number - number%4) + 4;
		printf("ceiling = %d\n", ceiling);
		pi(buffer, ceiling);
		printf("%s\n", buffer);
		digit = buffer[number];
	}
	else
	{
		pi(buffer, number);
		printf("%s\n", buffer);
		digit = buffer[number];
	}
	printf("%c\n", digit);
	return 0;
}

TwistedJester on

Posts

  • DJ-99DJ-99 Registered User regular
    edited December 2007
    It's been like 5 years since I did any C, but it looks like nobody's really helping you out yet, so I'll give it a try...this could very easily be wrong.

    I feel like the problem might lie where you declare the value for your digit variable.

    Usually, when you put a value in for char, you would have to say something like char digit = "4"; right? So, I'm not sure if it works by saying digit = buffer[number]; That's equivalent to saying digit = 4; I think.

    I'm not sure if you can declare it as an integer, but you could give it a try. Alternatively, maybe declare it as digit[1] and try digit[1] = buffer[number];

    I repeat, I haven't done any coding in seriously 5 years, and this answer will probably get laughed at, but that's ok. Good luck. I'm sure somebody who actually knows C will be along shortly to help out.

    Maybe try casting it as an int, in your printf function.

    Also, it's weird that you declared your array as a pointer. It's already a pointer. You made it a double pointer. Try getting rid of the *.

    DJ-99 on
  • ProtoProto Registered User regular
    edited December 2007
    DJ-99 wrote: »
    Also, it's weird that you declared your array as a pointer. It's already a pointer. You made it a double pointer. Try getting rid of the *.

    yeah, I think this is the problem.


    were you supplied the pi function?

    Proto on
    and her knees up on the glove compartment
    took out her barrettes and her hair spilled out like rootbeer
  • TwistedJesterTwistedJester Registered User regular
    edited December 2007
    Wow..... Yeah, removing the * solved everything. I don't know what I was thinking. Thanks a lot guys :^:

    TwistedJester on
  • ProtoProto Registered User regular
    edited December 2007
    pointers.... even 10 years later I still remember my hate for them.

    Proto on
    and her knees up on the glove compartment
    took out her barrettes and her hair spilled out like rootbeer
  • syrionsyrion Registered User regular
    edited December 2007
    Yeah, but they're used in every powerful programming language. C just has the disadvantage of having one of the earlier implementations.

    syrion on
Sign In or Register to comment.