Some general thoughts.
1. Indentation matters.
In particular, it's not a good idea to mix spaces and tabs for indentation. Your editor might make a decent job, but at some point, some tool or forum will make it look like dog-food.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
#include<stdio.h>
int main(void)
{
char brand;
printf("a) Perodua\n");
printf("b) Proton\n");
printf("Enter you car brand\n");
scanf("%s", &brand);
switch (brand) {
case 'a':
char model;
printf("c) Kancil\n");
printf("d) Myvi\n");
printf("Enter your car model\n");
scanf("%c", model);
break;
switch (model) {
case 'c':
char type;
printf("g) Service\n");
printf("h) Parts replacement\n");
printf("Enter your type of service\n");
scanf("%c", type);
break;
switch (type) {
case 'g':
char service;
printf("o) Machine service\n");
printf("p) Traditional service\n");
printf("Enter your service");
scanf("%s", &service);
break;
switch (service) {
case 'o':
printf("RM100\n");
printf("Thank you for your patronage\n");
break;
case 'p':
printf("RM150\n");
printf("Thank you for your patronage\n");
break;
default:
printf("error 404\n program.exe not found");
break;
}
case 'h':
int part;
printf("w) Compressor + Cooling coil + expension valve = RM 260\n");
printf("x) Condensor + Receiver drier + Gas R134a = RM 180\n");
scanf("%d", &part);
break;
switch (part) {
case 'w':
printf("RM330\n");
printf("Thank you for your patronage\n");
break;
case 'x':
printf("RM350\n");
printf("Thank you for your patronage\n");
break;
default:
printf("error 404\n program.exe not found");
break;
}
}
case 'd':
char atype;
printf("i) Service\n");
printf("j) Parts replacement\n");
printf("Enter your type of service\n");
scanf("%c", &atype);
break;
switch (atype) {
case 'i':
char aservice;
printf("q) Machine service\n");
printf("r) Traditional service\n");
printf("Enter your service");
scanf("%s", &aservice);
break;
switch (aservice) {
case 'q':
printf("RM100\n");
printf("Thank you for your patronage\n");
break;
case 'r':
printf("RM150\n");
printf("Thank you for your patronage\n");
break;
default:
printf("error 404\n program.exe not found");
break;
}
case 'j':
int apart;
printf("1) Compressor + Cooling coil + expension valve= RM 260\n");
printf("2) Condensor + Receiver drier + Gas R134a = RM 180\n");
scanf("%d", &apart);
break;
switch (apart) {
case '1':
printf("RM330\n");
printf("Thank you for your patronage\n");
break;
case '2':
printf("RM350\n");
printf("Thank you for your patronage\n");
break;
default:
printf("error 404\n program.exe not found");
break;
}
}
}
case 'b':
char amodel;
printf("e) Saga\n");
printf("f) Saga BLM\n");
printf("Enter your car model\n");
scanf("%c", amodel);
break;
switch (amodel) {
case 'e':
char dtype;
printf("k) Service\n");
printf("l) Parts replacement\n");
printf("Enter your type of service\n");
scanf("%s", &dtype);
break;
switch (dtype) {
case 'k':
char dservice;
printf("s) Machine service\n");
printf("t) Traditional service\n");
printf("Enter your service");
scanf("%s", &dservice);
break;
switch (dservice) {
case 's':
printf("RM100\n");
printf("Thank you for your patronage\n");
break;
case 't':
printf("RM150\n");
printf("Thank you for your patronage\n");
break;
default:
printf("error 404\n program.exe not found");
break;
}
case 'l':
int dpart;
printf("3) Compressor + Cooling coil + expension valve = RM 260\n");
printf("4) Condensor + Receiver drier + Gas R134a = RM 180\n");
scanf("%d", &dpart);
break;
switch (dpart) {
case '3':
printf("RM330\n");
printf("Thank you for your patronage\n");
break;
case '4':
printf("RM350\n");
printf("Thank you for your patronage\n");
break;
default:
printf("error 404\n program.exe not found");
break;
}
break;
}
break;
case 'f':
char etype;
printf("m) Service\n");
printf("n) Parts replacement\n");
printf("Enter your type of service\n");
scanf("%s", &etype);
break;
switch (etype) {
case 'm':
char eservice;
printf("u) Machine service\n");
printf("v) Traditional service\n");
printf("Enter your service");
scanf("%s", &eservice);
break;
switch (eservice) {
case 'u':
printf("RM100\n");
printf("Thank you for your patronage\n");
break;
case 'v':
printf("RM150\n");
printf("Thank you for your patronage\n");
break;
default:
printf("error 404\n program.exe not found");
break;
}
case 'n':
int epart;
printf("5) Compressor + Cooling coil + expension valve = RM 260\n");
printf("6) Condensor + Receiver drier + Gas R134a = RM 180\n");
scanf("%d", &epart);
break;
switch (epart) {
case '5':
printf("RM330\n");
printf("Thank you for your patronage\n");
break;
case '6':
printf("RM350\n");
printf("Thank you for your patronage\n");
break;
default:
printf("error 404\n program.exe not found");
break;
}
}
}
}
return 0;
}
|
2. Your code desperately needs functions!
You have a single bloated main with 250 lines of code that doesn't work.
There is no reasonable way to fix that.
For example, your main should be something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
void doPerodua() {
}
void doProton() {
}
int main(void)
{
char brand;
printf("a) Perodua\n");
printf("b) Proton\n");
printf("Enter you car brand\n");
scanf(" %c", &brand);
switch (brand) {
case 'a':
doPerodua();
break;
case 'b':
doProton();
break;
}
}
|
Then each of doPerodua() and doProton() both have a single printf + scanf + switch to decide the outcome of your next menu choice. Those switches in turn call further sub-functions.
3. Nested switch/case are evil, because they tend to produce very long functions very quickly.
4. Pay very close attention to where you place your break statements. Large chunks of your code never get executed.
1 2 3 4 5 6 7 8
|
printf("g) Service\n");
printf("h) Parts replacement\n");
printf("Enter your type of service\n");
scanf("%c", type);
break;
//!! This NEVER happens!
switch (type) {
|
5. Neither of your approaches to read a single character work properly.
> scanf("%s", &brand);
This will attempt to write at least TWO characters to brand, and that will overwrite some memory you don't own.
> scanf("%c", model);
In addition to not using &model, the %c will store the next input character, which is typically going to be the \n from the end of the previous input.
Use this to read the next printable character.
scanf(" %c", &model); //!! note the space before %c