+ Reply to Thread
Results 1 to 13 of 13
  1. #1
    Member
    Join Date
    May 2001
    Location
    San Diego, CA, USA
    Posts
    92

    C program switch erratic behavior

    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!

  2. #2
    Senior Member
    Join Date
    May 1999
    Location
    Denver, CO, USA
    Posts
    775
    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

  3. #3
    Ultimate Member eagle1's Avatar
    Join Date
    Mar 2000
    Location
    Guayama,P.R. , Puerto Rico
    Posts
    1,592
    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.!

  4. #4
    Member
    Join Date
    May 2001
    Location
    San Diego, CA, USA
    Posts
    92
    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).]

  5. #5
    Member
    Join Date
    Apr 2001
    Posts
    187
    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.
    gd luck, dragonB

  6. #6
    Member
    Join Date
    May 2001
    Location
    San Diego, CA, USA
    Posts
    92
    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!

  7. #7
    Member
    Join Date
    Apr 2001
    Posts
    187

    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

  8. #8
    Ultimate Member eagle1's Avatar
    Join Date
    Mar 2000
    Location
    Guayama,P.R. , Puerto Rico
    Posts
    1,592
    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


  9. #9
    Member
    Join Date
    Apr 2001
    Posts
    187
    Hehe, I also started in Engineering. Computer Engineering, but the electrical stuff didn't agree with me. So, I changed to CS. Although, I enjoyed most of my Math classes, and ended up getting one of my minors in it.
    dragonB

  10. #10
    Member
    Join Date
    May 2001
    Location
    San Diego, CA, USA
    Posts
    92
    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.

  11. #11
    Member
    Join Date
    Apr 2001
    Posts
    187
    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

  12. #12
    Ultimate Member eagle1's Avatar
    Join Date
    Mar 2000
    Location
    Guayama,P.R. , Puerto Rico
    Posts
    1,592
    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.!!!

  13. #13
    Senior Member
    Join Date
    May 1999
    Location
    Denver, CO, USA
    Posts
    775
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts







New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke