سؤال

First off, here's the code:

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
FILE *stream;

int main()
{
    int clients;
    printf("Number of Clients: ");
    scanf ("%d", &clients);
    int age;
    char name[100];
    char gender[100];
    char test[100];

    for (int x = 0; x < clients; x++)
    {
        printf("Enter your name: ");
        scanf("%s", &name[x]);
        printf("Enter your age: ");
        scanf("%d", &age);
        printf("Enter your gender (M/F): ");
        scanf("%s", &gender[x]);

        stream = fopen ("input.txt", "w");

        fprintf (stream, "Client #%d\n", x+1);
        fputs ("Name:   ", stream);
        fputs (name, stream);
        fprintf (stream, "\nAge:    %d", age);
        fputs ("\nGender: ", stream);
        fputs (gender, stream);
        fclose (stream);

    }

    system("input.txt");
    stream = fopen ("input.txt", "w");

    return 0;
}

Output when there's 1 client is normal:

Client #1
Name:   Jack
Age:    23
Gender: M

However, everything goes wrong when the number of clients are added:

Client #2
Name:   JSam
Age:    10
Gender: MF

Expected output should be:

Client #1
Name:   Jack
Age:    23
Gender: M

Client #2
Name:   Sam
Age:    10
Gender: F

What I tried but failed doing since I'm inexperienced in coding:

  • Setting an array to name, age and gender. (e.g. name[x], age[x] | Not sure if this makes array)
  • Attempted changing fopen so that multiple txt files would be made per entry http://ubuntuforums.org/showthread.php?t=1098904 - Failed since I couldn't get rid off all the errors.

Would really appreciate it if someone could point out what I'm doing wrong or help me revise the code. My main goal would be the said Expected Output by using fputs, fprints, fgets and etc. within a for loop.

هل كانت مفيدة؟

المحلول

well, your first error is that you define a character array of 100 characters, and you increment over it like it was a double dimension array:

int age;
char name[100];
char gender[100];
char test[100];

let's run your example, here's the memory of your array:

name = [ ][ ][ ][ ][ ][ ]

then you write into this "Jack" at position 0:

name = [J][a][c][k][\0]

then you write into this "Sam" at position 1:

name = [J][S][a][m][\0]

and here you have your bug!

So basically, what you want is to reuse your variables instead of iterating uselessly over them (you can thank @Pandrei and @M-Oehm for correcting me over that part):

int main()
{
    int clients;
    printf("Number of Clients: ");
    scanf ("%d", &clients);
    int age;
    char name[100];
    char gender;
    char test[100];

    stream = fopen ("input.txt", "w");

    for (int x = 0; x < clients; x++) {
        printf("Enter your name: ");
        scanf("%s", &name); // here you rewrite your variable
        printf("Enter your age: ");
        scanf("%d", &age); // here you were already doing it
        printf("Enter your gender (M/F): ");
        scanf("%c ", &gender); // here you only need one character, you don't need a full string 


        fprintf (stream, "Client #%d\n", x+1);
        fputs ("Name:   ", stream);
        fputs (name, stream);
        fprintf (stream, "\nAge:    %d", age);
        fputs ("\nGender: ", stream);
        fputs (gender, stream);

    }

    fclose (stream);



    // I don't understand what you meant below, but executing a text file might not work, and if it works could be **really** dangerous, the below should be erased!
    // system("input.txt");
    // here you open again a stream without using it!
    // stream = fopen ("input.txt", "w");

    return 0;
}

Just be aware that if you use a name that is longer that 100 characters, you may run into a buffer overflow issue. That's why you'd better use fgets() instead of scanf().

But you should definitely first open the Kernighan and Ritchie book and read the chapters about memory management, character arrays aka strings…

You should also look at answers on Stack Overflow about "why scanf is dangerous?", or that FAQ entry

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top