Click to See Complete Forum and Search --> : C program switch erratic behavior
doowle
07-01-2001, 12:46 AM
Hi, I am a C newbie taking a class in C. I can't get my teacher to respond, so I am hoping someone here can help me...I wrote a program that worked until I put the user options into a switch statement. Each switch option works, but very erratically now (they worked consistently before I put them into the switch statement). Sometimes it doesn't move all the way through the case, somtimes it does, sometimes only halfway through the case. It's bizarre, and I can't find the pattern to the behavior. If you could take a look at the source code and make suggestions I'd appreciate it. I posted it at http://www.petvault.com/C_sourcecode.htm
Thank you!
zskillz
07-01-2001, 01:20 AM
well, i didn't really examine it to closely, but I usually put a "default" into the switch statement so that it knows what to do if none of the right keys are pressed. this may be you problem
also, there's a chance that you included something in a specific case that is actually intended to be performed on all 3. this could lead to what seems to be somewhat erratic behavior.
now I don't know C at all. I know java. however, I would suggest that you take more time to examine exactly what this supposed erratic behavior is. There must be some problem with the logic of the proggie, because the program will do exactly what it is told. it cannot handle ambiguity.
I hope this helps!
-Z
eagle1
07-01-2001, 10:19 AM
I don't know much about C ( I have concentrated on C++ so some commands I don't even recognize) but I usually (as Z mentioned) put a default parameter in the switch case. Try it and see if it solves your problem.!
doowle
07-01-2001, 10:23 AM
Actually, I had a default in there but I took it out thinking maybe it was flipping to the default when I wasn't intending it to, or it was contributing to the problem in some other way. I just put it back in. It does make the problem slightly worse (switch menu pops up slightly more often instead of answer to switch command inputted). I know there has to be a problem in the switch logic, but I have been beating my head on this thing for three days now. I am sure it must be obvious, but maybe I have looked at it for so long I just can't see it. (I have taken a course in both Java and C by the way, and they seemed to be incredibly similar.)
When it hits a switch command, it will process the command (for instance, display inputs) maybe once-or maybe 5 times-and not process it a dozen times in the same run. If I choose 'a' for instance, I can type [ 'a' and hit return ] 15 times in a row-and it will display the inputs maybe two or three times. Sometimes all three times in a row, sometimes with ignored commands in between. Same behavior on b and c as well. I am very frustrated, I am used to programs that consistently have the same problem so I can step through and see what would cause that exact behavior. This type of thing I have never encounted before.
I did discover one thing....if I enter the command twice before hitting enter (aa or bb instead of a or b) the command seems to work...after first displaying the menu (?!)
I have now also posted a script of program behavior for viewing.
[This message has been edited by doowle (edited 07-01-2001).]
[This message has been edited by doowle (edited 07-01-2001).]
dragonB
07-01-2001, 04:52 PM
hi, just glanced at it quickly, and knowing C++, but mostly Java(as seems to be the majority here).
Here's 2 quick ideas that you might try...
1.
while ((c=getchar())!=EOF&&c!='q')
{
*** getchar();
switch(c)
What's the second getchar() for? You've already got the char in c, what does the second getchar do?(does it get the return, maybe?)
2.
Don't remember the syntax, but maybe you need to bracket what's enclosed in the case statement. like so...
{
case 'a':
{
i=0;
while (i < x.n)
{printf("%lf ", *(ptr-
>p+i));
i++;
}
printf("\n");
getchar();
menu_sub (menu);
break;
}
just a few ideas.. maybe I'm way off base, but you can't blame a guy for trying. http://www.sysopt.com/forum/smile.gif
gd luck, dragonB
doowle
07-01-2001, 07:28 PM
You are a genius ! It was indeed my brackets! It works perfectly now! Oh, I can't tell you how happy you made me...I have been trying to figure out the problem for days now, I was beyond frustrated.
Thank you so much.
You are all very kind, trying to help a student stuck on a problem like this. I really appreciate it!
dragonB
07-01-2001, 11:20 PM
http://www.sysopt.com/forum/smile.gif
I'm so glad that I could help. Makes me happy too. Mainly since I bet any of us that are/were CS majors have been in the same boat!
Ahh, the long nights in the computer lab...
dragonB
eagle1
07-02-2001, 05:59 AM
Hey dragon.!! I'm majoring in CS ( **** those freaking math classes) too (changed from engineering department).!! I have spent so many hours in the lab with simple problems (like this one) and when you finally know what you did wrong, you feel so.... GOD-LIKE!!! hahaha
dragonB
07-02-2001, 10:42 PM
Hehe, I also started in Engineering. Computer Engineering, but the electrical stuff didn't agree with me. http://www.sysopt.com/forum/smile.gif So, I changed to CS. Although, I enjoyed most of my Math classes, and ended up getting one of my minors in it.
dragonB
doowle
07-03-2001, 12:59 AM
You guys are getting me worried now...I am an IDS major, but added a CS minor because I liked my programming classes. I start my first CS math class in a few weeks... all my CS classes have been programming so far (I had already gotten as far as calculus for the IDS major).
Thanks again dragon, you saved my monitor from an untimely death (I was about ready to pitch my head straight into it in frustration).
I wonder if that's what I liked about my programming classes...that rush you get when it suddenly, finally WORKS.
dragonB
07-03-2001, 01:33 AM
Definitely feels good to finally have something work, and work right!
Anyway, sorry to scare you on the math. The hardest class for me was the matrix class. Not the movie, the grid thingys.. lol
But, I really enjoyed multi-dimensional Calculus..so, you never know.
dragonB
eagle1
07-03-2001, 09:25 PM
Well, I started in Computer Engineering too but when I took my first circuits class... no way--not for me!
CS sounds great (I'm really starting now -- since I switched) but math bothers me. I found very difficult Calculus II and Linear Algebra (mostly it was the professor!!) Everyone (except 2) flunked the class. The other classes have been easy but I still need to pass Linear Algebra and Abstract Algebra (they say is the worst!).
The programming classes I have taken have been easy but this next semester I start with Data Structures and Assembly.! I don't know if they are cool or not or if they are difficult but I think I will learn something...hopefully.!!!
zskillz
07-04-2001, 02:15 AM
haaha.. I've heard that abstract algebra sucks really bad. I'm taking some math class that deals with combinimetrics (or something like that) this semester to finish up my math minor...
-Z
SysOpt.com
Copyright Internet.com Inc. All Rights Reserved.