Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

CoiN

MORE C help needed! :)

This topic is 6685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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....

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Try:

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

instead.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
    
#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=-..

Share this post


Link to post
Share on other sites
quote:
Ive done this type of thing lots of times but yet this will not seem to work.[/QUOTE]

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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
And no it aint my homework

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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!