简体   繁体   English

C中使用数组的类似数据库的基本程序

[英]Basic database-like program in C using arrays

I have just learnt about arrays. 我刚刚学习了数组。 I was trying to create a database program using arrays. 我试图使用数组创建一个数据库程序。 It's a very basic program. 这是一个非常基本的计划。

#include<stdio.h>
#define N 1 //number of entries needed
int main()
{
    int i, k = 1, l = 1, w, x = 0, y = 0;
    int rollnum[N], hsc[N], cet[N], a[N], b[N];
    char name[100], city[100], c;
    for(i = 0; i < N; i++)
    {
        printf("%d.\n", (i+1));
        printf("Enter first name : ");
        do
        {
            c = getchar();
            if(c != '\n')
            {
            name[k] = c;
            k++;
            }
        }
        while(c != '\n');
        a[i] = k;
        k++;
        printf("\n");
        printf("Enter roll number : ");
        scanf("%d", &rollnum[i]);
        printf("\n");
        getchar();
        printf("Enter city : ");
        do
        {
            c = getchar();
            if(c != '\n')
            {
            city[l] = c;
            l++;
            }
        }
        while(c != '\n');
        b[i] = l;
        l++;
        printf("\n");
        printf("Enter HSC percentage : ");
        scanf("%d", &hsc[i]);
        printf("\n");
        printf("Enter CET marks : ");
        scanf("%d", &cet[i]);
        printf("\n");
        getchar();
    }

printf("\n\n\n");
k = 1;
l = 1;
for(i = 0; i < N; i++)
{
    printf("Entry %d\n", (i+1));
    printf("Student Name : ");
    x = (a[i] - x);
    for(w = 0; w < x; w++ && k++)
        putchar(name[k]);
    putchar('\n');
    printf("Roll number : %d", rollnum[i]);
    printf("\n");
    printf("City : ");
    y = (b[i] - y);
    for(w = 0; w < y; w++ && l++)
        putchar(city[l]);
    putchar('\n');
    printf("Marks : \n");
    printf("\t");
    printf("HSC : %d ", hsc[i]);
    printf("\t");
    printf("CET : %d / 200", cet[i]);
    printf("\n\n\n");
}
return 0;
}

The program is not functioning the way I want it to! 该程序没有按照我想要的方式运行! When I enter a name, the first letter is being printed out twice, same is the case with city! 当我输入一个名字时,第一个字母被打印两次,与城市的情况相同! If I put 2 entries by modifying my 'N' , I'm getting the first letters of name and address(of second entry) as garbage values . 如果我通过修改我的'N'来输入2个条目,我将获得名称和地址(第二个条目)的第一个字母作为垃圾值。 I don't think there is any error in my logic, because i tried doing it manually in my notebook and I did not find any fault in it. 我不认为我的逻辑中有任何错误,因为我尝试在我的笔记本中手动执行它并且我没有发现任何错误。

Can anyone help me find the mistake? 任何人都可以帮我找到错误吗? I know the program might not at all be good and efficient, but I'm just trying out stuff that I've learnt! 我知道这个程序可能根本不是好的和有效的,但我只是在尝试我学到的东西!

You have made some mistakes initializing some variables. 你在初始化一些变量时犯了一些错误。 Also you don't have to read a string one character at a time. 此外,您不必一次读取一个字符串。 You can use scanf("%s", someString) to read a whole string. 您可以使用scanf("%s", someString)来读取整个字符串。

And here is a working code that looks a lot more cleaner: 这是一个看起来更清洁的工作代码:

#include<stdio.h>
#define N 2 
int main()
{
    int rollnum[N], hsc[N], cet[N], i;
    char name[100][100], city[100][100];
    for(i = 0; i < N; i++)
    {
        printf("%d.\nEnter first name : ", (i+1));
        scanf("%s", name[i]);
        printf("\nEnter roll number : ");
        scanf("%d", &rollnum[i]);
        printf("\nEnter city : ");
        scanf("%s", city[i]);
        printf("\nEnter HSC percentage : ");
        scanf("%d", &hsc[i]);
        printf("\nEnter CET marks : ");
        scanf("%d", &cet[i]);
        printf("\n");
    }

printf("\n\n\n");

for(i = 0; i < N; i++)
{
    printf("Entry %d\nStudent Name : %s\nRoll number : %d\nCity : %s\nMarks : \n\tHSC : %d \tCET : %d / 200\n\n\n", (i+1), name[i], rollnum[i], city[i], hsc[i], cet[i]);
}

return 0;
}

It works for multiple entries too. 它也适用于多个条目。

In order to make your program work you have to replace this: 为了使你的程序工作,你必须替换它:

x = (a[i] - x);
    for(w = 0; w < x; w++ && k++)
        putchar(name[k]);
    putchar('\n');
    printf("Roll number : %d", rollnum[i]);
    printf("\n");
    printf("City : ");
    y = (b[i] - y);
    for(w = 0; w < y; w++ && l++)
        putchar(city[l]);

with this: 有了这个:

if (i == 0)
  x = a[i]-1;
else
  x = a[i] - a[i-1];
for(w = 0; w < x; w++)
    putchar(name[k++]);
putchar('\n');
printf("Roll number : %d", rollnum[i]);
printf("\n");
printf("City : ");
if (i == 0)
  y = b[i]-1;
else
  y = b[i] - b[i-1];
for(w = 0; w < y; w++)
    putchar(city[l++]);

the problem was that you didn't calculate the length of the words correctly, and also another error that I still can't explain but I removed that to. 问题是你没有正确计算单词的长度,还有另一个错误,我仍然无法解释,但我删除了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM