Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8598625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:11:55+00:00 2026-06-12T01:11:55+00:00

I have just learnt about arrays. I was trying to create a database program

  • 0

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 . 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:11:56+00:00Added an answer on June 12, 2026 at 1:11 am

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just learnt about temporary tables and using them has given me some
Have spend quite some time to learn Sencha touch, just like an evaluation about
I have just learnt Cocos2d, but i am not an expert on it. But
I am new to Java Persistence API . I have just learnt it and
I just have learnt how cool RSpec and Cabybara is, and now working around
First the disclaimer: I never learnt any programming in school, and just have to
I am trying to learn some DirectX API and, for now, I have just
I have a simple script trying to learn about hashes in Perl. #!/usr/bin/perl my
I'm just about ready to cry. I have read the php.net manual page, tried
I have been trying to write an basic authentication system as part of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.