Miscellaneous > Programming & Networking

A Small Question

(1/2) > >>

Aegwyn:
Hey, everyone.
I've been trying desperately to learn C, or at least some language. And I seem to have come up against a sort of barrier.... You'll laugh, when you see it, but I refuse go one step further before I make this darn thing work...


--- Code: ---
--- End code ---

Obviously, that's not ACTUALLY what I'm trying to do. Really.   . It's just a small program I made to isolate and study the problem I was having, with some success.

Now, I managed to make the loop work for buying things, which was more than my original, totally overdone code did. But, for some inexplicable reason, I cannot make the darn thing go back at  "default:". It just skips straight to the end.

So, can someone point out my almost certainly ridiculously obvious dilemma? Before it drives me totally insane?  

Service With A Smile ,
Aegwyn

Calum:
hey, i think i spotted the problem - you've got a little     in the middle of your code! take that out and it should all work fine!   ;)     :D     :D     :D

[ September 06, 2002: Message edited by: Calum ]

Bazoukas:
lolol calum


 Ill try to help you man. But i might not be able since i do C++. But i will take a look at it now.

Bazoukas:
i think i fixed it

 try it and tell me whats going on.

#include <stdio.h>

int continuer,smallchickenamount,mediumchickenamount,largechickenamount;
char shoppingcounter;

main()
{
int money;
money=200;
continuer=2;
largechickenamount=0;
mediumchickenamount=0;
smallchickenamount=0;
printf("Welcome to the Shopping Interface 2002.\n");


while( continuer )
{

printf("You have three choices the the Shopping interface\n");
printf("S: Buy a small chicken(50 Gold)\n");
printf("M: Buy a medium chicken(100 Gold)\n");
printf("L: Buy a large chicken(200 Gold)\n");
printf("Which do you want? S, M or L?\n");
printf("Right now you have %d S, %d M and %d L.\n",smallchickenamount,mediumchickenamount,largechickenamount);
scanf("%s",&shoppingcounter);

switch( shoppingcounter )
{
case 'S': printf("You bought a small chicken.\n");
if(money<50)

break;
else if(money>=50)
{
money=money-50;
smallchickenamount=smallchickenamount+1;
printf("Isnt that nice!\n");
continue;
}
break;
case 'M': printf("You bought a medium chicken.\n");
if(money<100)

break;
else if(money>=100)
{
money=money-100;
mediumchickenamount=mediumchickenamount+1;
printf("Isnt that nice!\n");
continue;
}
break;
case 'L': printf("You bought a large chicken.\n");
if(money<200)

break;
else if(money>=200)
{
money=money-200;
largechickenamount=largechickenamount+1;
printf("Isnt that nice!\n");
continue;

   }
break;
default:
   printf("Woops! Thats a screwup!\n");
continuer=2;
continue;

}

if(continuer==2)
{
continuer=1;
continue;
}
else if(continuer==1)
break;
}

printf("Ah well. You ran out of money. Too bad!\n");
printf("In the end, you had:\n");
printf("%d Small chickens\n",smallchickenamount);
printf("%d Medium chickens\n",mediumchickenamount);
printf("And %d Large chickens\n",largechickenamount);
}

voidmain:
You were just missing a "break" at the end of your "default":


--- Code: ---
--- End code ---

[ September 06, 2002: Message edited by: void main ]

Navigation

[0] Message Index

[#] Next page

Go to full version