I am a beginner in C, I am currently writing a small project(bank management system) that has no graphic.
So here is my code.
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
char name[500];
char country[499];
int day, mon, year;
long long csn;
char adress[500];
long long phone;
char type[500];
int action;
int after;
int createAcc()
{
printf("Full Name: ");
fgets(name, 500, stdin);
printf("Country: ");
fgets(country, 499, stdin);
printf("Adress: ");
fgets(adress, 500, stdin);
printf("Date of Birth \n");
printf("Day: ");
scanf_s("%d", &day);
printf("Month - Numbers only: ");
scanf_s("%d", &mon);
printf("Year: ");
scanf_s("%d", &year);
printf("Citizenship Number: ");
scanf_s("%d", &csn);
printf("Account type: ");
fgets(type, 500, stdin);
return 0;
}
int main()
{
printf(" BANK MANAGEMENT SYSTEM \n");
printf(" Welcome to the main menu. \n");
printf("1. Create an account - This will help you create a bank account; "
"It will ask for your Full Name, Country, Date of Birth, Citizenship Number, Adress, Phone Number, and finally chose your Account type.\n"
"2. Update information of existing account\n"
"3. Transactions - This will help you withdraw or deposit money from or to a bank account.\n"
"4. Remove an existing account\n"
"5. View account details\n"
"6. Exit - Terminates the program instantly.\n");
scanf_s("%d", &action);
if (action == 1)
{
printf("You have selected the first option: Create an account.\n");
printf("We will no begin creating your account.\n");
createAcc();
}
else if (action == 2)
{
printf("It appears you want to update an account.\n");
}
else if (action == 3)
{
printf("Chose one option\n");
printf("1. Deposit\n");
printf("2. Withdraw\n");
scanf_s("%d", &after);
if (after == 1)
{
printf("You have selected deposit.\n");
}
else
{
printf("You have selected withdraw.\n");
}
}
else if (action == 4)
{
printf("It appears you want to remove an account.");
}
else if (action == 5)
{
printf("It appears you want to view an existing account's information.");
}
else if (action == 6)
{
}
}
You can ignore everything after action == 2
. I expected my program to work like this:
Sample Input:
Full Name: Test Name
Country: Test Country
Adress: Test Adress 23
Date of Birth.
Day: 11
Month - Numbers only: 12
Year: 1999
Citizenship Number: 265
Account Type: Test Type
However, that is not what actually happens. This is what happens: When I input 1 to create an account, I get this output instead:
You have selected the first option: Create an account.
We will now begin creating your account.
Full Name: Country:
And if I keep going, when it reaches the account type line, it will simply ignore it and will not let me input that, and the program ends.
There is something unusual here. If I put the fgets above the printf line, the program will actually work.
int createAcc()
int createAcc()
{
fgets(name, 500, stdin);
printf("Full Name: ");
fgets(country, 499, stdin);
printf("Country: ");
fgets(adress, 500, stdin);
printf("Adress: ");
printf("Date of Birth \n");
printf("Day: ");
scanf_s("%d", &day);
printf("Month - Numbers only: ");
scanf_s("%d", &mon);
printf("Year: ");
scanf_s("%d", &year);
printf("Citizenship Number: ");
scanf_s("%d", &csn);
fgets(type, 500, stdin);
printf("Account type: ");
return 0;
}
But when it reaches the adress line, it simply skips it and goes directly to the date of birth. It also skips the account type after inputing the rest.
I have also tried using scanf_s. But the problem is that if I input a full name like Test Name
, the program bugs out.
int createAcc()
{
printf("Full Name: ");
scanf_s("%s", &name, 500);
printf("Country: ");
scanf_s("%s", &country, 499);
printf("Adress: ");
scanf_s("%s", &country, 500);
printf("Date of Birth \n");
printf("Day: ");
scanf_s("%d", &day);
printf("Month - Numbers only: ");
scanf_s("%d", &mon);
printf("Year: ");
scanf_s("%d", &year);
printf("Citizenship Number: ");
scanf_s("%d", &csn);
printf("Account type: ");
scanf_s("%s", &type, 500);
return 0;
}
Why does this happen?