Help messed up code

This is a discussion on Help messed up code within the C Programming forums, part of the General Programming Boards category; Hi i doing a table which updates scores and stuff .Everything worked fine when i was using numbers to postion ...

  1. #1
    Beginner
    Join Date
    Apr 2005
    Location
    uk
    Posts
    8

    Help messed up code

    Hi i doing a table which updates scores and stuff .Everything worked fine when i was using numbers to postion the teams but now that i am using strings and stuck a for and if loop. it does not update any more help.

    Code:
    //for label 1
    for(blah blah)//loops 12 times tillit finds correct label
    {
    if ( strcmp((teams[one].label,teamlabel1)==0)
    
    {
            scanf("%d",&tempi1);
    //do stuff which gives tempi1 which has been declared
    }
    else{
    }
    }
    //label2
    
    //enter label 2 name using scanf
    
    for(blah blah)//loops 12 times tillit finds correct label
    {
    if ( strcmp((teams[one].label,teamlabel2)==0)
    
    {
            scanf("%d",&tempi2);
    //do stuff which gives tempi2 which has been declared
    }
    else{
    }
    }
    //***********************************
    teams[label2].f = teams[label2].f + tempi;
    teams[label1].f = teams[label1].f + tempi;
    
    if (tempi1>tempi2)
    {
    
    
    //DO stuff
    }
    
    //update table
    }
    problem is after the ********** bit it stops and wont update the table. i played around with it and if i can iclude the code within the loop it works but it does everything 12 times any ideas wats gone wrong if i move it out the for loop it stops updating the table again ? i think the information has to be inside one of the brackets but so far no success

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    38
    I can't make much of your code... it seems to be incomplete and is poorly indented.
    Only problem I can think of is 'tempi': perhaps you meant 'tempi1' and 'tempi2'?

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    i totally agree with togra.. abt the info given is very less for us to make out anything..

  4. #4
    Beginner
    Join Date
    Apr 2005
    Location
    uk
    Posts
    8
    i will try to enter the full code with gaps filled in later tomorrow,i will be handing in the project in the morning slightly modified but i am still curious as to why the original version did not work.
    Thanks for looking

  5. #5
    Beginner
    Join Date
    Apr 2005
    Location
    uk
    Posts
    8

    help again

    ok this is the code the problem is tempgf1 and tempgf2 are not passed to the code at the bottom so calculations can be worked out.

    the same code was used before but at that time instead of %s being added %d was added and the program worked. i have had a friend and a c advanced student look at this and they cant find anything wrong.


    Code:
    /==========match detail entry==================
    
    void detailentry(team* teams)
    
    {
    int i = 0;
    int team1;
    int team2;
    team1=0;
    team2=0;
    
    char teamname[20];
    char teamname1[20];
    
    
    int tempgf1;
    int tempgf2;
    
    //=============enter 1st team results============
    
    
    
    printf("please enter team1 name\n");//asks for input
    
      scanf("%s",&teamname1);//checks whats been input
    
         for(team1=0;team1<12;team1++)
             {
    
                          f(strcmp(teams[team1].name,teamname1)==0)
             {
    
    
             printf("and score\n");
    
    
    
      scanf("%d",&tempgf1);
    
                teams[team1].gf=teams[team1].gf+tempgf1;
                teams[team1].p = teams[team1].p + 1;//adds 1 to value of p
    // NEED TO CALL IN VALUE OF GF2
    
    
             }
    
                    else
             {
    
              }
    
    
               } 
      
    
      //=============enter second teams results==========
    
    
       printf("please enter team2 name\n");
    
    
        scanf("%s",&teamname);
    
           for(team2=0;team2<12;team2++)
                     {
                           if(strcmp(teams[team2].name,teamname)==0)
                     {
    
         printf("and score\n");
    
    
                  }
                     else
                  {
    
                   }
                   }
    
     scanf("%d",&tempgf2 );
    
    
                      teams[team2].gf= teams[team2].gf+tempgf2;
                      teams[team2].p = teams[team2].p + 1;
      
    
    
    
    
    
    
    
    
       printf("%d",tempgf1);
       printf("%d",tempgf2);
    
    /////At this stage tempgf1 and tempgf2  seem to stop working
    
    
           teams[team2].ga = teams[team2].ga+tempgf1;
           teams[team1].ga = teams[team1].ga+tempgf2;
    
    
           if(tempgf1>tempgf2)
                {
    	        teams[team1].w = teams[team1].w+1;
                   	teams[team2].l = teams[team2].l+1;
                }
    	         else if(tempgf1<tempgf2)
    	              {
    	        	   teams[team1].l=teams[team1].l+1;
    		           teams[team2].w=teams[team2].w+1;
    	              }
    		        else
    		                {
    			           teams[team1].d=teams[team1].d+1;
    		            	teams[team2].d=teams[team2].d+1;
    		                  }
    
    
    
    
       teams[team1].t=(teams[team1].w*3)+(teams[team1].d*1);
       teams[team2].t=(teams[team2].w*3)+(teams[team2].d*1);
    
    
    
    
    }
    sorry is that better
    Last edited by Colonial Viper; 04-28-2005 at 11:57 AM. Reason: indenting

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Well working on the indentation would be a help.

    It's way harder to debug code which is poorly indented, and busy people like myself go look at nicely formatted code first, then either go home or work on the eyesores.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Beginner
    Join Date
    Apr 2005
    Location
    uk
    Posts
    8
    the code has been updated with indentations

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Colonial Viper
    the code has been updated with indentations
    Code:
    int main()
    {
    	int a;
    	while(1)
    	{
    		//code
    
    		if(1)
    		{
    			//code
    		}else{
    			//code
    
    			if(0)
    			{
    				//code
    			}elseif(1){
    				//code
    			}else{
    				//code
    			}
    		}
    
    		
    		for(;1;;)
    		{
    		}
    
    	}
    
    	
    	switch(a)
    	{
    
    	}
    
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    random indentation isn't going to help much. You should also use more descriptive names for your variables. wins, losses, draws, points.... (assuming I'm guessing right)

    from what I can tell this snippet of code is in the wrong spot, among other problems

    Code:
        scanf("%d",&tempgf2);
    
        teams[team2].gf= teams[team2].gf+tempgf2;
        teams[team2].p = teams[team2].p + 1;
    I didn't actually fix anything, just the indentation and added a few comments

    Code:
    void detailentry(team* teams)
    {
        int i=0, team1=0, team2=0;
        char teamname[20];
        char teamname1[20];
        int tempgf1;
        int tempgf2;
    
        //=============enter 1st team results============
    
        printf("please enter team1 name\n");    //asks for input
        scanf("%s",&teamname1);                 //checks whats been input
    
        for(team1=0; team1 < 12; team1++) {
            if(strcmp(teams[team1].name,teamname1)==0) {
                printf("and score\n");
                scanf("%d",&tempgf1);
                teams[team1].gf = teams[team1].gf+tempgf1;
                teams[team1].p = teams[team1].p + 1;    //adds 1 to value of p
                // NEED TO CALL IN VALUE OF GF2
    
                // NEED to break out of loop now, team found and updated
                // IF you don't break team1 will always be 12 after this loop
    
    
             }else{
                
             }
        }
    
        //=============enter second teams results==========
    
        printf("please enter team2 name\n");
        scanf("%s",&teamname);
    
        for(team2=0; team2 < 12; team2++) {
           if(strcmp(teams[team2].name,teamname)==0) {
                printf("and score\n");
            }else{
            }
        }
    
        // Wrong Spot !?
        scanf("%d",&tempgf2);
        teams[team2].gf= teams[team2].gf+tempgf2;
        teams[team2].p = teams[team2].p + 1;
    
        printf("%d",tempgf1);
        printf("%d",tempgf2);
    
        /////At this stage tempgf1 and tempgf2  seem to stop working
    
        teams[team2].ga = teams[team2].ga+tempgf1;
        teams[team1].ga = teams[team1].ga+tempgf2;
    
        if(tempgf1 > tempgf2){
            teams[team1].w = teams[team1].w+1;
            teams[team2].l = teams[team2].l+1;
        }else if(tempgf1 < tempgf2) {
            teams[team1].l=teams[team1].l+1;
            teams[team2].w=teams[team2].w+1;
        }else {
            teams[team1].d=teams[team1].d+1;
            teams[team2].d=teams[team2].d+1;
        }
    
        teams[team1].t=(teams[team1].w*3)+(teams[team1].d*1);
        teams[team2].t=(teams[team2].w*3)+(teams[team2].d*1);
    }
    Last edited by spydoor; 04-29-2005 at 12:05 PM.

  10. #10
    Beginner
    Join Date
    Apr 2005
    Location
    uk
    Posts
    8
    Thanks for the indentation.

    the scanf("%d",&tempgf2);

    Code:
    teams[team2].gf= teams[team2].gf+tempgf2;
        teams[team2].p = teams[team2].p + 1;
    the table displayed is as follows if no results have been inputed yet

    team name p w d l gf ga t
    team1_____0 0 0 0 0 0 0
    team2 _____0 0 0 0 0 0 0
    etc up to 12 teams

    Your right p=games played
    w = win,d = draw, l=loss,gf= goals for ga=goals against and t = total.
    the above guide is required so that the user can input the goals for the second team.In my previous code when i was inputed intgers for the team positions the code worked fine.
    The code that was handed in uses functions but i am still curious,puzzled why the above code never worked.with the test statement the tempgf were stored all the way till the if statement at the bottom and then they dereferenced ?
    Last edited by Colonial Viper; 04-30-2005 at 04:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. my code is messed up says the compiler
    By 0927 in forum C++ Programming
    Replies: 11
    Last Post: 01-30-2002, 12:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21