-3

I've tested the code and the error seems to be coming from the second scanf()

scanf("%s", password);

I've commented the entire if statement out testing what is working and what isn't I've also tested to see if strcmp() was working and it was. I don't understand why I'm getting a segmentation fault on that scanf()

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "Admin.h"
//#include "customer.h"


int main(int argc, char *argv[])
{
        typedef char * String;
        String AdminUser = "Admin";
        String AdminPW = "Admin";
        String username, password;

        printf("Welcome to Online Banking/ATM System\n");
        printf("====================================\n\n\n");

        printf("Enter your Customer/Admin ID: ");
        scanf("%s", username);

        if (strcmp(username, AdminUser) == 0) {
                printf("Enter your Customer/Admin Password: ");
                scanf("%s", password);

                if (strcmp(password, AdminPW) == 0) {
                        while(1){
                                break;
                        }
                }
        }
}

Also on a side note, if anyone could explain to me how to read and write files it would be great. I understand the basics of opening and closing a file and also reading and writing, but specifically what I need to know is how to write information into a file in such a way that I can refer back to it and pick pieces of information from it. EX:There's a list of people's banking information within a text file, I specifically want to see customer ID 12345. How can I do that, how can I edit it, and how can I delete it.

I'll probably figure it out eventually but help would be greatly appreciated!

Str8Dev
  • 1
  • 3
  • 3
    `typedef char *String` is a ***TERRIBLE*** idea. Never do this. – MD XF Apr 30 '17 at 03:28
  • You have not given `username` a value – Ed Heal Apr 30 '17 at 03:28
  • 1
    There are lots of questions on stackoverflow about reading and writing files. Do a search and you'll get some great help. You can also get working examples from sites such as cpluslus.com, tutorialspoint.com, and other websites and forums. – mgarey Apr 30 '17 at 03:29
  • 1
    Don't do this `typedef char * String` because it's going to confuse the living daylights out of you and anyone who reads your code. Additionally, you need to allocate or provide some memory for scanf to put things into. – pvg Apr 30 '17 at 03:29

1 Answers1

2

username and password are just declared as char *, they are not allocated any memory, this will lead to segmentation fault, you need to call malloc to allocate memory to them, or declare them as character array

Pras
  • 4,047
  • 10
  • 20