c - 如何使用函数将用户输入读取到数组中?定义错误?

标签 c arrays function

为什么我的函数调用无法正常工作?我正处于程序的开始阶段,我正在尝试将用户数据读入两个单独的数组。一个数组用于存储字符串,另一个数组用于存储整数,然后打印输入的内容。这应该是一个类小组项目,但我的小组没有出现在类里面,所以我很感谢我收到的任何帮助。

我正在尝试添加此冒泡排序,但正确定义 SIZE 给了我一个错误......

    #define SIZE 5
#include <stdio.h>
#include <string.h>
#include <stdio.h>


void input(char fullname[][25], int age[]);
void output(char fullname[][25], int age[]);
int compare(int x, int y);
void bubbleSort(int fullname[],int SIZE );

int main(int argc, char *argv[]) 
{
    char fullname[SIZE][25];
    int age[SIZE];
    // promt user for names and ages
    input(fullname, age);
    //output unsorted names and ages
    output(fullname, age);


    return 0;
}

void input(char fullname[][25], int age[]) 
{
    int i;
    for (i = 0; i < SIZE; i++) 
    {
        fflush(stdin);
        printf("Enter a full name\n");
        //scanf("%[\^n]\n", fullname[i]);
        fgets (fullname[i],40, stdin);
        printf("Enter the age\n");
        scanf("%d", &age[i]);

    }
}

void output(char fullname[][25], int age[]) 
{
    int i;
    for (i = 0; i < SIZE; i++)
        printf("%s, %d\n", fullname[i], age[i]);
}

最佳答案

这可能会有所帮助

#include <stdio.h>

#define SIZE 5

void input(char fullname[][25], int age[]);
void output(char fullname[][25], int age[]);

int main(int argc, char *argv[]) {
    char fullname[SIZE][25];
    int age[SIZE];
    input(fullname, age);
    output(fullname, age);
    return 0;
}

void input(char fullname[][25], int age[]) {
    int i;
    for (i = 0; i < SIZE; i++) {
        printf("Enter a full name\n");
        scanf("%s", fullname[i]);
        printf("Enter the age\n");
        scanf("%d", &age[i]);
    }
}

void output(char fullname[][25], int age[]) {
    int i;
    for (i = 0; i < SIZE; i++)
        printf("%s, %d\n", fullname[i], age[i]);
}

关于c - 如何使用函数将用户输入读取到数组中?定义错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29644061/

相关文章:

c - 我的 C 代码可移植且字节序独立吗?

objective-c - 在 c 函数中使用 performSelector 来调用委托(delegate)

C# 按多种不同条件对数组进行排序

php - 在数组中搜索值匹配并替换值 - php

c - OpenMP 全局私有(private)变量

用于重新排列列表的 Python 标准库函数

Javascript call() & apply() vs bind()?

c++ - 是否有调试器可以让我通过代码向后退一步?

创建一个初始化结构并返回指针的 C 函数

c# - DefaultIfEmpty 不起作用