Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. 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.

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.

Scheduled Pinned Locked Moved C / C++ / MFC
sharepointcollaborationregexquestion
17 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Tarun Jha

    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
    #include

    int 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;
    

    }

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #4

    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 for team_2. You do not need to clear the input array as it will get overwritten on the next gets call. And it skips the first loop because scanf leaves a newline character in the input buffer which then is consumed by the first call to gets. If you did as I have suggested elsewhere you could figure all these things out for yourself.

    T 1 Reply Last reply
    0
    • V Victor Nijegorodov

      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?

      T Offline
      T Offline
      Tarun Jha
      wrote on last edited by
      #5

      This is the output

      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.

      V 1 Reply Last reply
      0
      • L Lost User

        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 for team_2. You do not need to clear the input array as it will get overwritten on the next gets call. And it skips the first loop because scanf leaves a newline character in the input buffer which then is consumed by the first call to gets. If you did as I have suggested elsewhere you could figure all these things out for yourself.

        T Offline
        T Offline
        Tarun Jha
        wrote on last edited by
        #6

        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.

        L 1 Reply Last reply
        0
        • T Tarun Jha

          This is the output

          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.

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #7

          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"

          T 1 Reply Last reply
          0
          • V Victor Nijegorodov

            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"

            T Offline
            T Offline
            Tarun Jha
            wrote on last edited by
            #8

            no, it doen't take input for first loop, it just skips to the second one, does'nt matter from where i is initialized. Please run the code you will understand.

            1 Reply Last reply
            0
            • T Tarun Jha

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #9

              No, I am telling you to go and get a proper study guide and learn the C language in complete detail.

              T 2 Replies Last reply
              0
              • L Lost User

                No, I am telling you to go and get a proper study guide and learn the C language in complete detail.

                T Offline
                T Offline
                Tarun Jha
                wrote on last edited by
                #10

                ok

                1 Reply Last reply
                0
                • L Lost User

                  No, I am telling you to go and get a proper study guide and learn the C language in complete detail.

                  T Offline
                  T Offline
                  Tarun Jha
                  wrote on last edited by
                  #11

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

                  L 1 Reply Last reply
                  0
                  • T Tarun Jha

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

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #12

                    fflush(stdin) - Google Search[^]

                    T 1 Reply Last reply
                    0
                    • L Lost User

                      fflush(stdin) - Google Search[^]

                      T Offline
                      T Offline
                      Tarun Jha
                      wrote on last edited by
                      #13

                      And here is the working code :)

                      #include
                      #include

                      int 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
                      
                      1 Reply Last reply
                      0
                      • T Tarun Jha

                        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
                        #include

                        int 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;
                        

                        }

                        T Offline
                        T Offline
                        Tarun Jha
                        wrote on last edited by
                        #14

                        And here is the working code..

                        #include
                        #include

                        int 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
                        
                        1 Reply Last reply
                        0
                        • T Tarun Jha

                          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
                          #include

                          int 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;
                          

                          }

                          V Offline
                          V Offline
                          Vaclav_
                          wrote on last edited by
                          #15

                          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

                          V T 2 Replies Last reply
                          0
                          • V Vaclav_

                            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

                            V Offline
                            V Offline
                            Victor Nijegorodov
                            wrote on last edited by
                            #16

                            Dear Vaclav_, could you edit your post to properly format the code snippet? Otherwise it is very hard to read/understand.

                            1 Reply Last reply
                            0
                            • V Vaclav_

                              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

                              T Offline
                              T Offline
                              Tarun Jha
                              wrote on last edited by
                              #17

                              it works too..

                              #include
                              #include
                              #include

                              int 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;
                              

                              }

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups