使用基本C语言输出二维数组 [英] Output a two dimensional array using basic C language

查看:282
本文介绍了使用基本C语言输出二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个可以输入10个学生姓名的程序,但我只是不知道如何存储它们。我做了下面的代码,我真的认为没有错误,这只是有关如何使用名称输入的草稿。

I'm trying to make a program that can input 10 student's name, but I just don't know how to store them. I did the code below and I really think that there is no mistake, it is just a draft on how I would use the inputting of names. We are only allowed to use C language for this.

这就是我希望输出的方式:

This is how I want my output to be:

List:
arr[0][50]=jason
arr[1][50]=jade
.
.
.
arr[10][50]=mark

但它一直显示错误,我只能在它显示第一个相等值之前运行它。

But it just keeps showing an error, I can only run it before it shows the first equal.

下面是我的代码。

#include<stdio.h>
#define pf printf
#define sf scanf

#define ENTER 13
#define TAB 9
#define BKSP 8

main()
{
    char g7nameinput[0][50];
    char g7fname[10][50], g7mname[10][50], g7lname[10][50];
    char ch, choice;
    int incname, incnamelist=0, i, max=50;

    createstudent:
    pf("\n\nInput first name: ");
    incname=0;
    while(1)
    {
        ch=getch();

        if(ch==ENTER)
        {
            g7nameinput[0][incname]='\0';
            break;
        }
        else if(ch==BKSP)
        {
            if(incname>0)
            {
                incname--;
                pf("\b \b");
            }
        }
        else if(ch==TAB)
        {
            continue;
        }
        else
        {
            g7nameinput[0][incname]=ch;
            incname++;
            pf("%c",ch);
        }
    }
    pf("\nFirst Name inputted is %s",g7nameinput);
    pf("\n\nInputted correct? [Y/N]: ");
    sf("%c",&choice);

    if((choice=='Y')||(choice=='y'))
    {
        g7fname[incnamelist][50] = g7nameinput;
        incnamelist++;

        pf("\n\nList:");
        for(i=0;i<incnamelist;i++)
        {
            pf("\narr[%d][%d]=%s",i,max,g7fname[i][50]);
        }

        goto createstudent;
    }
    else
    {
        goto createstudent;
    }
}


推荐答案

但我只是不知道如何存储它们。...

使用结构体数组:

#define MAX_NAMES 10 //per your stated maximum

typedef struct {
  char first[80];
  char middle[80];
  char last[80];
}NAMES;

NAMES names[MAX_NAMES] = {0};//a container with space for 10 first, middle and last names

一种通过主函数输入学生姓名的非常简单的方法:

And a very simple way to input student names via a main function:

int main(void)
{
    int i;

    for(i=0;i<MAX_NAMES;i++)
    {
        printf("Enter the First name of student %d\n", i+1);
        fgets(names[i].first, sizeof(names[i].first), stdin);
        printf("Enter the Middle name of student %d\n", i+1);
        fgets(names[i].middle, sizeof(names[i].middle), stdin);
        printf("Enter the Last name of student %d\n", i+1);
        fgets(names[i].last, sizeof(names[i].last), stdin);
    }

    return 0;
}

这篇关于使用基本C语言输出二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆