0

My program is supposed to check credentials if the exist in the file created "MyDetails.txt", if yes, then allow the user to proceed else, terminate. However the program accepts any values passed to it and re-writes the existing data. I would like to find out what errors are in the code and if there is another alternative in solving it.(validating credentials within a file). Below is the code for the program.

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include <stdlib.h>
main()
{
FILE *Credentials;
char username[15];
char password[12];

//open file for writing
Credentials  =fopen("MyDetails.txt","w");
//check if file exists
if(Credentials == NULL)
{
    printf("File does not exist\n");
    return;
}
//New User
printf("Enter Username\n");
scanf("%s",&username);
fprintf(Credentials,"Username:%s\n",username);

printf("Enter Password\n");
scanf("%s",&password);
fprintf(Credentials,"Password:%s\n",password);

    if(username==username)
    {
    if(password=password)
    {
    printf("Login Successfull\n");  

    printf("\n");

    float result;
    int choice,num;
    printf("Press 1 to Exit\n");
    printf("Press 2 to Input Current Reading\n");
    printf("Press 3 to Calculate Bill\n");
    printf("Press 4 to Make Payment\n");
    choice = input();
    float current,rate,previous,units,monthly,bill,stand,paid;
    switch (choice){
    case 1:{
    exit(0);
    break;
    }

    case 2:{
    printf("Input Current Reading\n");
    scanf("%f",&current);

    }

    case 3:{
    printf("Enter Rate\n");
    scanf("%f",&rate);
    printf("Enter Previous Reading\n");
    scanf("%f",&previous);


    units=current-previous;   
    monthly=units*rate;                 
    bill=150+monthly;

    printf("\n Total Bill:%.3f\n",bill);


    }
    case 4:{
    printf ("------PAYMENT------\n");
    printf("\n");
    printf("Amount To Pay is %.3f",bill);
    printf("\n");
    printf("Enter Amount\n");
    scanf("%f",&paid);
    printf("Remaining Balance\n %.3f",bill-paid);
    break;
    }   
    }
    }
    else
    {
    printf("\n Wrong Password");
    }
    }
    else
    {
    printf("\n User Doesn't Exist'");
    }
    return 0;
    }
    int input()
    {
    int number;
    scanf("%d",&number);
    return (number);
    }

    void output(float number)
    {
    printf("%f",number);
    }           
Nickson
  • 15
  • 1
  • 5
  • Can you properly indent your code? – Cid Mar 03 '19 at 18:23
  • `if (username==username)` will always be true, you are comparing a variable against itself – Cid Mar 03 '19 at 18:24
  • 1
    `if (password=password)` is not a comparison, you are assigning the value of `password` inside `password` (`=` is assign, `==` is comparison) – Cid Mar 03 '19 at 18:25
  • 1
    There is so much errors in the code that it will be nearly impossible to answer without rewritting everything – Cid Mar 03 '19 at 18:28

1 Answers1

1

Firstly, you are comparing username against itself. Obviously that will always be a "true" result.

You should read the username from the file into one variable, then the user input into another variable, and compare those two different variables.

Secondly, you need to understand how to properly compare character arrays.

There are times that this isn't true, like comparing NaN values, but this isn't one of them!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055