the code should be\" C program language \" anagement sytem Preieeti Bank M Due:
ID: 3811617 • Letter: T
Question
the code should be" C program language "
Explanation / Answer
Code:
#include <stdio.h>
#include <string.h>
//structure for users
struct userType
{
char username[20];
char password[20];
double balance;
}users[10];
//function to create new user
int createUser(int count, struct userType users[])
{
int check = 0;
char username[20], password[20];
while (check == 0)
{
printf("Enter username (max 20 chars): ");
scanf("%s",&username);
if (count == 0)
check = 1;
else
{
for (int i = 0; i < count; i++)
{
if(strcmp(users[i].username, username) == 0)
{
printf("Username already exists. ");
check = 0;
break;
}
else
check = 1;
}
}
}
check = 0;
while (check == 0)
{
printf("Enter password (min 6, max 20 chars): ");
scanf("%s",&password);
if (strlen(password) < 6)
printf("Password should be min 6 chars ");
else
check = 1;
}
strcpy(users[count].username, username);
strcpy(users[count].password, password);
users[count].balance = 0.0;
count++;
return count;
}
//function to change password
void changePassword(int count, struct userType users[])
{
if(count == 0)
{
printf("No users available ");
return;
}
char username[20],oldpassword[20], newpassword[20];
int match = 0, usernum = -1;
printf("Please enter your username: ");
scanf("%s",&username);
for(int i = 0; i < count; i++)
{
if(strcmp(users[i].username, username) == 0)
{
match = 1;
usernum = i;
break;
}
}
if (match == 0)
{
printf("Username not found ");
return;
}
printf("Please enter old password: ");
scanf("%s",&oldpassword);
printf("Please enter new password: ");
scanf("%s",&newpassword);
if(strcmp(users[usernum].password, oldpassword) == 0)
{
strcpy(users[usernum].password, newpassword);
printf("Password changed successfully ");
}
else
printf("Old password incorrect ");
}
//function to deposit money
void deposit(int count, struct userType users[])
{
if(count == 0)
{
printf("No users available ");
return;
}
char username[20],password[20];
int match = 0, usernum = -1;
double deposit = -1.0;
printf("Please enter your username: ");
scanf("%s",&username);
for(int i = 0; i < count; i++)
{
if(strcmp(users[i].username, username) == 0)
{
match = 1;
usernum = i;
break;
}
}
if (match == 0)
{
printf("Username not found ");
return;
}
printf("Please enter your password: ");
scanf("%s",&password);
if(strcmp(users[usernum].password, password) == 0)
{
printf("Enter amount to be deposited: ");
scanf("%lf",&deposit);
if(deposit > 0.0)
{
users[usernum].balance += deposit;
printf("New Balance is %lf ",users[usernum].balance);
}
else
printf("Incorrect value ");
}
else
printf("Invalid password ");
}
//function to withdraw money
void withdraw(int count, struct userType users[])
{
if(count == 0)
{
printf("No users available ");
return;
}
char username[20],password[20];
int match = 0, usernum = -1;
double withdraw = -1.0;
printf("Please enter your username: ");
scanf("%s",&username);
for(int i = 0; i < count; i++)
{
if(strcmp(users[i].username, username) == 0)
{
match = 1;
usernum = i;
break;
}
}
if (match == 0)
{
printf("Username not found ");
return;
}
printf("Please enter your password: ");
scanf("%s",&password);
if(strcmp(users[usernum].password, password) == 0)
{
printf("Enter amount to be withdrawn: ");
scanf("%lf",&withdraw);
if(withdraw > 0.0 && users[usernum].balance > withdraw)
{
users[usernum].balance -= withdraw;
printf("Amount withdrawn: %lf New Balance is %lf ",withdraw,users[usernum].balance);
}
else if(withdraw > 0.0 && users[usernum].balance < withdraw)
{
printf("Insufficient funds ");
}
else
printf("Incorrect value ");
}
else
printf("Invalid password ");
}
int main()
{
int option, menu = 1, userCount = 0;
//Infinite menu till exit
while (menu == 1)
{
printf("1. Create a new account (username/password) ");
printf("2. Change the password ");
printf("3. Cash Deposit ");
printf("4. Cash Withdrawal ");
printf("5. Exit ");
scanf("%d",&option);
switch(option)
{
case 1:
if (userCount ==10) //checking for max user count
printf("Maximum user limit reached. ");
else
userCount = createUser(userCount, users);
break;
case 2:
changePassword(userCount, users);
break;
case 3:
deposit(userCount, users);
break;
case 4:
withdraw(userCount,users);
break;
case 5:
menu = 0;
break;
default:
printf("Invalid Option ");
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.