-3

so I'm relatively a beginner at C and I'm working for a code about account structs just for fun.
Also I'm new at stack overflow.
I am making a function named accN() where they use the printf and gets to input username, password, and email.
I'm guessing there is a lot of errors in the code. Anyway the code:

#include <stdio.h>
#include <string.h>
struct account{
    char accun[30];
    char accpw[30];
    char accem[30];
};
struct account accN(){
    struct account temp;
    char temps[3][30];
    printf("username:\n");
    gets(temps[0]);
    for (i = 0;  i < sizeof(temps[0]); ++i){
        if (temps[0][i] == ' '){
            for (j = i; j < sizeof(temps[0]); ++j){
                temps[0][j] = temps[0][j+1];
            }
        }
    }
    printf("password:\n");
    gets(temp.accpw);
    for (i = 0;  i < sizeof(temps[1]); ++i){
        if (temps[1][i] == ' '){
            for (j = i; j < sizeof(temps[1]); ++j){
                temps[1][j] = temps[1][j+1];
            }
        }
    }
    printf("email:\n");
    gets(temp.accem);
    for (i = 0;  i < sizeof(temps[2]); ++i){
        if (temps[2][i] == ' '){
            for (j = i; j < sizeof(temps[2]); ++j){
                temps[2][j] = temps[2][j+1];
            }
        }
    }
    strcpy(temp.accun, temps[0]);
    strcpy(temp.accpw, temps[1]);
    strcpy(temp.accem, temps[2]);
    return temp;
}
int main(){
    struct myacc = accN();
}
Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
  • 5
    [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036) – 001 May 20 '21 at 23:30
  • 1
    `"I'm guessing there is a lot of errors in the code. Anyway the code:"` -- Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel May 20 '21 at 23:30
  • 1
    Also, the 2nd & 3rd calls to `gets` are using the struct and not the array: `gets(temp.accpw);` – 001 May 20 '21 at 23:30
  • 4
    What's the question? – William Pursell May 20 '21 at 23:31
  • You've shown us your code, but you haven't actually asked a question. Your code doesn't compile, but you didn't include the error messages in your question. – Keith Thompson May 21 '21 at 00:25

1 Answers1

1

Your code has multiple issues. In your for loops, you are putting down i = 0; but not specifying that i should be an integer. The correct code should be int i = 0;. You are also getting the length of the gotten string using sizeof(). While this technically works, it can make code easy to break. You should instead use strlen(). In your main function, you create a variable without a type. Also, you just shouldn't use gets() in general. Instead, use fgets()

Finxx
  • 61
  • 1
  • 7