this is done in Turbo C so conversion to C++ is not a problem anymore. As promised, this is just a partial solution to let bordogoy learn by example. This program accepts positive number from 1 - 99 only, if you provide a value which is lesser than 1 and greater than 99, it will ask for another number.s
here is my solution, no arrays, no function, no fuss!
Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAXSTR 80
void main(){
unsigned int input, ones, tens;
char strout[MAXSTR], strone[MAXSTR], strten[MAXSTR];
do{
clrscr();
printf("Enter a number from 1-99: ");
scanf("%u",&input);
}while((input<1)||(input>99));
/*
Get the TENS value and ONES value;
*/
tens = input / 10;
ones = input % 10;
/* or ones = input - (tens * 10); */
switch(tens){
case 1: strcpy(strten,"Ten ");
break;
case 2: strcpy(strten,"Twenty ");
break;
case 3: strcpy(strten,"Thirty ");
break;
case 4: strcpy(strten,"Forty ");
break;
case 5: strcpy(strten,"Fifty ");
break;
case 6: strcpy(strten,"Sixty ");
break;
case 7: strcpy(strten,"Seventy ");
break;
case 8: strcpy(strten,"Eighty ");
break;
case 9: strcpy(strten,"Ninety ");
break;
}
switch(ones) {
case 1: strcpy(strone,"One ");
break;
case 2: strcpy(strone,"Two ");
break;
case 3: strcpy(strone,"Three ");
break;
case 4: strcpy(strone,"Four ");
break;
case 5: strcpy(strone,"Five ");
break;
case 6: strcpy(strone,"Six ");
break;
case 7: strcpy(strone,"Seven ");
break;
case 8: strcpy(strone,"Eight ");
break;
case 9: strcpy(strone,"Nine ");
break;
}
strcpy(strout,strten);
strcat(strout,strone);
if(tens == 1){
switch(ones) {
case 1: strcpy(strout,"Eleven ");
break;
case 2: strcpy(strout,"Twelve ");
break;
case 3: strcpy(strout,"Thirteen ");
break;
case 4: strcpy(strout,"Fourteen ");
break;
case 5: strcpy(strout,"Fifteen ");
break;
case 6: strcpy(strout,"Sixteen ");
break;
case 7: strcpy(strout,"Seventeen ");
break;
case 8: strcpy(strout,"Eighteen ");
break;
case 9: strcpy(strout,"Nineteen ");
break;
}
}
printf("Value in String: %s\n",strout);
getch();
}
I got to hand it to silent-kill, his code is much short and efficient, but for first year students who just had a first encounter with C based programming language, mangamote jud intawn kay dili jud kasabot. Mag.unsa manang ang code nga nga short and concise sa person nga dili pa ka comprehend sa logic of the program. It will be to complex for them.
The best way to learn programming is to take it step by step, algorithm, flowcharting is the key. But if you have above average logic in programming you need not use algo/flowchart, just code it directly and debug as you go.
I hope the thread starter learned from this experience, through the help of those who replied to this thread.