【问题标题】:Output a two dimensional array using basic C language [closed]使用基本 C 语言输出二维数组 [关闭]
【发布时间】:2019-12-04 16:09:05
【问题描述】:

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

这就是我想要的输出:

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

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

下面是我的代码。请原谅我的语法,我对编码很陌生。

#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;
    }
}

【问题讨论】:

  • printfscanf 真的长到你觉得需要用预处理器宏来缩写它们吗?
  • 我建议你寻求一个好的助教的帮助。我们这里没有设置延长辅导。
  • 是的,我们的任务是编写一个包含所有 C 语言语法的程序,上面的代码是我尝试制作一个可以输入 10 个学生姓名的程序。我不知道这是否正确,但我朋友告诉我,我可以只做很多变量(name1-name10),但问题是我们还需要获取中间名和姓氏。
  • @fred idk 我们的老师先做的 lmao
  • 在这和“任务用所有 C 语言语法编写程序”之间,我对你的老师有严重的怀疑。

标签: c arrays printf


【解决方案1】:

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

使用结构数组:

#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

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

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;
}

【讨论】:

  • 感谢您的帮助!!!正如你提到的,我必须制作一个 w/ 结构,现在一切都很好。太感谢了。 struct studentname { char fname[50]; char mname[50]; char lname[50]; } student[10]; 对不起,如果我派生了你的工作,老实说,我不能将 fgets 用于我们的程序
  • @DyrdKmp - 没问题,此代码是免费提供的!是你的老师坚持不使用fgets()吗?
【解决方案2】:

输出二维数组是一项简单的任务,您可以将其视为数组中的数组。您需要做的就是在两个数组上进行迭代,并以您喜欢的任何方式打印。

#include <stdio.h>

int main(void) {
    int array[4][2] = { // 4 arrays, each has 2 elements
        { 1, 2 }, // array[0]
        { 3, 4 }, // array[1]
        { 5, 6 }, // array[2]
        { 7, 8 }, // array[3]
    };

    for (int i = 0; i < 4; ++i) { // iterate on the 4 arrays we have
        for (int j = 0; j < 2; ++j) { // iterate on each element of those arrays
            printf("%d", array[i][j]); // print out that element
        }
    }
}

现在,想想如何将它整合到您的程序中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 2016-04-22
    • 2016-08-04
    • 2018-09-30
    • 2022-01-10
    相关资源
    最近更新 更多