0
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
struct person {
   char user[50];
   char password[50];
   int amount;
};
int i=0;
int h=0;
int *n=&h;
struct person *p = NULL;
void adduser();
int main()
{
    int x;
     printf("Welcome to the bank\n");
    printf("what would you like to do\n");
    printf("type 1 to add a user\n");
    printf("type 2 to add to balance\n");
    printf("type 3 to take from balance\n");
    printf("type 4 to check from balance\n");
    scanf("%d%*c",&x);
    if(x==1){
        adduser();
    }
    else if (x == 2){
        printf("2");
    }
    else if (x == 3){
        printf("3");
    }
    else if (x == 4){
        printf("4");
    }
    else{
        main();
    }
}
    
void adduser(){
    struct person *temp = realloc(p, *n * sizeof(struct person));
    if (temp != NULL)
    p = temp;
    printf("n=%d\n",*n-1);
    printf("enter your new username\n");
    scanf("%s",&(p+*n)->user);
    int o=*n-1;
    for (i;i<=o;i++)
    {
        if(strcmp((p+*n)->user,(p+i)->user) == 0)
        {
            printf("user already exists");
            adduser();
        }
    }
    printf("enter your new password\n");
    scanf("%s",(p+*n)->password);
    *n+=1;
    main();
}

hello I'm a beginner with C I'm trying to see i have string the user input similar to one in my structure using this code.

   for (i;i<=o;i++)
    {
        if(strcmp((p+*n)->user,(p+i)->user) == 0)
        {
            printf("user already exists");
            adduser();
        }
    }

my code is supposed ask the user to enter their new user(at least what i posted) and checks if a similar user exists but my loop gets ignored i don't know what to do with it . i feel like my problem lies elsewhere but i cant figure it out. Also i might have done fundamental mistakes because I'm very new to structures and pointers.

i tried searching online for solutions but i couldn't find a similar situation.

oma
  • 1
  • 1
  • 1. Please use meaningful variable names. 2. Please use function parameters, not globals. 3. Please use tabs. 4. How much memory do you [`realloc()`](https://en.cppreference.com/w/c/memory/realloc)? – Ken Y-N Dec 23 '20 at 23:49
  • In your `strcmp((p+*n)->user, ...)`, it looks like `p` is the user list array, and `n` is the number of entries in there. You want your loop variable, `o`, instead: `strcmp((p+o)->user, ...)`. That said, I second Ken Y-N's comments. As a general rule on naming: the wider the variable scope, the more explanatory the name should be. A single-letter loop variable might be okay; globals should have better names like `user_list` or `user_count`. – Perette Dec 23 '20 at 23:53
  • It looks like you never increment h or o or *n, so o will always be 0 and your loops will never loop over anything (any you'll also allocate 0 bytes for your p array)... – Chris Dodd Dec 23 '20 at 23:55
  • 1
    `*n=&h;` This can't possibly compile. Please post real code. `for (i;i<=o;i++)` Why is `i` global, and why is it not initialized here. – dxiv Dec 23 '20 at 23:56
  • @ChrisDodd: you missed `*n+=1;` – Joshua Dec 23 '20 at 23:56
  • @KenY-N i understand 1 and i tabbed everything to the same length because it didn't let me write the code in that format otherwise(still new to this).the memory is supposed to be growing (using the *n+=1) so the user can input as much as they want. and can you please tell me why not to use globals i found it convenient because i can use them on multiple functions. thanks – oma Dec 23 '20 at 23:56
  • @dxiv just typed it along the way since i have no idea if there is any difference. – oma Dec 23 '20 at 23:58
  • 1
    @oma Your question implies that the issue is when you run the code. But the code you posted does not compile, let alone run. So nobody can guess what the real code is that you are having issues with. Of course that's a problem, a big problem. See [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – dxiv Dec 24 '20 at 00:00
  • @Joshua: That is after the loops and the allocations – Chris Dodd Dec 24 '20 at 00:01
  • @dxiv ah i didnt know that since my compiler works(called c-free) what compiler should i use? – oma Dec 24 '20 at 00:08
  • @oma C-Free is an IDE, not a compiler, and I don't think any of the compilers it works with would compile statements at the global scope, outside any function. Voting to close the question, until you [edit](https://stackoverflow.com/posts/65432246/edit) it and copy/paste the actual code you are having problems with. – dxiv Dec 24 '20 at 00:18
  • @oma (*Removed closing vote after the edit.*) In addition to the answer that was posted, you could add a `printf` right before the `for` loop and look at the values of `i` and `o`, which should explain some of what's going on. – dxiv Dec 24 '20 at 00:48
  • its working now as intended after the n pointer fix and some tweaks thanks @dxiv by the way i want to ask is it bad to loop the main like i did in the code if i want to go back to the starting page? – oma Dec 24 '20 at 01:30
  • @oma It is allowed for `main` to call itself (in C, though not in C++), see for example [calling main() in main() in c](https://stackoverflow.com/questions/4238179/calling-main-in-main-in-c). But it is not a common pattern, and you rely on the compiler to optimize the [tail recursion](https://en.wikipedia.org/wiki/Tail_call) otherwise you will overflow the stack eventually. It would be safer (and also easier to follow) if you moved that code into a separate function, and called that function in a loop from main. – dxiv Dec 24 '20 at 03:37

1 Answers1

0

This code is clearly in error:

struct person *temp = realloc(p, *n * sizeof(struct person));

since we're almost immediately going to do:

scanf("%s",&(p+*n)->user);

this really needs to read

struct person *temp = realloc(p, (*n + 1) * sizeof(struct person));

In addition,

for (i;i<=o;i++)

is clearly wrong and should be

for (i=0;i<=o;i++)

and as dxiv points out, i really should be declared in this for loop rather than as a global.

Incidentally, are you having trouble with loops? I don't see that recursive call of main() much. It's really clever, but this code works well only if compiled with optimizations on (which in turn is harder to debug). I'd advise replacing

else{
    main();
}

and the other calls to main() with an infinite loop around all of main() (which eventually will have a loop exit condition or a conditional break; statement on the input ladder.

Joshua
  • 40,822
  • 8
  • 72
  • 132