Page 1 of 1

Programming C in Linux: Probably real simple problem...

PostPosted: Mon Jan 23, 2006 6:52 pm
by mastersquirrel
All I need to know is what clears whatever data is in a variable? In Windows I think it was 'null', but apparently it's not the same in Unix. Does anyone know what 'null' in Windows is in Unix or Linux?

PostPosted: Mon Jan 23, 2006 8:44 pm
by mastersquirrel
This is why I like programming in windows....

Alright look, I'm trying to make a menu program where a user can input data when they choose a certain menu option. However, once the loop starts over it just keeps looping and never stopping. It's very annoying cause I can't figure out what's causing it. Any ideas?

PostPosted: Mon Jan 23, 2006 10:50 pm
by Warrior4Christ
Check *very* carefully over the exit conditions and where they are supposed to be set true. Many times it can be a careless typing error that produces a valid program that doesn't do what you want.

As for the 'null' thing, it would be more dependent on the flavour of C you are using, rather than the OS. What compiler are you using?

PostPosted: Mon Jan 23, 2006 11:20 pm
by Slater
hmm... so you have an infinite loop that you can't figure out how to terminate? Show me the code... maybe I can see something in there.

PostPosted: Tue Jan 24, 2006 5:12 pm
by ClosetOtaku
How are you terminating the loop currently? Are you using a WHILE, DO-WHILE, or CASE? Have you included input validation criteria? A bunch of different things for starters....

PostPosted: Tue Jan 24, 2006 10:27 pm
by Slater
from the sound of things, it's a switch-case statement. If so...

1. don't forget your "break;" statements at the end of each case

2. don't forget to have a default case. This case is for if the program encounters a switch that you have not anticipated, or that is so normal that you don't feel that you need a case for it.

Here's an example of that switch from my earlier posted program:
Code: Select all
        cin >> accChooser;
       
        switch (accChooser)
        {
            case 1:
                accuracy = 3;
                break;
            case 2:
                accuracy = 5;
                break;
            case 3:
                accuracy = 15;
                break;
            case 4:
                accuracy = 30;
                break;
            case 5:
                accuracy = 90;
                break;
            case 6:
                accuracy = 1337;
                break;
            default:
                cout << "\n\n... Screw exception handling for today.\n"
                     << "Live for the Swarm!\n" << endl;
                     exit(1);
        }


not sure if you need a break statement at the end of your default, since that's where the code block ends. It certainly isn't needed here since the program recieves instruction to terminate the program.

Hope this was helpful.

PostPosted: Wed Jan 25, 2006 12:29 am
by Kaligraphic
You don't. A break statement is just used to prevent fall-through execution there. If you don't mind executing the contents of multiple cases, you don't technically need the break statements at all.

PostPosted: Wed Jan 25, 2006 3:35 am
by Slater
this is true, but for most cases that I can think of, the programmer will want to make sure break statements are there. Most of the time, that's the case with menus.

PostPosted: Wed Jan 25, 2006 5:36 pm
by Mithrandir
Can you show some code? It's a bit tough to debug without....