• Advertisement

Archived

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

MORE C help needed! :)

This topic is 6451 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
btw, your loop should start from 0 to 9 and not from 1 to 9... I have chagned that in the above code...

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
May aswell get another question in here

Should this piece of code work?

    
void main(void)

{
char *p;

scanf ("%s", p);
printf ("%s", p);
}


Im sure i remember using character pointers to hold strings this way, but my mind has gone blank today


Share this post


Link to post
Share on other sites
why not just use cin and cout
it makes the code look so much better and easier to read
you dont have to make all those function calls.......i know using cin and cout really do call functions but this is hidden from you and is easy to implement
    
#include <iostream.h>

void main()
{
double average[10];
char names[10][50];
for (int count=0; count < 10; count++)
{
cout << "Enter name " << count << endl;
cin >> names[count];
cout << "Enter average " << count << endl;
cin >> average[count];
}
for (int i=0; i<10; i++)
{
cout << names<i> << " " << average[i] << endl;
}




"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Share this post


Link to post
Share on other sites
quote:

Should this piece of code work?

void main(void)
{
char *p;
scanf ("%s", p);
printf ("%s", p);
}



No. p is a pointer to a character or character array, but it isn''t initialized, so it points to nothing. You''ll get an access error. You need to add:

p = new char[50];

in order to get it to work. Also, after you''ve used p (i.e. at the end of the program), you should delete it:

delete [] p;


Share this post


Link to post
Share on other sites
CoiN, to answer your question, yes, you CAN do that, but that doesn''t mean it''s correct. You write to random memory, and most of the time that will cause a GPF if you''re working under Windows, if you''re in DOS, you might not get any errors, but the program is going to crash someone''s computer (including yours) if it unexpectedly write to a memory location it''s not supposed to... In order to fix the problem you have to allocate enough memory to hold your array of chars (string). Other than that, the code is going to work, especially in DOS...

Hope this helps!

..-=ViKtOr=-..

Share this post


Link to post
Share on other sites
Thanks, Ive had a really funny day forgetting lotsa stuff

I''ll just stick to using the standard array of characters

Share this post


Link to post
Share on other sites

  • Advertisement