user3739485
user3739485

Reputation: 1

How to make a program keep asking user to enter Value in C programming

I know that I have to use the while loop, however I did try to put the while condition as while(choice =! 'e') {program statement} v....v but it doesn't work. I just new in C and this is my 1st Assignment, My code works as normal but I just want to make it perfect like a real ATM machine. So basically the assignment asked student to create a ATM machine v...v So what I want is after the user go through 1 of 5 selection, the program will ask them like at the beginning to to other transaction until they press 'Exit' option as 'e'. Thank in advance.

Upvotes: 0

Views: 7215

Answers (4)

Will
Will

Reputation: 921

I don't know if it is a typo, but the "not equal" operator is !=

You wrote

while(choice =! 'e') 

instead of

while(choice != 'e')

In the 1st case, you are negating(!) 'e' (what results in 0), and then assigning(=) the result to choice. I recommend you to enable all compiler warnings to detect these situations.

Upvotes: 0

Rukshan Perera
Rukshan Perera

Reputation: 147

Put your whole program inside a while loop have a reality of a ATM machine

while(1){ \\your program }

for your menu create a separate function and use it inside a switch statement

normally when you know all the choices in advance it is more
convenient to use a switch instead of a if-else-if

char menu(){
     printf(" INSERT YOUR ATM CARD : ");
     printf("\n ***********************************");
     printf("\n *           MENU                  *");
     printf("\n *     a. Deposit                  *");
     printf("\n *     b. Withdraw                 *");
     printf("\n *     c. Interest                 *");
     printf("\n *     d. Check Account Balance    *");
     printf("\n *     e. Exit                     *");
     printf("\n ***********************************");
     printf("\n\n");
     printf("\nEnter your choice: ");
     scanf("%c", &choice);
     return c;
 }

Then your program would be like

int main(){

    while (1)
    {
           switch(menu){
             //check for cases
              case 'x':
              case 'X':
                     //do relevant
                     break;
              ...............
              ..............
              case 'e':
              case 'E':
                     //EXIT the program ( not loop )
                     break;

              default:       
                  printf( "Invalid Input! please try again \n" );
                  break; //not necessary 
           }//end switch

    }//end while(super loop-from Embedded systems knowledge Atm machine is a one such)
}//end main

Upvotes: 1

In my opinion, you should use a do...while loop.

Why?

Because it would give you a chance to initialize your control variable (account) before testing it for valid values.

Your code would become this (I'm skipping some unrelated code):

int ChooseAccountType(int cheq, int sav)
{
    int account = 0;
    do {
        printf("Please Select Account\n");
        printf("*    1.Chequing     *\n");
        printf("*    2.Saving       *\n");
        printf("*********************\n");
        scanf("%d",&account);
    } while ( account != 1 || account != 2 );
    if ( account == 1 )
        return cheq;
    else if ( account == 2 )
        return sav;
    return 0;
}

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 753515

You say you tried:

while (choice =! 'e')

The trouble with that is that it is valid C, but it is not what you intended, which was:

while (choice != 'e')

As it stands, your loop assigned the value of !'e' (which is 0) to choice, and then test whether zero is not zero...and it isn't, so the loop is never entered. The way I wrote it doesn't modify choice and does compare to see if it is the same as 'e'.

Upvotes: 2

Related Questions