1 Attachment(s)
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!Attachment 11371
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;
}