Thread: Beating my head on my keyboard!

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    Beating my head on my keyboard!

    Hello everyone!

    I'm in the same class as rmchrist. I've been trying to figure out what in the heck I'm doing wrong with this program. I'm a total newb when it comes to C and cannot for the life of me figure out what is wrong with this. I'm using Dev C++ and have all warnings turned on. It gives me all kinds of errors while compiling. Can anyone see anything wrong just by glancing at it? It's probably something very simple that I'm missing. Thanks for any advice you may have!

    Code:
    include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ()
    {
    
        char studname[30];
        int grade=0;
        int assignment=0;
        int gradeavg=0;
      
        do
    {
        printf("What is the student's name? ");
        scanf("%s",studname);
    }
        for(assignment=1;assignment<=5;assignment++)  //getting grades 1-5
    {
        printf("What is the score for %s's assignment #%d? ",studname,assignment);
        scanf("%d",&grade);
        gradeavg=gradeavg+grade;
        while(strcmp(studname,"bye")==0);//ending loop when studname is "bye"
    }
    {
        gradeavg=gradeavg/5;
        printf("%s's average is %d. \n",studname,gradeavg);//printing gradeavg
       
    }
      
       
        system("PAUSE");
        end:return 0;
    }

  2. #2
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    well, for a start your brackets are all wrong,:

    Code:
    do
    {
       //code to execute
    }while(boolean_condition);
    €dit: I'm not sure about this one, but I think strcmp returns 0 when it's the same, which means your while argument should be "==1", but I'm not sure on that one...

    £2: What's "end:"? Surely not C(ode)...
    Last edited by mako; 01-15-2006 at 08:46 PM.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Washington D.C.
    Posts
    25
    include statements start off with a #. Look at your first line.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    ah.. thank you! Missed the # with cut and paste =)

    Fixed my brackets and now I only have one error to try and figure out!

    Thank you all sooooo much! Been looking at this thing for entirely too long. I'm very greatful!

    -Travis

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    *sigh* think my brackets are still screwy. No matter what I change, it always errors on the very last line with syntax error.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ()
    {
        char studname[30];
        int grade=0;
        int assignment=0;
        int gradeavg=0;
      
        do
    {
        printf("What is the student's name? ");
        scanf("%s",studname);
    
        for(assignment=1;assignment<=5;assignment++)  //getting grades 1-5
    {
        printf("What is the score for %s's assignment #%d? ",studname,assignment);
        scanf("%d",&grade);
        gradeavg=gradeavg+grade;
        
    }
        while(strcmp(studname,"bye")==0);//ending loop when studname is "bye"
    {
        gradeavg=gradeavg/5;
        printf("%s's average is %d. \n",studname,gradeavg);//printing gradeavg
      
    system("PAUSE");
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Edit: Never mind.
    2nd edit: Ok, I ran your code through my compiler and then problem is that you forgot to end your do-while loop with a curly brace (put one on the left of "while"). You also have an extra curly brace at the end of your do-while loop. (underneath "while") Remove it.


    xeddiex.
    Last edited by xeddiex; 01-15-2006 at 10:32 PM.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    On the line after your while, you have an opening brace with no matching closing brace. This is probably what's causing your compiler to complain. The code would be much clearer to you, and you would avoid these errors, if you indented it better. You should indent a new level for each opening brace, and go back out for each closing brace.

    Also, your comment next to the while is incorrect, which means the code may not be doing what you think. The loop will exit when studname is *not* "bye", since strcmp returns 0 when the two strings are identical. Also, it's a good idea to use strncmp instead of strcmp as it is safer.
    Last edited by lemnisca; 01-15-2006 at 10:33 PM.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Thank you both very much! That fixed it!

    -Travis

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Yeah, this person has some very ugly code format right there. tlightle, you need to format your code and indent because it's just very hard and frustrating for people to help you when your code is all messed up looking. I had to make it pretty for ya in my compiler in order to fix spot the problematic areas :P. Anyway, I think you're not using the MSVC++ compiler, so, if you're not and you're on Windows. I suggest you give it a try (it's free by the way). It'll make your life a whole lot easier!


    xeddiex.

    [BTW; you have multiple other problems with your code that I haven't mentioned but that shouldn't prevent you from compiling. But I think those were addressed already by the previous posters.]

  10. #10
    old man
    Join Date
    Dec 2005
    Posts
    90
    Yes, the formatting is not nice ... it makes it hard to see what's going on. This is a common format:

    Code:
      while (blah)
      {
        chatter;
        if (more blah)
        {
          yadda;
          etcetera;
        }
      }
    Then you can see how things group together.

    As for logic errors, note that you don't get a new name when you check for "bye" ... so the loop won't end when you want it to.

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Ok I think my formatting is a little better. Thanks for all the help with that and the idiotic errors I was making. Now I keep getting a syntax error before "else;" on compile. I feel stupid working on this stupid thing for 24+ hours! Anybody got any ideas?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ()
    {
        char studname[30];
        int grade=0;
        int assignment=0;
        int gradeavg=0;
      
        If(strcmp(studname,"bye")==0);
                
        {
            printf("Thanks for using my grading app!\n");
            
        }
        else; 
              
                 printf("What is the student's name? ");
                 scanf("%s",studname);
             
           
                 for(assignment=1;assignment<=5;assignment++)  //getting grades 1-5
                {
                     printf("What is the score for %s's assignment #%d? ",studname,assignment);
                     scanf("%d",&grade);
                     gradeavg=gradeavg+grade;
        
                }
     
        gradeavg=gradeavg/5;
        printf("%s's average is %d. \n",studname,gradeavg);//printing gradeavg
         
                       
    system("PAUSE");
    return 0;
    
    }

  12. #12
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by tlightle

    If(strcmp(studname,"bye")==0);

    {
    printf("Thanks for using my grading app!\n");

    }
    else;

    printf("What is the student's name? ");
    scanf("%s",studname);

    [/code]

    Um, that's probably not going to work. What you have is the equivalent of

    Code:
        if (strcmp(studname,"bye") == 0)
           ;
    which does nothing.

    Then you have a set of braces around a printf statement (in Perl they'd call that a bare block, but not sure what it is in C -- I know you can do it in C++)

    Then you have an else.

    Lose the semi-colons on the end of the if and the else, and see how that works....

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by tlightle
    Ok I think my formatting is a little better. Thanks for all the help with that and the idiotic errors I was making. Now I keep getting a syntax error before "else;" on compile. I feel stupid working on this stupid thing for 24+ hours! Anybody got any ideas?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ()
    {
        char studname[30];
        int grade=0;
        int assignment=0;
        int gradeavg=0;
      
        If(strcmp(studname,"bye")==0);
                
        {
            printf("Thanks for using my grading app!\n");
            
        }
        else; 
              
                 printf("What is the student's name? ");
                 scanf("%s",studname);
             
           
                 for(assignment=1;assignment<=5;assignment++)  //getting grades 1-5
                {
                     printf("What is the score for %s's assignment #%d? ",studname,assignment);
                     scanf("%d",&grade);
                     gradeavg=gradeavg+grade;
        
                }
     
        gradeavg=gradeavg/5;
        printf("%s's average is %d. \n",studname,gradeavg);//printing gradeavg
         
                       
    system("PAUSE");
    return 0;
    
    }
    I highlighted in bold the areas you need to fix. "Very" carefully look over them - I think you can manage to figure out the problems now:P

  14. #14
    old man
    Join Date
    Dec 2005
    Posts
    90
    Try something like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char studname[30];
    
    char *
    getname (void)
    {
      printf ("What is the student's name? ");
      scanf ("%s", studname);
      return studname;
    }
    
    
    int main (void)
    {
    /*      char studname[30]; */
        int grade=0;
        int assignment=0;
        int gradeavg=0;
      
    /*    If(strcmp(studname,"bye")==0); */
        while (strcmp (getname(), "bye" != 0)
        {
    /*        printf("Thanks for using my grading app!\n");
            
        }
        else; 
              
                 printf("What is the student's name? ");
                 scanf("%s",studname);
     */         
           
                 for(assignment=1;assignment<=5;assignment++)  //getting grades 1-5
                {
                     printf("What is the score for %s's assignment #%d? ",studname,assignment);
                     scanf("%d",&grade);
                     gradeavg=gradeavg+grade;
        
                }
     
        gradeavg=gradeavg/5;
        printf("%s's average is %d. \n",studname,gradeavg);//printing gradeavg
        }
    
        printf("Thanks for using my grading app!\n");
    
                       
    system("PAUSE");
    return 0;
    
    }
    Last edited by eerok; 01-16-2006 at 11:33 PM.

  15. #15
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    You're checking to see what the user has typed before they have even typed anything.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  3. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  4. Linked List Help
    By Perverse in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2005, 08:33 AM
  5. Linked list, is this correct?
    By scrappy in forum C Programming
    Replies: 5
    Last Post: 11-13-2003, 12:06 AM