If-else-then problem
| ponki d monkey (23) | |||
| Hi! I'm a c++ student. I use Borland 5.02 as my compiler. I have a problem with my assignment. Our teacher asked us to program something like an interface of an Automated Teller Machine (ATM). What I want to happen is for the program to ask for a confirmation to continue or exit if the PIN that the user enters does not match. When the the confirmation message appears, the program should do 3 things: 1. If the user enters 'y' or 'Y' (YES), the program will loop back at the beginning so he/she can enter the correct PIN; 2. If the user enters 'n' or 'N' (NO), the program should exit; 3. If the user enters a letter other than those 2 choices, the program will display an 'Invalid' message. The If-else-then statement that I have written doesn't seem to test the 'ans' variable when I try it with the letter 'n'. It goes directly to the next line instead of exiting the program. Can anyone help me with this? I already tried so many different things just to fix this, but I just can't. Here's the code and thanks in advance: #include <iostream.h> #include <conio.h> #include <stdio.h> //#include <string.h> #include <ctype.h> #include <stdlib.h> //------------------------------------------------------------------------------ //Function Protocols void form(int,int,int,int,int,int); void frame(int,int,int,int,int,int); void validateMenu(void); void mainMenu1(int); //------------------------------------------------------------------------------ //Main block void main(void) { char aname[15],pinbase1[5],pinbase2[32],pin1[5],ans,choice1[1]; float acctbal; int j,i,choice2,val; do{ do{ do{ clrscr(); ans=0; validateMenu(); gotoxy(39,7); gets(aname); gotoxy(39,10); for(i=0;i<=4;i++) { pin1[i]=getch(); if(pin1[i]=='\b') { if(i<1) { cout<<"\b"; cout<<" "; i=-1; } else { cout<<"\b"; cout<<" "; cout<<"\b"; i=i-2; } } else if(pin1[i]==13) { break; } else { cout<<"*"; pinbase1[i]=pin1[i]; pinbase1[i+1]=0; } } j=0; for(i=0;i<=4;i++) { if(!isdigit(pinbase1[i])) { j++; } } }while(j>1 || strlen(pinbase1)>4); gotoxy(39,13); for(i=0;i<=4;i++) { pin1[i]=getch(); if(pin1[i]=='\b') { if(i<1) { cout<<"\b"; cout<<" "; i=-1; } else { cout<<"\b"; cout<<" "; cout<<"\b"; i=i-2; } } else if(pin1[i]==13) { break; } else { cout<<"*"; pinbase2[i]=pin1[i]; pinbase2[i+1]=0; } } j=0; for(i=0;i<=4;i++) { if(!isdigit(pinbase2[i])) { j++; } } }while(j>1 || strlen(pinbase2)>4); val=strcmp(pinbase1,pinbase2); //------------------------------------------------------------------------------ //This is where the problem is... if(val!=0) { do{ form(5,19,75,21,YELLOW,RED); gotoxy(6,20); cprintf("Invalid PIN and confirmation. Would you like to try again? [y/n] "); frame(5,19,75,21,YELLOW,RED); textbackground(BLACK); gotoxy(71,20); cprintf(" "); gotoxy(72,20); cin>>ans; if(ans!='y' || ans!='Y') { if(ans=='n' || ans=='N') { clrscr(); gotoxy(1,1); cprintf("GOODBYE!"); cout<<"\n\n\n"<<ans; } else { gotoxy(24,23); cprintf("Invalid entry! Please try again."); cout<<ans; } } }while(ans!='n' && ans!='N' && ans!='y' && ans!='Y'); } }while(ans=='y' || ans=='Y'); clrscr(); cout<<"\n\n\nTHERE SHOULD BE ANOTHER SET OF CODES HERE, BUT IT'S TOO LONG THAT'S WHY I DIDN'T INCLUDE."; //Until here //------------------------------------------------------------------------------ getch(); } //------------------------------------------------------------------------------ //FUNCTIONS void form(int x1,int y1,int x2,int y2,int forecolor,int backcolor) { textcolor(forecolor); textbackground(backcolor); for(int k=y1;k<=y2;k++) { for(int h=x1;h<=x2;h++) { gotoxy(h,k);cprintf(" "); } } } void frame(int x1,int y1,int x2,int y2,int color,int bgcolor) { textcolor(color); textbackground(bgcolor); for(int x = x1+1; x < x2; x++) { gotoxy(x,y1); cprintf("Ä"); //top horizontal line gotoxy(x,y2); cprintf("Ä"); //bottom horizontal line } for(int y = y1+1; y < y2; y++) { gotoxy(x1,y); cprintf("³"); //left vertical line gotoxy(x2,y); cprintf("³"); //right vertical line } gotoxy(x1,y1); cprintf("Ú"); //upper left corner gotoxy(x2,y1); cprintf("¿"); //upper right corner gotoxy(x1,y2); cprintf("À"); //lower left corner gotoxy(x2,y2); cprintf("Ù"); //lower right corner } void validateMenu(void) { form(22,3,58,16,WHITE,BLUE); gotoxy(24,7); cprintf("Account Name:"); gotoxy(24,10); cprintf("4-digit PIN:"); gotoxy(24,13); cprintf("Confirm PIN:"); form(34,4,47,4,WHITE,YELLOW); frame(22,4,58,16,YELLOW,BLUE); form(34,4,45,4,WHITE,YELLOW); gotoxy(35,4); cprintf("VALIDATION"); textbackground(BLACK); gotoxy(38,7); cprintf(" "); gotoxy(38,10); cprintf(" "); gotoxy(38,13); cprintf(" "); } void mainMenu1(int accountbalance) { if(accountbalance==0) { form(2,2,38,24,WHITE,BLUE); gotoxy(6,6); cprintf("[1] New Account"); gotoxy(6,16); cprintf("[6] EXIT"); gotoxy(6,22); cprintf("Enter choice:"); textcolor(8); textbackground(BLUE); gotoxy(6,8); cprintf("[2] Deposit"); gotoxy(6,10); cprintf("[3] Balace Inquiry"); gotoxy(6,12); cprintf("[4] Withdraw"); gotoxy(6,14); cprintf("[5] Close Account"); frame(2,3,38,19,YELLOW,BLUE); frame(2,20,38,24,YELLOW,BLUE); form(3,4,12,3,WHITE,YELLOW); gotoxy(4,3); cprintf("MAIN MENU"); textbackground(BLACK); gotoxy(20,22); cprintf(" "); } else { form(2,2,38,24,WHITE,BLUE); gotoxy(6,8); cprintf("[2] Deposit"); gotoxy(6,10); cprintf("[3] Balace Inquiry"); gotoxy(6,12); cprintf("[4] Withdraw"); gotoxy(6,14); cprintf("[5] Close Account"); gotoxy(6,16); cprintf("[6] EXIT"); gotoxy(6,22); cprintf("Enter choice:"); textcolor(8); textbackground(BLUE); gotoxy(6,6); cprintf("[1] New Account"); frame(2,3,38,19,YELLOW,BLUE); frame(2,20,38,24,YELLOW,BLUE); form(3,4,12,3,WHITE,YELLOW); gotoxy(4,3); cprintf("MAIN MENU"); textbackground(BLACK); gotoxy(20,22); cprintf(" "); } } | |||
| BlahBlah (7) | |||
| Tabbing would help. | |||
| firedraco (847) | |||
| Actually, tabbing isn't supported unless you use code tags (below, although I use 3 spaces instead of a tab). I would probably just use a while loop for the pin, something like:
| |||
| ponki d monkey (23) | |||
| Hi thanks for the response. I understand the approach that you did, firedraco. I think that'll work on my assignment. I will try that later. However, I don't know what tabbing and 'code tags' mean. Sorry, I'm not good with terminologies. Can you explain that to me? I really appreciate it... | |||
| Faldrax (322) | |||
| Code Tags are the way to get your sample code to format nicely in this forum. So if you type [code] int main() { //this does nothing:-) return 0; } [/code] it will appear as
which is much easier for everyone to read, and hence to understand and help fix:-) Tabbing refers to the indentation of the code - you can either use a single tab or 2-3 spaces to indent. Again, it all helps make it easy to read. In the example above I have used 3 spaces to indent the code inside the {}. Note that the formatter for the forum removes leading spaces unless you are within Code Tags. If you don't do so already, it is probably a good idea to format all your code this way as you write it as it helps highlight the structure of the code, making spotting erors and making changes simpler. | |||
| ponki d monkey (23) | |||
| Oh I see. Sorry, I wasn't aware of that. Next time I post, I'll do that. Anyway, I already fixed my assignment. Thanks to you all! | |||
This topic is archived - New replies not allowed.
