Advertisement

MORE C help needed! :)

Started by June 27, 2000 11:13 AM
15 comments, last by CoiN 24 years, 5 months ago
This is the piece of code thats givin'' me problems:void main(void) { int count; double average[10]; char names[10][50]; for (count=1;count<10;count++) { printf ("\nEnter name %d... ", count); gets (names[count]); printf ("\nEnter Average %d... ", count); scanf ("%lf", &average[count]); } for (count=1;count<10;count++) { printf ("\n%d. %s %lf", count, name[count], average[count]); } } Its a simple piece of code which reads ten strings, and ten doubles. Ive done this type of thing lots of times but yet this will not seem to work. The Progblem: After the program reads the first string and double in, the next time it loops round it just skips past the gets() function and prints Enter Average and waits on on scanf for an input. This seems pretty strange to me but I must be doin somin wrong Thanks in Advance....
Try:

scanf ("%s",names[count]);

instead.
Advertisement
Looks like your homework from Intro to Computer Science 101

Try stepping through it with your debugger and watch your call stack very carefully. Note that any time you write a function that uses va_args or call a function that uses variable arguments you can get bugs from the call stack.
    #include <stdio.h>#include <conio.h>void main(void){	int count;	double average[10];	char names[10][50];	for (count=0;count<10;count++)	{		printf ("\nEnter name %d... ", count+1);		scanf("%s", names[count]);		printf ("\nEnter Average %d... ", count+1);		scanf ("%lf", &average[count]);	}	for (count=0;count<10;count++)	{		printf ("\n%d. %s %lf", count, names[count], average[count]);	}	getch();}[/SOURCE]I just replaced the gets() with a scanf()...The other way is as follows (it uses gets() and flushes the stdin buffer before reading again...)[SOURCE]#include <stdio.h>#include <conio.h>void main(void){	int count;	double average[10];	char names[10][50];	for (count=0;count<10;count++)	{		fflush(stdin);		printf ("\nEnter name %d... ", count+1);		gets (names[count]);		printf ("\nEnter Average %d... ", count+1);		scanf ("%lf", &average[count]);	}	for (count=0;count<10;count++)	{		printf ("\n%d. %s %lf", count, names[count], average[count]);	}	getch();}    


Hope this helps..

..-=ViKtOr=-..
btw, your loop should start from 0 to 9 and not from 1 to 9... I have chagned that in the above code...
quote: Ive done this type of thing lots of times but yet this will not seem to work.

If you had really done that, you''d know what the cause to the problem is. So it probably IS your homework... Anyways.. this one was a freebie...
Advertisement
Try this:

void main(void)
{
int count;
double average[10];
char names[10][50];
char buffer[20];

for (count=1;count<10;count++)
{
printf ("\nEnter name %d... ", count);
gets (names[count]);

printf ("\nEnter Average %d... ", count);
gets (buffer);
sscanf (buffer, "%lf", &average[count]);


}

for (count=1;count<10;count++)
{
printf ("\n%d. %s %lf", count, names[count], average[count]);
}
}


The scanf() function does not strip out the return key from the keyboard buffer so the next time you ask for a name, the computer thinks that the user just hit the return key. Instead, read the keyboard into another buffer and use sscanf().

Cheers

Matt





Check out my project at:www.btinternet.com/~Matthew.Bennett
WOW!

Just how many people can jump on a thread while I compile ten lines of code......

You sure got your money''s worth out of this thread CoiN.



Check out my project at:www.btinternet.com/~Matthew.Bennett
Thanks for the help all....
And no it aint my homework

And I have done this type of stuff before but it was in pascal so.... :p

This topic is closed to new replies.

Advertisement