GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Feb-2006, 14:52
Heresy Heresy is offline
New Member
 
Join Date: Feb 2006
Posts: 10
Heresy is on a distinguished road

Noob Programmer having trouble with if and switch statements in program


Currently, 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:
#include <stdio.h>

int
main(void)
{
	int number;
	int ones, tens;

	printf("Input an integer number between -99 and 99: ");
		scanf("%d", &number);

	tens = number / 10;
	ones = (number - tens * 10);

	if (number > 99 || number < -99)
		printf("Sorry, number not between -99 and 99.\n");
	if (number == 0)
		printf("Zero");
	if (number == 10)
		printf("Ten");
	if (number == 11)
		printf("Eleven");
	if (number == 12)
		printf("Tweslve");
	if (number == 13)
		printf("Thirteen");
	if (number == 14)
		printf("Fourteen");
	if (number == 15)
		printf("Fifteen");
	if (number == 16)
		printf("Sixteen");
	if (number == 17)
		printf("Seventeen");
	if (number == 18)
		printf("Eighteen");
	if (number == 19)
		printf("Nineteen");
	else

switch (tens)
{
            case '0':
	        	printf("");
				break;
            case '2':
                printf("Twenty");
                break;
            case '3':
                printf("Thirty");
                break;
            case '4':
                printf("Forty");
                break;
            case '5':
                printf("Fifty");
                break;
            case '6':
                printf("Sixty");
                break;
            case '7':
                printf("Seventy");
                break;
            case '8':
                printf("Eighty");
                break;
            case '9':
                printf("Ninety");
                break;
}
switch (ones)
{
            case '0':
                printf("");
                break;
            case '1':
                printf("One");
                break;
            case '2':
                printf("Two");
                break;
            case '3':
                printf("Three");
                break;
            case '4':
                printf("Four");
                break;
            case '5':
                printf("Five");
                break;
            case '6':
                printf("Six");
                break;
            case '7':
                printf("Seven");
                break;
            case '8':
                printf("Eight");
                break;
            case '9':
                printf("Nine");
                break;
}
Last edited by cable_guy_67 : 17-Feb-2006 at 15:06. Reason: Please enclose c code in [c] ... [/c] tags
  #2  
Old 17-Feb-2006, 15:56
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Noob Programmer having trouble with if and switch statements in program


Quote:
Originally Posted by Heresy
am now at a loss.

Your variables "tens" and "ones" are ints.

SO: the case labels should be integet constants, not character literals
CPP / C++ / C Code:
case 1: ....   /* not case '1': */

case 2: ....  /* not case '2': */

...

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  
Old 17-Feb-2006, 16:29
Heresy Heresy is offline
New Member
 
Join Date: Feb 2006
Posts: 10
Heresy is on a distinguished road

Re: Noob Programmer having trouble with if and switch statements in program


Quote:
Originally Posted by davekw7x
Your variables "tens" and "ones" are ints.

SO: the case labels should be integet constants, not character literals

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.

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  
Old 17-Feb-2006, 16:51
Guidelines Plz Guidelines Plz is offline
Junior Member
 
Join Date: Sep 2005
Posts: 87
Guidelines Plz is on a distinguished road

Re: Noob Programmer having trouble with if and switch statements in program


Please read the Guidelines so you can post better questions. And proofread your question. This post:
Quote:
Originally Posted by Heresy
I understand the majority of the content and have debugged as much as I can but am now at a loss... I compiled the program without any errors but it is doing what I want it to do....
says you have a problem, "but it is doing what I want it to do" meaning it's working. I realize this is a typo. But you wrote and ran the program, we didn't, so we don't know what it is doing! So what can we look for? Tell us...

Quote:
Originally Posted by Heresy
I did that except that now I am getting an error now.
And?

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  
Old 17-Feb-2006, 17:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Noob Programmer having trouble with if and switch statements in program


Quote:
Originally Posted by Heresy
I did that except that now I am getting an error now.

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:
Originally Posted by Heresy
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.

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:
if (condition) this single statement is executed if the arithmetic value of "condition" is not zero This is the rest of the program

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:
if (condition) this single statement is executed if the arithmetic value of "condition" is not zero else this single statement is executed if the arithmetic value of "condition" is zero This is the rest of the program



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:
if (condition) { statement .. .. All statements enclosed by the curly braces are executed if the value of the "condition" is not zero .. .. } This is the rest of the program

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:
if (condition) { .. these statements are executed if the arithmetic value of "condition" is not zero .. } else { .. .. these statements are executed if the arithmetic value of "condition" is zero .. .. } The rest of the program

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 GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 20:24.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.