Connecting Tech Pros Worldwide Forums | Help | Site Map

program that would accept a non-negative integer &

Newbie
 
Join Date: Aug 2008
Posts: 1
#1: Aug 31 '08
#include<string.h>
#include<conio.h>
#include<stdio.h>
char*getName(int num)
{
char word[25];
switch (num)
{
case 1:strcpy(word,"one");break;
case 2:strcpy(word,"two");break;
case 3:strcpy(word,"three");break;
case 4:strcpy(word,"four");break;
case 5:strcpy(word,"five");break;
case 6:strcpy(word,"six");break;
case 7:strcpy(word,"seven");break;
case 8:strcpy(word,"eight");break;
case 9:strcpy(word,"nine");break;
case 10:strcpy(word,"ten");break;
case 11:strcpy(word,"eleven");break;
.
.
.
.
.
.
case 65535:strcpy(word,"sixty five thousand five hundred thirty five");break;
}
return word;
}
void main()
{
int num=0;
clrscr();
scanf("%d",&num);
printf(getName(num));
getch();
}

this is my code.. my problem is how can i directly go to 65535 without following all of the numbers from 1-65535...

pliss help me ASAP...

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Aug 31 '08

re: program that would accept a non-negative integer &


That code doesn't compile: here are the environmental limits stated by the Standard:

Quote:

Originally Posted by Standard C


-- 127 nesting levels of blocks |

-- 63 nesting levels of conditional inclusion

-- 12 pointer, array, and function declarators (in any
combinations) modifying an arithmetic, structure,
union, or incomplete type in a declaration

-- 63 nesting levels of parenthesized declarators within a
full declarator

-- 63 nesting levels of parenthesized expressions within a
full expression

-- 63 significant initial characters in an internal
identifier or a macro name (each universal character
name or extended source character is considered a
single character)

-- 31 significant initial characters in an external
identifier (each universal character name specifying a
character short identifier of 0000FFFF or less is
considered 6 characters, each universal character name
specifying a character short identifier of 00010000 or
more is considered 10 characters, and each extended
source character is considered the same number of
characters as the corresponding universal character
name, if any)

-- 4095 external identifiers in one translation unit

-- 511 identifiers with block scope declared in one block

-- 4095 macro identifiers simultaneously defined in one
preprocessing translation unit

-- 127 parameters in one function definition

-- 127 arguments in one function call

-- 127 parameters in one macro definition

-- 127 arguments in one macro invocation

-- 4095 characters in a logical source line

-- 4095 characters in a character string literal or wide
string literal (after concatenation)

-- 65535 bytes in an object (in a hosted environment only)

-- 15 nesting levels for #included files

-- 1023 case labels for a switch statement (excluding
those for any nested switch statements)


-- 1023 members in a single structure or union

-- 1023 enumeration constants in a single enumeration

-- 63 levels of nested structure or union definitions in a
single struct-declaration-list

kind regards,

Jos
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 385
#3: Aug 31 '08

re: program that would accept a non-negative integer &


Quote:

Originally Posted by bordogoy

Expand|Select|Wrap|Line Numbers
  1. char*getName(int num)
  2. {
  3. char word[25];
  4. switch (num)
  5. {
  6. .....
  7. return word;
  8. }
  9.  

You should not return address of local variable in this way.

Quote:
this is my code.. my problem is how can i directly go to 65535 without following all of the numbers from 1-65535...
The answer to this question is "Just skip all case statements except 0..9 and 65535, leaving only 11 case statements". However if you need to solve the problem from subject and "find its w", whatever this "w" is, you need to analyze thousands, hundreds, numbers from 0 to 20 and from 20 to 99 (with hyphen and number from 0 to 9).
And if your int is 16 bit, use ling int or unsigned int.
Reply


Similar C / C++ bytes