943,680 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 563
  • C RSS
Feb 4th, 2008
0

having problems with %c and %s

Expand Post »
so here's the code.
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. void main()
  5. {struct
  6. {
  7. char seat;
  8. }a[19];
  9. int x,z;
  10. char y;
  11. a[0].seat='A';
  12. a[1].seat='B';
  13. a[2].seat='C';
  14. a[3].seat='D';
  15. a[4].seat='A';
  16. a[5].seat='B';
  17. a[6].seat='C';
  18. a[7].seat='D';
  19. a[8].seat='A';
  20. a[9].seat='B';
  21. a[10].seat='C';
  22. a[11].seat='D';
  23. a[12].seat='A';
  24. a[13].seat='B';
  25. a[14].seat='C';
  26. a[15].seat='D';
  27. a[16].seat='A';
  28. a[17].seat='B';
  29. a[18].seat='C';
  30. a[19].seat='D';
  31. clrscr();
  32. printf("Flight Seat Reservation:");
  33. 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);
  34. printf("\n\t2\t%c\t%c\t%c\t%c", a[4].seat, a[5].seat, a[6].seat, a[7].seat);
  35. printf("\n\t3\t%c\t%c\t%c\t%c", a[8].seat, a[9].seat, a[10].seat, a[11].seat);
  36. printf("\n\t4\t%c\t%c\t%c\t%c", a[12].seat, a[13].seat, a[14].seat, a[15].seat);
  37. printf("\n\t5\t%c\t%c\t%c\t%c", a[16].seat, a[17].seat, a[18].seat, a[19].seat);
  38. for(z=0;z<=20;++z)
  39. {printf("\n\nEnter your row number:\n");
  40. scanf("%d", &x);
  41. if(x==1||x==2||x==3||x==4||x==5)
  42. {printf("\n\nEnter your seat letter:\n");
  43. scanf("%s", &y);
  44. if(y!='a'&&y!='A'&&y!='b'&&y!='B'&&y!='c'&&y!='C'&&y!='d'&&y!='D')
  45. {printf("\n\nSorry, you have entered a wrong seat letter.");
  46. printf("\n\nPlease enter another seat letter: ");
  47. scanf("%s", &y);}}
  48. else
  49. {printf("\n\nSorry you have entered a wrong row number");
  50. printf("\n\nPlease enter seat number again: \n");
  51. scanf("%d", &x);
  52. printf("\n\nEnter your seat letter:\n");
  53. scanf("%s", &y);};
  54. if(x==1)
  55. {if(y=='a'||y=='A')
  56. a[0].seat='*';
  57. else if(y=='b'||y=='B')
  58. a[1].seat='*';
  59. else if(y=='c'||y=='C')
  60. a[2].seat='*';
  61. else if(y=='d'||y=='D')
  62. a[3].seat='*';}
  63. else if(x==2)
  64. {if(y=='a'||y=='A')
  65. a[4].seat='*';
  66. else if(y=='b'||y=='B')
  67. a[5].seat='*';
  68. else if(y=='c'||y=='C')
  69. a[6].seat='*';
  70. else if(y=='d'||y=='D')
  71. a[7].seat='*';}
  72. else if(x==3)
  73. {if(y=='a'||y=='A')
  74. a[8].seat='*';
  75. else if(y=='b'||y=='B')
  76. a[9].seat='*';
  77. else if(y=='c'||y=='C')
  78. a[10].seat='*';
  79. else if(y=='d'||y=='D')
  80. a[11].seat='*';}
  81. else if(x==4)
  82. {if(y=='a'||y=='A')
  83. a[12].seat='*';
  84. else if(y=='b'||y=='B')
  85. a[13].seat='*';
  86. else if(y=='c'||y=='C')
  87. a[14].seat='*';
  88. else if(y=='d'||y=='D')
  89. a[15].seat='*';}
  90. else if(x==5)
  91. {if(y=='a'||y=='A')
  92. a[16].seat='*';
  93. else if(y=='b'||y=='B')
  94. a[17].seat='*';
  95. else if(y=='c'||y=='C')
  96. a[18].seat='*';
  97. else if(y=='d'||y=='D')
  98. a[19].seat='*';}
  99. printf("Flight Seat Reservation:");
  100. 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);
  101. printf("\n\t2\t%c\t%c\t%c\t%c", a[4].seat, a[5].seat, a[6].seat, a[7].seat);
  102. printf("\n\t3\t%c\t%c\t%c\t%c", a[8].seat, a[9].seat, a[10].seat, a[11].seat);
  103. printf("\n\t4\t%c\t%c\t%c\t%c", a[12].seat, a[13].seat, a[14].seat, a[15].seat);
  104. printf("\n\t5\t%c\t%c\t%c\t%c", a[16].seat, a[17].seat, a[18].seat, a[19].seat);}
  105. getch();
  106. }

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);}}
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ninaijanai is offline Offline
2 posts
since Feb 2008
Feb 4th, 2008
0

Re: having problems with %c and %s

one way without much reading you -
try sscanf and parse.

http://www.neuron.yale.edu/neuron/do...on/sscanf.html
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kapstav is offline Offline
7 posts
since Feb 2008
Feb 4th, 2008
0

Re: having problems with %c and %s

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'] = '*';
Reputation Points: 36
Solved Threads: 0
Light Poster
Passmark is offline Offline
32 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: precedence of logican and and or operator
Next Thread in C Forum Timeline: define variable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC