所以,我全局定义了我的函数,因此它们被识别,但我不允许全局定义我的变量,现在我的函数定义中无法识别我的变量。 (错误:"标识符-variableName-未定义"。)任何解决此问题的建议都将非常感谢!
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#define PAUSE system("pause")
#define SIZE 10
int main(void) {
void enterNumbers();
void addNumbers();
void numbersAveraged();
void displayNumbers();
int i = 0;
int usersArray[SIZE] = { 0 };
int numbers = 0;
int totalOfInput = 0;
int averageOfInput = 0;
int count = 0;
int option = 0;
do {
printf("==========================================================\n");
printf("=================Number Management System====================\n");
printf("============================================================\n\n");
printf("Things you can do here.\n");
printf("\n1. Give us numbers to work with! \n");
printf("\n2. Let Me add all the numbers you gave me.\n");
printf("\n3. I will display the average of all of the numbers you've put in.\n");
printf("\n4. I will display all of the numbers you put in. \n");
printf("\n5. Exit \n");
scanf("%i", &option);
switch (option) {
case 1:
enterNumbers();
break;
case 2:
addNumbers();
break;
case 3:
numbersAveraged();
break;
case 4:
displayNumbers();
break;
case 5:
exit(0);
break;
default:
printf("This is not a valid choice, please type 1-5. ");
PAUSE;
}
} while (option != 5);
void enterNumbers(); {
for (i = 0; i == SIZE; i++) {
printf("Please type in a number. Enter -1 when you are done entering numbers.");
scanf("%i", &usersArray[i]);
count++;
if (usersArray[i] == -1) {
break;
}
if (i == SIZE) {
printf("That's enough numbers! I can't hold any more I'm afraid.");
}
}
}
void addNumbers(); {
void enterNumbers();
for (i = 0; i < count; i++) {
totalOfInput += usersArray[i];
}
printf("OK, so the total of all of these numbers is: %d", totalOfInput);
}
void numbersAveraged(); {
addNumbers();
averageOfInput = totalOfInput / i;
printf(" The average of the numbers you added was %i", averageOfInput);
}
void displayNumbers(); {
printf("OK, here are all of the numbers you put in. \n");
for (i = 0; i < count; i++) {
printf("%d \n", usersArray[i]);
}
}
}
答案 0 :(得分:0)
C没有嵌套功能。
我们将C中的全局变量声明为main()
以上。
为了存储某些数字的平均值,我们必须定义一个可以包含有理数的浮点变量(即5.69),因为a / b可能没有整数答案(10/3 = 3.33)。 float averageOfInput = 0;
我对您的来源执行了一些基本的更改,但它确实有效。但这不是一个好的或完整的版本。你可以通过学习更多C来改进它。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#define PAUSE system("pause")
#define SIZE 5
///Global Variables
int usersArray[SIZE] = { 0 };
int count = 0;
int numbers = 0;
int totalOfInput = 0;
float averageOfInput = 0;
int i = 0;
///Function declaration
void enterNumbers();
void addNumbers();
void numbersAveraged();
void displayNumbers();
int main(void)
{
int option = 0;
do {
printf("\n");
printf("==========================================================\n");
printf("=================Number Management System====================\n");
printf("============================================================\n\n\n\n");
printf("Things you can do here.\n");
printf("\n1. Give us numbers to work with! \n");
printf("\n2. Let Me add all the numbers you gave me.\n");
printf("\n3. I will display the average of all of the numbers you've put in.\n");
printf("\n4. I will display all of the numbers you put in. \n");
printf("\n5. Exit \n");
scanf("%i", &option);
switch (option) {
case 1:
enterNumbers();
break;
case 2:
addNumbers();
break;
case 3:
numbersAveraged();
break;
case 4:
displayNumbers();
break;
case 5:
exit(0);
break;
default:
printf("This is not a valid choice, please type 1-5. ");
PAUSE;
}
} while (option != 5);
}//End main()
///Function definition
void enterNumbers()
{
int temp;
printf("Please type in a number. Enter -1 when you are done entering numbers.\n");
for (i = 0; i < SIZE; i++) {
scanf("%i", &temp);
if (temp == -1) {
break;
}
else {
usersArray[i] = temp;
count++;
}
}
}
void addNumbers()
{
totalOfInput = 0;
enterNumbers();
for (i = 0; i < count; i++) {
totalOfInput += usersArray[i];
}
printf("OK, so the total of all of these numbers is: %d\n", totalOfInput);
}
void numbersAveraged(){
addNumbers();
averageOfInput = (float) totalOfInput / count;
printf(" The average of the numbers you added was %f\n", averageOfInput);
}
void displayNumbers(){
printf("OK, here are all of the numbers you put in.\n");
for (i = 0; i < count; i++) {
printf("%d \n", usersArray[i]);
}
}