![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Noob Programmer having trouble with if and switch statements in programCurrently, I am working on a program for a beginning C Programming course. I understand the majority of the content and have debugged as much as I can but am now at a loss. We are assigned a project that is supposed to get a number from -99 to 99 then output its name. I compiled the program without any errors but it is doing what I want it to do. The coding is below and I do realize that I completely forgot to add the negative element, but I can do that after I post. Right now, we are limited to using if, else, and switch statements. So no loops.
CPP / C++ / C Code:
Last edited by cable_guy_67 : 17-Feb-2006 at 15:06.
Reason: Please enclose c code in [c] ... [/c] tags
|
|
#2
|
|||
|
|||
Re: Noob Programmer having trouble with if and switch statements in programQuote:
Your variables "tens" and "ones" are ints. SO: the case labels should be integet constants, not character literals CPP / C++ / C Code:
When you get the case stuff straightned out you might find other things to fix, but maybe this will get you unstuck at least temporarily. Regards, Dave |
|
#3
|
|||
|
|||
Re: Noob Programmer having trouble with if and switch statements in programQuote:
I did that except that now I am getting an error now. Also, how does the compiler know to go from the else statement to the two switch statements? I think I am just missing something simple or not connect everything together properly. |
|
#4
|
|||
|
|||
Re: Noob Programmer having trouble with if and switch statements in programPlease read the Guidelines so you can post better questions. And proofread your question. This post:
Quote:
Quote:
Concentrate on Guideline #1, #2, #3. __________________
Please read http://www.gidforums.com/t-5566.html. They were written to help you create a request that is readable and has enough information we can actually tell what you need help with. |
|
#5
|
|||
|
|||
Re: Noob Programmer having trouble with if and switch statements in programQuote:
If you would post your code and state exactly what error you are getting, maybe someone here can help. I know (believe it or not) how frustrating it is at first when nothing seems to work. Even the stuff that used to work a little bit isn't working at all, right? The code in your original post was missing a closing brace } at the end of the program. When I added that, the program compiled OK, but most of the inputs didn't give any output (for the reason that I stated in my previous response). I think you are pretty close except for the case labels. When I changed all of the case labels, I got somewhat better action. Quote:
Here is how the if/else stuff works. First example: if the condition is "true" (value of the expression is not zero), a single statement is executed --- the single statement immediately following the "if()". If the condition is "false" (value of the expression is zero), the single statement immediately following the "if()" is skipped. Either way, the program continues on after that. Code:
Second example: if the condition is "true", the single statement following the "if()" is executed. If not, the statement immediately following the "if()" is not executed, but the single statement immediately following the "else" is executed. Either way, the program continues on after that. Code:
Here's how you can have more than one statement after the "if" and/or "else": Just enclose a block of statements in curly braces {}, then the entire block is executed or not, depending on the value of the "condition" expression. Third example: A block of statements is executed or skipped, depending on the "condition". Either way, program execution continues on after that. Code:
Fourth example: Either the block of statements after "if()", or the block after the "else" is executed, depending on the value of the "condition". Either way, program execute continues on after the blocks. Code:
A lot of people (including me) usually use the curly braces even if there is only one statement after the "if" or "else". All of the statements in a block are usually indented to make the control structure easy to see by us humans. The compiler ignores indentation. Regards, Dave |
Recent GIDBlog
Observations of Iraq by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Is my program coded well? | dave88 | C Programming Language | 6 | 14-Oct-2005 09:18 |
| Linked Lists | DCOM | C Programming Language | 4 | 27-Mar-2005 00:00 |
| Re: Command Line Arguments, Part 1 | WaltP | C Programming Language | 0 | 10-Jul-2004 23:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The