0

I have been programming a C program for few days now, however I have met some problems that I am unable to solve.

My code is:

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>

int main()
{
   char user_type;
   printf("Welcome to XYZ Bank\n\n\n\n");
   printf("Hint: a for admin and c for customer\n");
   printf("Please input your account type (c/a):");
   scanf("%c",&user_type);
   system("cls");
   if (user_type=='c')
   {
       customer_login();
   }
   else if (user_type=='a')
   {
        admin_login();
   }
   else if (user_type!='c' || user_type!='a')
   {
       printf("Invalid selection is made, the program will restart");
       main();
   }
}

The problem that I faced is when the user_type is not 'a' or 'c', the program is supposed to restart the main(), however all it does is to continue printing invalid selection.

Is there some command that allows me to restart main()? or is there a better way to do things? I think that a do while loop can be applied but I am not sure about it.

I am aware that the problem lies in the 3rd statement of my else-if loop, it would be helpful if somebody gave me tips on where to start.

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
JakeDan
  • 9
  • 3
  • 4
    `(user_type!='c' || user_type!='a')` will **always** be true. If `user_type=='c'`, then it's true that `user_type!='a'`. If `user_type=='a'`, then it's true that `user_type!='c'`. Thus, one or the other branch of that OR will hold. – Charles Duffy Oct 18 '17 at 15:09
  • 1
    @xing oh gosh that just solved everything! Thanks! so sorry for asking such a simple question – JakeDan Oct 18 '17 at 15:13
  • @CharlesDuffy yeah it was else before however I changed it, sorry for asking you to solve a flawed logic question, thanks! – JakeDan Oct 18 '17 at 15:15
  • 1
    Your program does not contain a loop -- though it should -- and in particular, `else if` does not constitute or create one. It *does* call `main()` recursively, which is responsible for the repetition you see. That's permitted, but it's rarely wise. – John Bollinger Oct 18 '17 at 15:15
  • You could also try making `main` call another function which does the work. You shouldn't be calling `main` yourself – doctorlove Oct 18 '17 at 15:18
  • @xing in this case do I use the while or do/while in the if? – JakeDan Oct 18 '17 at 15:28

0 Answers0