while - if - and switch

I am trying to write a very basic program that mirrors what an ATM does. It is a final project for my first programming class. First I tried all if statements but the = and the == were messing me up. i want to say:


/* ATM program */

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


int totalbalance = 1000; /* Initial balance */
int wamount;

int main ( void ) /* begin main function */

{



int answermain; /* holds answer value to main menu guestion */

system( "clear" );

printf( "\t*** What would you like to do? ***\n" );

printf( "\t*** WITHDRAW --> Press 1 ***\n" );
printf( "\t*** ***\n" );
printf( "\t*** DEPOSIT ---> Press 2 ***\n" );
printf( "\t*** ***\n" );
printf( "\t*** BALANCE ---> Press 3 ***\n" );
printf( "\t*** ***\n" );
printf( "\t*** QUIT ------> Press 4 ***\n" );

printf( "\t*** Make selection below and then press 'Enter' ***\n" );

printf( "\n\n\t Make selection here ----> ");
scanf( "%d", answermain);

if (answermain > 4 ) {

printf( "\n\n\t Invalid selection. Please try again\n" );
printf( "\n\n\t Make selection here ----> ");
scanf( "%d", &answermain);

}

else if (answermain < 1 ) {

printf( "\n\n\t Invalid selection. Please try again\n" );
printf( "\n\n\t Make selection here ----> ");
scanf( "%d", &answermain);
}


else if (answermain == 1){

system( "clear" );
printf( "\t****************************************************************\n" );
printf( "\t****************************************************************\n" );
printf( "\t*** ***\n" );
printf( "\t*** Withdraw Transaction ***\n" );
printf( "\t*** ***\n" );
printf( "\t****************************************************************\n" );
printf( "\t****************************************************************\n\n\n\n" );
printf( "\tPlease enter the amount you wish to withdraw: " );
scanf( "%d", &wamount);

if ( wamount < 0 ) {
printf( "\n\n\tInvalid selection. Please try again\n" );
printf( "\n\n\tPlease enter the amount you wish to withdraw ----> ");

}

else if ( wamount > totalbalance ) {


printf( "\n\n\tThat amount exceeds your balance. Please try again\n" );
printf( "\n\n\tPlease enter the amount you wish to withdraw ----> ");
}

}

if( answermain == 4 ) {

system( "clear" );
printf( "\t****************************************************************************\n" );
printf( "\t****************************************************************************\n" );
printf( "\t*** ***\n" );
printf( "\t*** ***\n" );
printf( "\t*** ***\n" );
printf( "\t*** ***\n" );
printf( "\t*** ***\n" );
printf( "\t*** Thank you for visiting Robinson Financial! ***\n" );
printf( "\t*** ***\n" );
printf( "\t*** ***\n" );
printf( "\t*** ***\n" );
printf( "\t*** ***\n" );
printf( "\t*** ***\n" );
printf( "\t****************************************************************************\n" );
printf( "\t****************************************************************************\n\n\n\n\n\n" );
}


return 0;
}


I can't figure out how to make it start over or why the if statements are not working. I'm not looking for someone to do my assignment just some gguidance. I'm sure I'm missing something small!! I tried doing it in funcions first, but that was a disaster. Feel free to email me mark_rob25@yahoo.com

Mark
MADE SOME PROGRESS!!!! I got the program to work a little better but it only loops each section once. Like if I enter -5 (which meets the condition on less then 0) it will say that was invalid try again. but if you enter it again it moves through to the next piece of code. ANY HELP???


/* ATM program */
#include <stdio.h>
#include <stdlib.h>

int main ( void ) /* begin main function */
{
int answermain; /* holds answer value to main menu guestion */
int totalbalance = 1000; /* intitall balance */
int wamount;

system( "clear" );
printf( "\t******************************************************************************\n" );
printf( "\t*** Welcome to Robinson Financial ***\n" );
printf( "\t*** What would you like to do? ***\n" );
printf( "\t*** WITHDRAW --> Press 1 ***\n" );
printf( "\t*** DEPOSIT ---> Press 2 ***\n" );
printf( "\t*** BALANCE ---> Press 3 ***\n" );
printf( "\t*** QUIT ------> Press 4 ***\n" ); ***\n" );
printf( "\t*** Make selection below and then press 'Enter' ***\n" );
printf( "\t******************************************************************************\n" );

printf( "\n\n\t Make selection here ----> ");
scanf( "%d", &answermain);

if (answermain >=5 ) {
printf( "\n\n\t Invalid selection. Please try again\n" );
printf( "\n\n\t Make selection here ----> ");
scanf( "%d", &answermain);
}

if (answermain <= 0 ) {
printf( "\n\n\t Invalid selection. Please try again\n" );
printf( "\n\n\t Make selection here ----> ");
scanf( "%d", &answermain);
}

if (answermain == 1) {
system( "clear" );
printf( "\t****************************************************************\n" );
printf( "\t*** Withdraw Transaction ***\n" );
printf( "\t****************************************************************\n\n\n\n" );
printf( "\tPlease enter the amount you wish to withdraw: " );
scanf( "%d", &wamount);

if ( wamount < 0 ) {
printf( "\n\n\tInvalid selection. Please try again\n" );
printf( "\n\n\tPlease enter the amount you wish to withdraw ----> ");
scanf( "%d", &wamount);
}

if ( wamount > totalbalance ) {
printf( "\n\n\tThat amount exceeds your balance. Please try again\n" );
printf( "\n\n\tPlease enter the amount you wish to withdraw ----> ");
scanf( "%d", &wamount);
}

/* else if ( wamount > 0 ) {
totalbalance = totalbalance - wamount */
/* printf( "Transaction complete. Your new balance is: $%d\n", &totalbalance); */
}


if( answermain == 2 ) {

system( "clear" );
printf( "\t***************************************************************\n" );
printf( "\t*** Deposit Transaction ***\n" );
printf( "\t***************************************************************\n\n\n\n" );
printf( "\tPlease enter the amount you wish to deposit: " );
scanf( "%d", &wamount);

if ( wamount < 0 ) {
printf( "\n\n\tInvalid selection. Please try again\n" );
scanf( "%d", &wamount);
}


if ( wamount > 10000 ) {
printf( "\n\n\tThat amount exceeds the maximum deposit. Please try again\n" );
printf( "\n\n\tPlease enter the amount you wish to withdraw ----> ");
scanf( "%d", &wamount);
}
}

if (answermain == 4) {

system( "clear" );
printf( "\t****************************************************************************\n" );
printf( "\t*** Thank you for visiting Robinson Financial! ***\n" );
printf( "\t****************************************************************************\n\n\n\n\n\n" );
}


return 0;
}
It would make your code quite a bit more readable if you wrapped it in code tags. As for looping, you could make a large while loop surrounding the code you want to repeat, and only exit the loop if you select exit.
Topic archived. No new replies allowed.