Help figuring out problem with my first C program

This is a discussion on Help figuring out problem with my first C program within the C Programming forums, part of the General Programming Boards category; I am attempting to write a program (for school) that will accept data about the length of metal rods and ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19

    Help figuring out problem with my first C program

    I am attempting to write a program (for school) that will accept data about the length of metal rods and sort them into three categories (too long, too short, and normal), tally up the overall lengths of all three categories, and average the lengths of all three categories (and give total number of rods, total length of all rods, and total of all averages). My professor also wants the program to display an error (but continue working) if the data set continues less than or more than 20 metal rods and he wants each data entry to display on the screen immediately following its entry to ensure accuracy of entry. (phew...that's a lot of background!). Anyhow, I've attached my programmed below - I couldn't get it to copy-paste, no matter how many which ways I did the whole tags things. It just kept giving me errors and not letting my post. I think I am just cursed today in general.

    The problem is that I can't even get the program to run as is! I get an error telling me that the program is expecting a ; before the { of my else if statement, but I know that it incorrect (and if I put one in, it just makes even more errors!) I have spent quite a few hours trying to reformat the code thinking I must be overlooking something. I even went as far as to break all of my printf functions into separate lines (annoying, I know) just to be sure that I hadn't missed some tiny punctuation mark that was throwing off the whole darn code. Even if someone can just point me to the line or in the right direction, I would be truly grateful as I really don't know what I am missing here!

    Thanks so much!code.txt

  2. #2
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19
    Code:
    #include <stdio.h>
    
    int main(void)
    
    {
    double metal_rod_length_cm; //length of each individual metal rod
    double length; //temporary storage for normal length rods entered
    
    int normal_count; //total count of metal rods that are exactly 10 cm  long
    int short_count; //total count of metal rods that are <10 cm long
    int long_count; //total count of metal rods that are >10 cm long
    int overall_count; //total count of normal rods, short rods, and long  rods
    
    int normal_total; //sum of lengths of all normal metal rods
    int short_total; //sum of lengths of all short metal rods
    int long_total; //sum of lengths of all long metal rods
    int overall_total; //sum of all lengths of normal, short, and long metal  rods
    
    double normal_average; //average length of all normal metal rods
    double short_average; //average length of all short metal rods
    double long_average; //average length of all long metal rods
    double overall_average; //sum of all averages all metal rods
    
    printf("\nWelcome.\n");
    printf("Categorize manufactured metal rods based on length in  centimeters\n");
    printf("and calculate averages of different categories.\n\n");
    printf("Input the length (in centimeters) of each metal rod, one at a  time:"); //prompt for length, in cm.
    
    do{
    scanf("lg", &metal_rod_length_cm); //keyboard input for length of  metal rod
    printf("%g", &metal_rod_length_cm); //screen output for length just  inputted
    
    if (metal_rod_length_cm==10)
    {
    ++normal_count; //increases with each metal rod entered that is exactly  10 cm long
    ++overall_count; //increases with each metal rod measurement that is  entered
    normal_total += length; //adds length of all normal length metal rods
    overall_total += length; //adds all metal rods entered to overall length  total
    }
    
    else if (metal_rod_length_cm <10)
    {
    ++short_count; //increases with each metal rod entered that is less than  10 cm long
    ++overall_count; //increases with each metal rod measurement that is  entered
    short_total += length; //adds length of all short length metal rods
    overall_total += length; //adds all metal rods entered to overall length  total
    }
    
    else (metal_rod_length_cm >10)
    {
    ++long_count; //increases with each metal rod entered that is greater  than 10 cm long
    ++overall_count; //increases with each metal rod measurement that is  entered
    long_total += length; //adds length of all long length metal rods
    overall_total += length; //adds all metal rods entered to overall length  total
    }
    }while (metal_rod_length_cm !=0); //while metal rod length entered does  not equal zero
    
    if (overall_count>20)
    {
    printf("Error: You do not have 20 metal rods in batch to be analyzed.");
    }
    
    else if (overall_count<20)
    {
    printf("Error: You do not have 20 metal rods in batch to be analyzed.");
    }
    
    if (metal_rod_length_cm=0)
    {
    normal_average=normal_total/normal_count;
    short_average=short_total/short_count;
    long_average=long_total/long_count;
    overall_average=overall_total/overall_count;
    }
    
    printf("Statistics:\n");
    printf("Desc\t Number\t Total:\t Average:\n");
    printf("Normal\t");
    printf("%i\t",normal_count);
    printf("%i\t",normal_total);
    printf("%g\n",normal_average);
    printf("Short\t");
    printf("%i\t",short_count);
    printf("%i\t",short_total);
    printf("%g\n",short_average);
    printf("Long\t");
    printf("%i\t",long_count);
    printf("%i\t",long_total);
    printf("%g\n",long_average);
    printf("Overall\t");
    printf("%i\t",overall_count);
    printf("%i\t",overall_total);
    printf("%g\n",overall_average);
    
    return 0;

  3. #3
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19
    There, somehow I could post the code as a reply but not as part of my original text. Go figure!

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    28
    Aren't you missing the final } for the main function?

  5. #5
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19
    that got lost in all the copying and pasting....sorry...that is there on my screen. good catch though. wish it were that simple!

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    343
    Line 49, else (blah) should be else if (blah)

    Line 68, you're assigning 0 to a variable instead of comparing it.

    Other warnings to fix :

    $ gcc -c 1.c -Wall
    1.c: In function `main':
    1.c:30:1: warning: too many arguments for format
    1.c:30:1: warning: too many arguments for format
    1.c:31:1: warning: format `%g' expects type `double', but argument 2 has type `d
    ouble *'
    1.c:31:1: warning: format `%g' expects type `double', but argument 2 has type `d
    ouble *'
    1.c:49:6: warning: statement with no effect
    1.c:50:1: error: expected `;' before `{' token
    1.c:68:1: warning: suggest parentheses around assignment used as truth value
    1.c:95:1: error: expected declaration or statement at end of input

    ETA - forget the last one,it's just complaining about the lack of a } at the end of the code. The rest are worries, though.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19
    Code:
    #include <stdio.h>
    
     
    int main(void)
     
    {
    double metal_rod_length_cm; //length  of each individual metal rod
    double length; //temporary  storage for normal length rods entered
     
    int normal_count;  //total count of metal rods that are  exactly 10 cm  long
    int short_count;  //total count of metal rods that are  <10 cm long
    int long_count; //total count of metal rods that are >10 cm  long
    int overall_count;  //total count of normal rods, short  rods, and long  rods
     
    int  normal_total; //sum  of lengths of all normal metal rods
    int short_total; //sum  of lengths of all short metal rods
    int long_total; //sum of lengths  of all long metal rods
    int overall_total;  //sum of all lengths of normal,  short, and long metal  rods
     
    double normal_average;  //average length of all normal metal  rods
    double short_average;  //average length of all short metal  rods
    double long_average;  //average length of all long metal  rods
    double overall_average;  //sum of all averages all metal rods
     
    printf("\nWelcome.\n");
    printf("Categorize manufactured metal  rods based on length in  centimeters\n");
    printf("and calculate  averages of different categories.\n\n");
    printf("Input the length  (in centimeters) of each metal rod, one at a  time:"); //prompt for  length, in cm.
     
    do{
    scanf("lg",  &metal_rod_length_cm); //keyboard  input for length of  metal rod
    printf("%g", &metal_rod_length_cm); //screen output for length just  inputted
     
    if (metal_rod_length_cm==10)
    {
    ++normal_count; //increases with each metal rod entered that is  exactly  10 cm long
    ++overall_count; //increases  with each metal rod measurement that is  entered
    normal_total  += length; //adds length of all normal  length metal rods
    overall_total += length; //adds all metal rods entered to overall length  total
    }
     
    else if (metal_rod_length_cm  <10)
    {
    ++short_count; //increases  with each metal rod entered that is less than  10 cm long
    ++overall_count;  //increases with each metal rod  measurement that is  entered
    short_total += length; //adds length of all short length metal rods
    overall_total  += length; //adds all metal rods  entered to overall length  total
    }
     
    else (metal_rod_length_cm  >10)
    {
    ++long_count; //increases  with each metal rod entered that is greater  than 10 cm long
    ++overall_count;  //increases with each metal rod  measurement that is  entered
    long_total += length; //adds length of all long length metal rods
    overall_total  += length; //adds all metal rods  entered to overall length  total
    }
    }while (metal_rod_length_cm  !=0); //while metal rod length entered  does  not equal zero
     
    if (overall_count>20)
    {
    printf("Error: You do not  have 20 metal rods in batch to be analyzed.");
    }
     
    else  if (overall_count<20)
    {
    printf("Error: You do not  have 20 metal rods in batch to be analyzed.");
    }
     
    if  (metal_rod_length_cm=0)
    {
    normal_average=normal_total/normal_count;
    short_average=short_total/short_count;
    long_average=long_total/long_count;
    overall_average=overall_total/overall_count;
    }
     
    printf("Statistics:\n");
    printf("Desc\t Number\t Total:\t  Average:\n");
    printf("Normal\t");
    printf("%i\t",normal_count);
    printf("%i\t",normal_total);
    printf("%g\n",normal_average);
    printf("Short\t");
    printf("%i\t",short_count);
    printf("%i\t",short_total);
    printf("%g\n",short_average);
    printf("Long\t");
    printf("%i\t",long_count);
    printf("%i\t",long_total);
    printf("%g\n",long_average);
    printf("Overall\t");
    printf("%i\t",overall_count);
    printf("%i\t",overall_total);
    printf("%g\n",overall_average);
     
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19
    Line 49, else (blah) should be else if (blah)
    Okay, this one I understand and fixed...

    Line 68, you're assigning 0 to a variable instead of comparing it.
    How do I get it to compare then instead of assign? More parentheses?

    Other warnings to fix :

    $ gcc -c 1.c -Wall ???????
    1.c: In function `main': ??????
    1.c:31:1: warning: format `%g' expects type `double', but argument 2 has type `d
    ouble *' what is the difference between 'double' and 'double *'?
    1.c:31:1: warning: format `%g' expects type `double', but argument 2 has type `d
    ouble *'
    1.c:49:6: warning: statement with no effect
    1.c:50:1: error: expected `;' before `{' token How can I add a ';' here, it will mess up the code, won't it? According to what little I know about programming, I thought this was properly formatted....

    I am beginning to think I should have found a course that was more beginner than Intro to programming....

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    28
    Compare is == not =.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    28
    He said to ignore the final error because that was due to the missing bracket for the main function.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    28
    I think the other issue is that your printing a pointer (i.e. &var_name) instead of the variable itself (i.e. var_name).

  12. #12
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19
    Thanks everyone for your help. I'm going to try to tweak it some more with all of your input and see what happens. Cross your fingers for me and pray that I never have to program something more important than a homework assignment lol

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You should make the error messages quit the program, instead of continuing with the said insufficient data.

    Also, line 50, you cannot specify the else condition, if you want to specify use else if. Else is only used for when the data meets none of the conditions.

    Also, your program (even when compiled) doesn't work. I input "123" and it immediately starts printing a series of 010101... to the console. However, it works when I fix some of the obvious warnings/errors.

    A final point to remember is that without using locks or some other form of control, your output can easily be overcome by fast typers, and this mess results:

    Code:
    105678
    910567
    1067
    56710657
    1056767
    510
    67510
    The bold is the 10 you're trying to print out, and everything else is demonstrating the issue.

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    343
    Quote Originally Posted by dustynlily View Post
    Line 49, else (blah) should be else if (blah)
    Okay, this one I understand and fixed...

    Line 68, you're assigning 0 to a variable instead of comparing it.
    How do I get it to compare then instead of assign? More parentheses?

    Other warnings to fix :

    $ gcc -c 1.c -Wall ???????
    1.c: In function `main': ??????
    This is just the command line and the compiler reporting which function the following warnings are in.
    1.c:31:1: warning: format `%g' expects type `double', but argument 2 has type `d
    ouble *' what is the difference between 'double' and 'double *'?
    One's double-precision floating point value, the other's a pointer to a double. Look for unneeded & characters.

    1.c:49:6: warning: statement with no effect
    1.c:50:1: error: expected `;' before `{' token How can I add a ';' here, it will mess up the code, won't it? According to what little I know about programming, I thought this was properly formatted....
    Often when a strange error follows on the line after another problem, fixing the first problem will get rid of the second as well.

  15. #15
    Registered User
    Join Date
    Jan 2012
    Location
    Connecticut
    Posts
    19

    Ok, I'm back with more questions....

    So I have all of the error messages cleared, the program compiles, and its working 99% how I want it to. There are just two small things I am trying to get the program to do that I am having trouble with.

    #1: I want the averages to come out 2 actual decimal place values. The decimal places are showing up, but with only zeroes and visual studio is saying its because (perhaps) I am changing over my doubles to integers when I do my += function. Not sure how to fix this problem! Please help!

    #2: I am trying to right justify my output columns and can't find any information online about how to code for right justification. (Hopefully this is the easier of the two questions I have!)

    See my code as it stands now below:
    Code:
     #include<stdio.h>
    int main(void)
    {
    double metal_rod_length_cm; //length of each individual metal rod
    int normal_count=0; //total count of metal rods that are exactly 10 cm long
    int short_count=0; //total count of metal rods that are <10 cm long
    int long_count=0; //total count of metal rods that are >10 cm long
    int overall_count=0; //total count of normal rods, short rods, and long rods
    int normal_total=0; //sum of lengths of all normal metal rods
    int short_total=0; //sum of lengths of all short metal rods
    int long_total=0; //sum of lengths of all long metal rods
    int overall_total=0; //sum of all lengths of normal, short, and long metal rods
    double normal_average; //average length of all normal metal rods
    double short_average; //average length of all short metal rods
    double long_average; //average length of all long metal rods
    double overall_average; //sum of all averages all metal rods
    printf("\nWelcome.\n");
    printf("Categorize manufactured metal rods based on length in centimeters\n");
    printf("and calculate averages of different categories.\n\n");
    do
    {
    printf("Input the length (in centimeters) of the metal rod, one at a time:"); //prompt for length, in cm.
    scanf("%lg", &metal_rod_length_cm); //keyboard input for length of metal rod
    printf("You entered: %g\n", metal_rod_length_cm); //screen output for length just inputted
    if (metal_rod_length_cm==10)
    {
    ++normal_count; //increases with each metal rod entered that is exactly 10 cm long
    ++overall_count; //increases with each metal rod measurement that is entered
    normal_total +=metal_rod_length_cm; //adds length of all normal length metal rods
    overall_total +=metal_rod_length_cm; //adds all metal rods entered to overall length total
    }
    elseif (metal_rod_length_cm <10 && metal_rod_length_cm!=0)
    {
    ++short_count; //increases with each metal rod entered that is less than 10 cm long
    ++overall_count; //increases with each metal rod measurement that is entered
    short_total +=metal_rod_length_cm; //adds length of all short length metal rods
    overall_total +=metal_rod_length_cm; //adds all metal rods entered to overall length total
    }
    elseif (metal_rod_length_cm >10)
    {
    ++long_count; //increases with each metal rod entered that is greater than 10 cm long
    ++overall_count; //increases with each metal rod measurement that is entered
    long_total +=metal_rod_length_cm; //adds length of all long length metal rods
    overall_total +=metal_rod_length_cm; //adds all metal rods entered to overall length total
    }
    }while (metal_rod_length_cm !=0); //while metal rod length entered does not equal zero
    if (overall_count>20)
    {
    printf("Error: You do not have more than 20 metal rods in batch to be analyzed.\n");
    } 
    elseif (overall_count<20)
    {
    printf("Error: You do not have less than 20 metal rods in batch to be analyzed.\n");
    }
    if (metal_rod_length_cm==0)
    {
    normal_average=normal_total/normal_count;
    short_average=short_total/short_count;
    long_average=long_total/long_count;
    overall_average=overall_total/overall_count;
    }
    printf("Statistics:\n");
    printf("Desc\t Number\t\t Total:\t\t Average:\n");
    printf("Normal\t");
    printf(" %i\t\t",normal_count);
    printf(" %.2i\t\t",normal_total);
    printf(" %4.2f\n",normal_average);
    printf("Short\t");
    printf(" %i\t\t",short_count);
    printf(" %.2i\t\t",short_total);
    printf(" %4.2f\n",short_average);
    printf("Long\t");
    printf(" %i\t\t",long_count);
    printf(" %.2i\t\t",long_total);
    printf(" %4.2f\n",long_average);
    printf("Overall\t");
    printf(" %i\t\t",overall_count);
    printf(" %.2i\t\t",overall_total);
    printf(" %4.2f\n",overall_average);
    return 0;
    }
    

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-22-2010, 12:15 PM
  2. help figuring my output problem out
    By jamort in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2009, 09:04 PM
  3. Help figuring out equation.
    By gator6688 in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2007, 03:17 PM
  4. Need help figuring out what to learn
    By the dead tree in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-17-2004, 11:02 AM
  5. help figuring out a problem
    By kwm32 in forum Windows Programming
    Replies: 2
    Last Post: 03-20-2004, 09:47 PM

Tags for this Thread


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