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

The geek forum. PHP, Perl, HTML, hardware questions etc.. it's all in here. Got a techie question? We'll sort you out. Ask your questions or post a link to your own site here!

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

Postby mastersquirrel » Mon Jan 23, 2006 6:52 pm

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?
User avatar
mastersquirrel
 
Posts: 696
Joined: Thu Oct 07, 2004 10:55 am
Location: I saw a squirrel! ...... It was going like this!!!

Postby mastersquirrel » Mon Jan 23, 2006 8:44 pm

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?
User avatar
mastersquirrel
 
Posts: 696
Joined: Thu Oct 07, 2004 10:55 am
Location: I saw a squirrel! ...... It was going like this!!!

Postby Warrior4Christ » Mon Jan 23, 2006 10:50 pm

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?
Everywhere like such as, and MOES.

"Expect great things from God; attempt great things for God." - William Carey
User avatar
Warrior4Christ
 
Posts: 2045
Joined: Sat Aug 20, 2005 8:10 pm
Location: Carefully place an additional prawn on the barbecue

Postby Slater » Mon Jan 23, 2006 11:20 pm

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.
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia

Postby ClosetOtaku » Tue Jan 24, 2006 5:12 pm

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....
"If I find in myself a desire which no experience in this world can satisfy, the most probable explanation is that I was made for another world." -- C.S. Lewis
User avatar
ClosetOtaku
 
Posts: 927
Joined: Tue Jul 06, 2004 3:12 am
Location: Alexandria, VA

Postby Slater » Tue Jan 24, 2006 10:27 pm

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.
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia

Postby Kaligraphic » Wed Jan 25, 2006 12:29 am

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.
The cake used to be a lie like you, but then it took a portal to the deception core.
User avatar
Kaligraphic
 
Posts: 2002
Joined: Wed Jul 21, 2004 12:00 pm
Location: The catbox of DOOM!

Postby Slater » Wed Jan 25, 2006 3:35 am

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.
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia

Postby Mithrandir » Wed Jan 25, 2006 5:36 pm

Can you show some code? It's a bit tough to debug without....
User avatar
Mithrandir
 
Posts: 11071
Joined: Fri Jun 27, 2003 12:00 pm
Location: You will be baked. And then there will be cake.


Return to Computing and Links

Who is online

Users browsing this forum: No registered users and 147 guests