| | |
having problems with %c and %s
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2008
Posts: 2
Reputation:
Solved Threads: 0
so here's the code.
the problem is every time i change
with %c, it won't allow the user to enter a letter and skips to "Sorry, you have entered a wrong seat letter" and even if i don't change it to %c, it won't change the a[].seat to '*'.
T_T
and i'm not sure about the for loop, can someone help me?
help will be very much appreciated, thanks.
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> void main() {struct { char seat; }a[19]; int x,z; char y; a[0].seat='A'; a[1].seat='B'; a[2].seat='C'; a[3].seat='D'; a[4].seat='A'; a[5].seat='B'; a[6].seat='C'; a[7].seat='D'; a[8].seat='A'; a[9].seat='B'; a[10].seat='C'; a[11].seat='D'; a[12].seat='A'; a[13].seat='B'; a[14].seat='C'; a[15].seat='D'; a[16].seat='A'; a[17].seat='B'; a[18].seat='C'; a[19].seat='D'; clrscr(); printf("Flight Seat Reservation:"); printf("\n\n\n\t1\t%c\t%c\t%c\t%c", a[0].seat, a[1].seat, a[2].seat, a[3].seat); printf("\n\t2\t%c\t%c\t%c\t%c", a[4].seat, a[5].seat, a[6].seat, a[7].seat); printf("\n\t3\t%c\t%c\t%c\t%c", a[8].seat, a[9].seat, a[10].seat, a[11].seat); printf("\n\t4\t%c\t%c\t%c\t%c", a[12].seat, a[13].seat, a[14].seat, a[15].seat); printf("\n\t5\t%c\t%c\t%c\t%c", a[16].seat, a[17].seat, a[18].seat, a[19].seat); for(z=0;z<=20;++z) {printf("\n\nEnter your row number:\n"); scanf("%d", &x); if(x==1||x==2||x==3||x==4||x==5) {printf("\n\nEnter your seat letter:\n"); scanf("%s", &y); if(y!='a'&&y!='A'&&y!='b'&&y!='B'&&y!='c'&&y!='C'&&y!='d'&&y!='D') {printf("\n\nSorry, you have entered a wrong seat letter."); printf("\n\nPlease enter another seat letter: "); scanf("%s", &y);}} else {printf("\n\nSorry you have entered a wrong row number"); printf("\n\nPlease enter seat number again: \n"); scanf("%d", &x); printf("\n\nEnter your seat letter:\n"); scanf("%s", &y);}; if(x==1) {if(y=='a'||y=='A') a[0].seat='*'; else if(y=='b'||y=='B') a[1].seat='*'; else if(y=='c'||y=='C') a[2].seat='*'; else if(y=='d'||y=='D') a[3].seat='*';} else if(x==2) {if(y=='a'||y=='A') a[4].seat='*'; else if(y=='b'||y=='B') a[5].seat='*'; else if(y=='c'||y=='C') a[6].seat='*'; else if(y=='d'||y=='D') a[7].seat='*';} else if(x==3) {if(y=='a'||y=='A') a[8].seat='*'; else if(y=='b'||y=='B') a[9].seat='*'; else if(y=='c'||y=='C') a[10].seat='*'; else if(y=='d'||y=='D') a[11].seat='*';} else if(x==4) {if(y=='a'||y=='A') a[12].seat='*'; else if(y=='b'||y=='B') a[13].seat='*'; else if(y=='c'||y=='C') a[14].seat='*'; else if(y=='d'||y=='D') a[15].seat='*';} else if(x==5) {if(y=='a'||y=='A') a[16].seat='*'; else if(y=='b'||y=='B') a[17].seat='*'; else if(y=='c'||y=='C') a[18].seat='*'; else if(y=='d'||y=='D') a[19].seat='*';} printf("Flight Seat Reservation:"); printf("\n\n\n\t1\t%c\t%c\t%c\t%c", a[0].seat, a[1].seat, a[2].seat, a[3].seat); printf("\n\t2\t%c\t%c\t%c\t%c", a[4].seat, a[5].seat, a[6].seat, a[7].seat); printf("\n\t3\t%c\t%c\t%c\t%c", a[8].seat, a[9].seat, a[10].seat, a[11].seat); printf("\n\t4\t%c\t%c\t%c\t%c", a[12].seat, a[13].seat, a[14].seat, a[15].seat); printf("\n\t5\t%c\t%c\t%c\t%c", a[16].seat, a[17].seat, a[18].seat, a[19].seat);} getch(); }
the problem is every time i change
if(y!='a'&&y!='A'&&y!='b'&&y!='B'&&y!='c'&&y!='C'&&y!='d'&&y!='D')
{printf("\n\nSorry, you have entered a wrong seat letter.");
printf("\n\nPlease enter another seat letter: ");
scanf("%s", &y);}}T_T
and i'm not sure about the for loop, can someone help me?
help will be very much appreciated, thanks.
one way without much reading you -
try sscanf and parse.
http://www.neuron.yale.edu/neuron/do...on/sscanf.html
try sscanf and parse.
http://www.neuron.yale.edu/neuron/do...on/sscanf.html
It would take a post several pages long to detail all the problems and inefficiencies in this code. I don't know where to start. It is a mess. A real mess.
But the good news is that at least you had an attempt at writing some code.
But here are a few pointers (unintentional bad 'C' pun there).
1) You don't need a struct if you only have 1 member of the struct.
2) Use toupper to replace all those ungainly boolean expressions.
Replace
if (y=='a'||y=='A')
with
y= toupper (y)
if (y=='A')
3) Use case statements rather than if then else, as appropriate.
4) Don't use x,y,z,a,b, as variable names. Use meaningful names, like Row and SeatLetter.
5) If your seats are arranged in a grid, use a two dimensional array to model them.
6) Use a for loop to initialize the values. You have to imagine how your code would work if you had 900 seats instead of your current 20 seats.
7) Replace that massive if then else block with one line of code that directly sets the value, like
Seats[x][y-'A'] = '*';
But the good news is that at least you had an attempt at writing some code.
But here are a few pointers (unintentional bad 'C' pun there).
1) You don't need a struct if you only have 1 member of the struct.
2) Use toupper to replace all those ungainly boolean expressions.
Replace
if (y=='a'||y=='A')
with
y= toupper (y)
if (y=='A')
3) Use case statements rather than if then else, as appropriate.
4) Don't use x,y,z,a,b, as variable names. Use meaningful names, like Row and SeatLetter.
5) If your seats are arranged in a grid, use a two dimensional array to model them.
6) Use a for loop to initialize the values. You have to imagine how your code would work if you had 900 seats instead of your current 20 seats.
7) Replace that massive if then else block with one line of code that directly sets the value, like
Seats[x][y-'A'] = '*';
![]() |
Similar Threads
- If i use an illegal copy of windows,will it cause problems? (Windows NT / 2000 / XP)
- Internet explorer problems (Web Browsers)
- Problems Downloading Windows XP (Windows NT / 2000 / XP)
- Connection Problems (Networking Hardware Configuration)
- Pop up ads (Web Browsers)
- No time... bitch load of problems (Windows NT / 2000 / XP)
Other Threads in the C Forum
- Previous Thread: precedence of logican and and or operator
- Next Thread: lvalue required why
| Thread Tools | Search this Thread |
#include * adobe ansi append array arrays asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv research reversing scripting segmentationfault sequential single socket socketprograming standard string systemcall testing threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi





