开发者

C program to calc avg etc

i wrote this code in class today with the teacher helping me but I'm home now and need guidance, I'm not sure what i should do next to get it to compile atleast

the objective is to:

create a menu enter a number(option A) dispaly the average (option B) display the highest and lowest number(option C and D) display the total of all numbers entered(option E) display the total amount of numbers entered(option F) and quit(option G)

here is what i have so far, i apologies if its messy

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

//int getNumber (aNumber) {
 //   printf("Enter an integer between 0 and 1000.\n");
//    scanf("%i", &aNumber);
 //   int result;
 //   }
char getMenuLetter();
int getNumber();
//declare variables

int aNumber = 0;
float avg = 0.0;
int high = -1;
int low = 1001;
int total = 0;
int count = 0;
char getChoice = 'x';

int main() {

//proptotype functions



do {
    getChoice = getMenuLetter();

    switch (getChoice)
           case 'A':
           aNumber = getNumber();
           count++;
           total += aNumber;
           low = testLow(aNumber, low)
           high = testHigh(aNumber, high);
                        break;
           case 'B';
           avg = (double) total/count; //display avg
           printf("The average is %.2f", avg);
                       break;
           case 'C':
           high = getHigh();
           printf("The highest value of all the numbers entered is %i.\n", high); //display highest number
                       break;
           case 'D':
           low = getLow;
           printf("The lowest value of all the numbers entered is %i.\n", low); //displayer lowest value
                       break;
           case 'E':
           printf("The total of all the numbers entered is %i.\n", total);
                       break;
           case 'F':
           printf("The amount of numbers entered so far is %i.\n", count);
           case 'G';
                       break: //end switch


} while (userChoice != 'G');

}

int testLow(int n) {
    int result;

    if (n < low)
          result = n;
    else

    return 0;

} //End of main

char getMenuLetter() {
     char result;
     system("cls") //clear the screen.

     printf("*************************************************\n");
     printf("A) Enter a number between 0 and 1,000\n");
     printf("B) Display the average\n");
     printf("C) Display the highest value entered\n");
     printf("D) Display the lowest value entered\n");
     printf("E) Display the sum of all numbers\n");
     printf("F) Display the count of all numbers entered\n");
     printf("G) Quit the program\n");
     printf("*************************************************\n");
     scanf("%c", &result);
     result =toupper(result);
///print f %c 
//system pause

if (result != 'A' || result != 'B' || result !='C' || result !='D' || result !='E' ||开发者_运维百科 result != 'F' || result !='G'){
            printf("You must enter A - G only! \n)");
            system("pause");
 } //end if

} while(result != 'A' || result != 'B' || result !='C' || result !='D' || result !='E' || result != 'F' || result !='G');
     return result;
     //end of GetMenuLetter


Here is what I suggest:

  1. Compile your program first. Your compiler will return most of your errors (the important ones, at least).
  2. Pay attention to your use of curly bases. In C (and in many other languages), the compiler will treat lines that follow other lines linearly. The curly braces cause a multidimensional interpretation. As a beginner to programming, you should practice using curly braces where you can, just so you get into the habit of segregating instructions. Also, you should pay close attention to matching your open curly braces with your closed curly braces. For more information, you should see the C Standard, 6.8: Statements and Blocks.
  3. Your switch() block should end with a default: value, just in case you reach a choice that's unexpected.
  4. I don't suggest putting your functions prototype inside your main() procedure. It has to do with scopes. Check this out, from Section 6.2.1 of the standard.

2 For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope. Different entities designated by the same identifier either have different scopes, or are in different name spaces. There are four kinds of scopes: function, file, block, and function prototype. (A function prototype is a declaration of a function that declares the types of its parameters.)

I don't know what else to tell you. Try what I proposed in order. Make sure you read the standard though. As a final suggestion: try programming in a more ordered manner. Your code won't look so sloppy if you keep coding under the intent of wanting to make something you can read by the time you're finished.

Good luck.


Some hints:

  • Check your compiler errors and warnings beginning with the first.
  • Switch on additional warnings of your compiler (e.g. parameters -W -Wall for gcc).
  • There is a significant difference between ";" and ":" in C.
  • The body of a switch statement has to be enclosed in curly braces.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜