To write a c program to maintain a log of football match b/w 2 teams and then compare the results of the log entered by user and announce the winner.
-
here is the code i have wrriten but when reaching the for()loop it just skips though the first loop of for( i.e. i=0 )and starts with i=1. What is wrong in the code.
#include
#includeint main()
{
int i, log, k, c1=0, c2=0, j=0;
char team_1[100], team_2[100], input[100], ch;printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); if(strcmp(team\_1, team\_2)==0) { printf("Oops! both names are same. Please enter again: \\n"); while(strcmp(team\_1, team\_2)==0) { printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); } } printf("Enter the number of logs: \\n\\n"); scanf("%d", &log); for(i=0; ic2) printf("Team-1 wins"); else printf("Team-2 wins"); return 0;
}
-
here is the code i have wrriten but when reaching the for()loop it just skips though the first loop of for( i.e. i=0 )and starts with i=1. What is wrong in the code.
#include
#includeint main()
{
int i, log, k, c1=0, c2=0, j=0;
char team_1[100], team_2[100], input[100], ch;printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); if(strcmp(team\_1, team\_2)==0) { printf("Oops! both names are same. Please enter again: \\n"); while(strcmp(team\_1, team\_2)==0) { printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); } } printf("Enter the number of logs: \\n\\n"); scanf("%d", &log); for(i=0; ic2) printf("Team-1 wins"); else printf("Team-2 wins"); return 0;
}
Tarun Jha wrote:
when reaching the for()loop it just skips though the first loop of for( i.e. i=0 )and starts with i=1
How did you find out it? Did you debug your code? Then what are the values of i and log right before stepping in the loop?
-
Tarun Jha wrote:
when reaching the for()loop it just skips though the first loop of for( i.e. i=0 )and starts with i=1
How did you find out it? Did you debug your code? Then what are the values of i and log right before stepping in the loop?
This is the output i am getting. As you can see for i=0, it just skips through.
Quote:
Enter name of Team-1: rob Enter the name of Team-2 max Enter the number of logs: 5 Enter log-1 Invalid entry ! Enter log-2 max Enter log-3 max Enter log-4 rob Enter log-5 max Team-2 wins Process returned 0 (0x0) execution time : 23.573 s Press any key to continue.
-
here is the code i have wrriten but when reaching the for()loop it just skips though the first loop of for( i.e. i=0 )and starts with i=1. What is wrong in the code.
#include
#includeint main()
{
int i, log, k, c1=0, c2=0, j=0;
char team_1[100], team_2[100], input[100], ch;printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); if(strcmp(team\_1, team\_2)==0) { printf("Oops! both names are same. Please enter again: \\n"); while(strcmp(team\_1, team\_2)==0) { printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); } } printf("Enter the number of logs: \\n\\n"); scanf("%d", &log); for(i=0; ic2) printf("Team-1 wins"); else printf("Team-2 wins"); return 0;
}
Why are you asking for both team names if they are equal? You already have
team_1
name so you only need a different name forteam_2
. You do not need to clear theinput
array as it will get overwritten on the nextgets
call. And it skips the first loop becausescanf
leaves a newline character in the input buffer which then is consumed by the first call togets
. If you did as I have suggested elsewhere you could figure all these things out for yourself. -
Tarun Jha wrote:
when reaching the for()loop it just skips though the first loop of for( i.e. i=0 )and starts with i=1
How did you find out it? Did you debug your code? Then what are the values of i and log right before stepping in the loop?
This is the output
Enter name of Team-1:
rob
Enter the name of Team-2
max
Enter the number of logs:
5Enter log-1
Invalid entry !Enter log-2
maxEnter log-3
maxEnter log-4
robEnter log-5
maxTeam-2 wins
Process returned 0 (0x0) execution time : 23.573 s
Press any key to continue. -
Why are you asking for both team names if they are equal? You already have
team_1
name so you only need a different name forteam_2
. You do not need to clear theinput
array as it will get overwritten on the nextgets
call. And it skips the first loop becausescanf
leaves a newline character in the input buffer which then is consumed by the first call togets
. If you did as I have suggested elsewhere you could figure all these things out for yourself. -
This is the output
Enter name of Team-1:
rob
Enter the name of Team-2
max
Enter the number of logs:
5Enter log-1
Invalid entry !Enter log-2
maxEnter log-3
maxEnter log-4
robEnter log-5
maxTeam-2 wins
Process returned 0 (0x0) execution time : 23.573 s
Press any key to continue.Tarun Jha wrote:
Enter the number of logs: 5 Enter log-1
And there is your code:
for(i=0; i
So why do you think the loop skips when i is zero??? It works and prints the result
"Enter log-1" -
Tarun Jha wrote:
Enter the number of logs: 5 Enter log-1
And there is your code:
for(i=0; i
So why do you think the loop skips when i is zero??? It works and prints the result
"Enter log-1" -
are you suggesting that i initialize input to zero before applying gets() as you told me to do in the previous question answered by you.
-
No, I am telling you to go and get a proper study guide and learn the C language in complete detail.
-
No, I am telling you to go and get a proper study guide and learn the C language in complete detail.
-
i will but befoer that can you show me how to correct the buffering problem of scanf() in order to remove newline charecter from gets().
-
And here is the working code :)
#include
#includeint main()
{
int i, log, j, c1=0, c2=0;
char team_1[100], team_2[100], input[100], ch;printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); if(strcmp(team\_1, team\_2)==0) { printf("Oops! both names are same. Please enter again: \\n"); while(strcmp(team\_1, team\_2)==0) { printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); } } printf("Enter the number of logs: \\n"); scanf("%d%\*c", &log); for(i=0; ic2) printf("\\nTeam-1 wins"); else if(c1
-
here is the code i have wrriten but when reaching the for()loop it just skips though the first loop of for( i.e. i=0 )and starts with i=1. What is wrong in the code.
#include
#includeint main()
{
int i, log, k, c1=0, c2=0, j=0;
char team_1[100], team_2[100], input[100], ch;printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); if(strcmp(team\_1, team\_2)==0) { printf("Oops! both names are same. Please enter again: \\n"); while(strcmp(team\_1, team\_2)==0) { printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); } } printf("Enter the number of logs: \\n\\n"); scanf("%d", &log); for(i=0; ic2) printf("Team-1 wins"); else printf("Team-2 wins"); return 0;
}
And here is the working code..
#include
#includeint main()
{
int i, log, j, c1=0, c2=0;
char team_1[100], team_2[100], input[100], ch;printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); if(strcmp(team\_1, team\_2)==0) { printf("Oops! both names are same. Please enter again: \\n"); while(strcmp(team\_1, team\_2)==0) { printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); } } printf("Enter the number of logs: \\n"); scanf("%d%\*c", &log); for(i=0; ic2) printf("\\nTeam-1 wins"); else if(c1
-
here is the code i have wrriten but when reaching the for()loop it just skips though the first loop of for( i.e. i=0 )and starts with i=1. What is wrong in the code.
#include
#includeint main()
{
int i, log, k, c1=0, c2=0, j=0;
char team_1[100], team_2[100], input[100], ch;printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); if(strcmp(team\_1, team\_2)==0) { printf("Oops! both names are same. Please enter again: \\n"); while(strcmp(team\_1, team\_2)==0) { printf("Enter name of Team-1: \\n"); gets(team\_1); printf("Enter the name of Team-2\\n"); gets(team\_2); } } printf("Enter the number of logs: \\n\\n"); scanf("%d", &log); for(i=0; ic2) printf("Team-1 wins"); else printf("Team-2 wins"); return 0;
}
You have identified "problem area " , so work on problem area first. No matter how many books you read , they will NEVER tell you that MAJORITY of errors are typos, and than you will have hidden SYNTAX errors. The last errors are programming - LOGICAL errors. Before you compare real INPUTS, you have to have correct program sequence working so emulating the actual inputs would be next step. You are starting Backward with analysis of logical / real input errors. PS You have a "numerical" application , so prefacing printed numbers with "minus" sign" seems little odd. for(i=0; i
-
You have identified "problem area " , so work on problem area first. No matter how many books you read , they will NEVER tell you that MAJORITY of errors are typos, and than you will have hidden SYNTAX errors. The last errors are programming - LOGICAL errors. Before you compare real INPUTS, you have to have correct program sequence working so emulating the actual inputs would be next step. You are starting Backward with analysis of logical / real input errors. PS You have a "numerical" application , so prefacing printed numbers with "minus" sign" seems little odd. for(i=0; i
Dear Vaclav_, could you edit your post to properly format the code snippet? Otherwise it is very hard to read/understand.
-
You have identified "problem area " , so work on problem area first. No matter how many books you read , they will NEVER tell you that MAJORITY of errors are typos, and than you will have hidden SYNTAX errors. The last errors are programming - LOGICAL errors. Before you compare real INPUTS, you have to have correct program sequence working so emulating the actual inputs would be next step. You are starting Backward with analysis of logical / real input errors. PS You have a "numerical" application , so prefacing printed numbers with "minus" sign" seems little odd. for(i=0; i
it works too..
#include
#include
#includeint main()
{
int log, i, j, k, count1=1, count2=0, c;printf("Enter the number of logs: \\n"); scanf("%d%\*c", &log); char names\[log\]\[100\]; //Entering names. printf("Enter names: \\n"); gets(names\[0\]); //completing in a single loop. for(i=1; icount2) { puts(names\[0\]); printf("Wins !"); } else if(count2>count1) { puts(names\[k\]); printf("Wins !"); } else if(count1==count2) { printf("It's a tie!!"); } break; } count1++; } return 0;
}