我在编写代码方面非常陌生,而且正在参加C编程课程。
我正在尝试编写一个银行菜单程序。我无法通过函数将值放入数组中。我认为指针部分的最后一个功能是我遇到的困难。我很欣赏答案,但我希望能够理解我做错了什么以及我应该如何认识它。
除此之外,您可以想到的任何改进都将受到赞赏。谢谢!
***此外,请注意整个代码不完整。我需要修复这一部分才能写出其他部分,这就是我被困住的地方。
//
// main.c
// HMenuFunctionArray
//
// Created by Yasmin on 5/13/18.
// Copyright © 2018 Yasmin Hosein. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define DEPOSIT 5
void choiceG(unsigned (*array[DEPOSIT]));
int main() {
char choice;
int sum, maxim, minim, aver = 0;
int count;
int array[DEPOSIT];
do {
printf("\nChoose an option from the menu below:\n");
printf( "\tG\t-\t" );
printf("Get a new deposit\n");
printf( "\tS\t-\t" );
printf("Sum of all deposits\n");
printf( "\tD\t-\t" );
printf("Deposits to be displayed from highest to lowest\n");
printf( "\tA\t-\t" );
printf("Average of all deposits\n");
printf( "\tL\t-\t" );
printf("Lowest deposit will be displayed\n");
printf( "\tQ\t-\t" );
printf("Quit the program\n");
printf("\nYour choice (please use uppercase): ");
scanf("%c", &choice);
switch (choice) {
case 'G':
choiceG (&array[DEPOSIT]);
break;
case 'S':
printf( "G");
break;
case 'D':
printf( "G");
break;
case 'A':
printf( "G");
break;
case 'L':
printf( "G");
break;
case 'Q':
break;
default:
printf("Incorrect menu option selected.\n");
}
}
while (choice != 'Q') ;
}
void choiceG (unsigned *array[DEPOSIT]) {
int a[5] = { 0 };
int i, j = 0;
for(i = 0; i < DEPOSIT; ++i ){
printf("Deposit # %d - $",i);
scanf("%d",*a+i);}
printf("Your deposit amounts are: $ ");
for(j = 0; j < DEPOSIT ; j++)
printf("%p ", (void *) &a[j]);
return ;
}
答案 0 :(得分:0)
int array[DEPOSIT];
创建一个int
数组。确定。
choiceG(&array[DEPOSIT]);
在index = DEPOSIT
传递int的地址。但是,这是一个无效的索引。有效的是0 to DEPOSIT-1
。这应该是:
choiceG(array);
那将传递数组 - 而不仅仅是它的一个元素。接下来,
void choiceG(unsigned (*array[DEPOSIT]));
我认为你想传递数组,所以这应该只是
void choiceG(int array[DEPOSIT]);
最后:
void choiceG(int array[DEPOSIT])
{
int a[5] = { 0 }; // What is this for?
int i, j = 0;
// Get all the deposits from the user
for(i = 0; i < DEPOSIT; ++i ){
printf("Deposit # %d - $", i);
scanf("%d", &array[i]); // Scan directly into the array
}
// Print all the deposits just entered
printf("Your deposit amounts are: $ ");
for (j = 0; j < DEPOSIT ; j++) {
printf("%d ", array[j]); // Print the int value
}
printf("\n"); // Flush stdout
return ;
}
答案 1 :(得分:0)
代码有几个小问题:
因此,当您定义一个接收数组的函数时,只需将指针传递给数据类型就足够了。例如接受整数数组的函数可以声明为:
void functionThatTakesAnArrayOfInts(int *array);
如果要访问该数组中的元素,可以执行以下操作:
array[index]
如果你想要一个指向数组中该索引的指针,你可以这样做:
&array[index]
以下代码运行。
//
// main.c
// HMenuFunctionArray
//
// Created by Yasmin on 5/13/18.
// Copyright © 2018 Yasmin Hosein. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define DEPOSIT 5
void choiceG(int *array);
int main() {
char choice;
int sum, maxim, minim, aver = 0;
int count;
int array[DEPOSIT];
do {
printf("\nChoose an option from the menu below:\n");
printf( "\tG\t-\t" );
printf("Get a new deposit\n");
printf( "\tS\t-\t" );
printf("Sum of all deposits\n");
printf( "\tD\t-\t" );
printf("Deposits to be displayed from highest to lowest\n");
printf( "\tA\t-\t" );
printf("Average of all deposits\n");
printf( "\tL\t-\t" );
printf("Lowest deposit will be displayed\n");
printf( "\tQ\t-\t" );
printf("Quit the program\n");
printf("\nYour choice (please use uppercase): ");
scanf("%c", &choice);
switch (choice) {
case 'G':
choiceG (array);
break;
case 'S':
printf( "G");
break;
case 'D':
printf( "G");
break;
case 'A':
printf( "G");
break;
case 'L':
printf( "G");
break;
case 'Q':
break;
default:
printf("Incorrect menu option selected.\n");
}
}
while (choice != 'Q');
}
void choiceG (int *array) {
int a[5] = { 0 };
int i, j = 0;
for(i = 0; i < DEPOSIT; ++i ) {
printf("Deposit # %d - $",i);
scanf("%d", &a[i]);
}
printf("Your deposit amounts are: $ ");
for(j = 0; j < DEPOSIT ; j++) {
printf("%d ", a[j]);
}
return ;
}
希望这有帮助!