Question:
Please help me with C Code?
Rithwik
2012-06-08 03:34:31 UTC
Here is the C- Code

#include
#include
void main()
{
int value[100],val[1000];
char name[5][20],ch='y';
clrscr();
printf("\nEnter the 4 player names: ");
for(int i=1;i<=4;i++)
{
printf("\nPlayer[%d]: ",i);
scanf("%s",&name[i]);
}

while(ch=='y' || ch=='Y')
{
clrscr();
printf("\nEnter Scores: ");
for(int j=1;j<=4;j++)
{
printf("%s: ",name[j]);
scanf("%d",&value[j]);
}
for(int x=1;x<=4;x++)
{
val[x]+=value[x];
}
printf("\nDo you want to continue?(y/n): ");
scanf("%s",&ch);
}
clrscr();
printf("\n**********Score-Board************");
for(int y=1;y<=4;y++)
{
printf("\n%s: %d",name[y],val[y]);
}
getch();

}

The problem is that , while i am running this code, it works fine for the 1st time . But when i compile it for the second time, the SCORE BOARD displays the value after adding up the Previously compiled values . Please help me how to Remove the Previously stored values ( that adds up with the new values in the another Compilation of the same code ) I think the values are stored in Stack . but i am not sure abt it.
Five answers:
Matthew
2012-06-08 03:50:04 UTC
At the Top of the code before the int value bit add a line to clear the scoreboard like scoreboard="" I don't know what the c equivalent is but that us used in vb
2012-06-08 04:11:07 UTC
You need to get a better C reference.



"void main()"



"scanf("%s", ..."



Just out of curiosity, where did you pick up the bad habits I've quoted above?



edit: Look at the program linked to below. It has a better design than yours does - it reads a specific format from a text file. There are programs called "text editors" that people use to compose and modify text files. Your program shouldn't aim to do their job. People should use text editors to compose text files that are read and processed by your program.



Also, it dynamically allocates storage for each player. No maximum limits is imposed, allowing it to work with larger data sets. The same limitation is imposed on the length of the name, but instead of overflowing a buffer it will throw an error message which informs the user of the line number with the long name.



Finally, it's valid C code without dependence on libraries that aren't cross platform (like conio), meaning that it will run successfully on many architectures.



http://ideone.com/KtLF3



Anyway, it should be said that reading formatted input can be difficult to do correctly, and that it's a topic which should be introduced after you have a strong grasp of the language. The fact that many references start out by teaching you *wrong* ways to read input indicates that there are many terrible teachers out there. I suggest reading "The C Programming Language (Second Edition)" if you wish to learn C correctly.
?
2016-12-10 19:51:13 UTC
it is real that in case you create an empty C++ console venture and manually rename your cpp document to furnish it .c extension, seen Studio *will* execute the Microsoft C compiler, in spite of the undeniable fact that it fairly is a doubly-out of date 1989 C compiler. Microsoft deserted C a protracted time in the past. in case you choose to stay with this IDE and assemble C, exchange the underlying compiler. Intel C/C++ is a stable decision.
roger
2012-06-08 07:49:47 UTC
one problem is here:

printf("\nDo you want to continue?(y/n): ");

scanf("%s",&ch);



ch is a single character

you are using %s as a specifier in scanf oops.

you need to use %c to scan in a single character.
2012-06-08 03:48:06 UTC
Programming is not my area, but can you not use a Debugger?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...