Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,297 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,442 people online right now. Registration is fast and FREE... Join Now!




help with code/cutting it short

 
Reply to this topicStart new topic

help with code/cutting it short, i need help i have this long code i want to cut short

james_L
3 Sep, 2008 - 04:13 AM
Post #1

New D.I.C Head
*

Joined: 28 Aug, 2008
Posts: 13


My Contributions
any ideas? other ways of solving the problem cutting the code short...

CODE


#include<iostream>
using namespace std;

void main()
{
int j;
cout<<"Enter a number from 20 to 99:"<<endl;
cin>>j;
if(j>=20&&j<=29)
{
    cout<<"the given number is twenty";
    j=j-20;
if(j==1)
        cout<<" one\n";
if(j==2)
        cout<<" two\n";
if(j==3)
        cout<<" three\n";
if(j==4)
        cout<<" four\n";
if(j==5)
        cout<<" five\n";
if(j==6)
        cout<<" six\n";
if(j==7)
        cout<<" seven\n";
if(j==8)
        cout<<" eight\n";
if(j==9)
        cout<<" nine\n";
}
if(j>=30&&j<=39)
{
    cout<<"the given number is thirty";
    j=j-30;
if(j==1)
        cout<<" one\n";
if(j==2)
        cout<<" two\n";
if(j==3)
        cout<<" three\n";
if(j==4)
        cout<<" four\n";
if(j==5)
        cout<<" five\n";
if(j==6)
        cout<<" six\n";
if(j==7)
        cout<<" seven\n";
if(j==8)
        cout<<" eight\n";
if(j==9)
        cout<<" nine\n";
}
if(j>=40&&j<=49)
{
    cout<<"the given number is forty";
    j=j-40;
if(j==1)
        cout<<" one\n";
if(j==2)
        cout<<" two\n";
if(j==3)
        cout<<" three\n";
if(j==4)
        cout<<" four\n";
if(j==5)
        cout<<" five\n";
if(j==6)
        cout<<" six\n";
if(j==7)
        cout<<" seven\n";
if(j==8)
        cout<<" eight\n";
if(j==9)
        cout<<" nine\n";
}
if(j>=50&&j<=59)
{
    cout<<"the given number is fifty";
    j=j-50;
if(j==1)
        cout<<" one\n";
if(j==2)
        cout<<" two\n";
if(j==3)
        cout<<" three\n";
if(j==4)
        cout<<" four\n";
if(j==5)
        cout<<" five\n";
if(j==6)
        cout<<" six\n";
if(j==7)
        cout<<" seven\n";
if(j==8)
        cout<<" eight\n";
if(j==9)
        cout<<" nine\n";
}
if(j>=60&&j<=69)
{
    cout<<"the given number is sixty";
    j=j-60;
if(j==1)
        cout<<" one\n";
if(j==2)
        cout<<" two\n";
if(j==3)
        cout<<" three\n";
if(j==4)
        cout<<" four\n";
if(j==5)
        cout<<" five\n";
if(j==6)
        cout<<" six\n";
if(j==7)
        cout<<" seven\n";
if(j==8)
        cout<<" eight\n";
if(j==9)
        cout<<" nine\n";
}
if(j>=70&&j<=79)
{
    cout<<"the given number is seventy";
    j=j-70;
if(j==1)
        cout<<" one\n";
if(j==2)
        cout<<" two\n";
if(j==3)
        cout<<" three\n";
if(j==4)
        cout<<" four\n";
if(j==5)
        cout<<" five\n";
if(j==6)
        cout<<" six\n";
if(j==7)
        cout<<" seven\n";
if(j==8)
        cout<<" eight\n";
if(j==9)
        cout<<" nine\n";
}
if(j>=80&&j<=89)
{
    cout<<"the given number is eihty";
    j=j-80;
if(j==1)
        cout<<" one\n";
if(j==2)
        cout<<" two\n";
if(j==3)
        cout<<" three\n";
if(j==4)
        cout<<" four\n";
if(j==5)
        cout<<" five\n";
if(j==6)
        cout<<" six\n";
if(j==7)
        cout<<" seven\n";
if(j==8)
        cout<<" eight\n";
if(j==9)
        cout<<" nine\n";
}
if(j>=90&&j<=99)
{
    cout<<"the given number is ninety";
    j=j-90;
if(j==1)
        cout<<" one\n";
if(j==2)
        cout<<" two\n";
if(j==3)
        cout<<" three\n";
if(j==4)
        cout<<" four\n";
if(j==5)
        cout<<" five\n";
if(j==6)
        cout<<" six\n";
if(j==7)
        cout<<" seven\n";
if(j==8)
        cout<<" eight\n";
if(j==9)
        cout<<" nine\n";
}
}



pirate.gif
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Help With Code/cutting It Short
3 Sep, 2008 - 04:34 AM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,022



Thanked: 81 times
Dream Kudos: 1175
My Contributions
Try making the if statements a function:

CODE

void doCheck(int min, int max, char* num){
  if(j>=min && j<=max){
    cout<<"the given number is " << num;
    j -= min;
    if(j==1)
        cout<<" one\n";
    if(j==2)
        cout<<" two\n";
    if(j==3)
        cout<<" three\n";
    if(j==4)
        cout<<" four\n";
    if(j==5)
        cout<<" five\n";
    if(j==6)
        cout<<" six\n";
    if(j==7)
        cout<<" seven\n";
    if(j==8)
        cout<<" eight\n";
    if(j==9)
        cout<<" nine\n";
  }
}


REALIZE - I don't use cout, so the variable output for "num" may be incorrect.

Then all you have to do is create a loop to go through all the possible mins and maxes.

HTH
User is online!Profile CardPM
+Quote Post

sensui
RE: Help With Code/cutting It Short
3 Sep, 2008 - 04:35 AM
Post #3

D.I.C Head
Group Icon

Joined: 24 Aug, 2008
Posts: 132



Thanked: 20 times
Dream Kudos: 50
My Contributions
Oh, God! what's there?
First: use a switch statement;
Second: use modulo operator ( % ) and division operator ( / ) to get the last and the first digit.
Look at this example:
cpp
#include <iostream>
using namespace std;

int main() {
int j;
do {
cout << "Enter a number from 20 to 99: " << endl;
cin >> j;
} while( j > 99 || j < 20 );

switch( j / 10 ) {
case 2:
cout << "twenty ";
break;
case 3:
cout << "thirty ";
break;
case 4:
cout << "forty ";
break;
case 5:
cout << "fifty ";
break;
case 6:
cout << "sixty ";
break;
case 7:
cout << "seventy ";
break;
case 8:
cout << "eighty ";
break;
case 9:
cout << "ninety ";
break;
};

switch( j % 10 ) {
case 0:
break;
case 1:
cout << "one";
break;
case 2:
cout << "two";
break;
case 3:
cout << "three ";
break;
case 4:
cout << "four ";
break;
case 5:
cout << "five ";
break;
case 6:
cout << "six ";
break;
case 7:
cout << "seven ";
break;
case 8:
cout << "eight ";
break;
case 9:
cout << "nine ";
break;
};

cin.get();
return EXIT_SUCCESS;
}


Was this post helpful ? smile.gif -->
User is offlineProfile CardPM
+Quote Post

james_L
RE: Help With Code/cutting It Short
4 Sep, 2008 - 03:40 AM
Post #4

New D.I.C Head
*

Joined: 28 Aug, 2008
Posts: 13


My Contributions
QUOTE(sensui @ 3 Sep, 2008 - 05:35 AM) *

Oh, God! what's there?
First: use a switch statement;
Second: use modulo operator ( % ) and division operator ( / ) to get the last and the first digit.



Was this post helpful ? smile.gif --> Once again thanks for the help sensui-sama! ,(^o^)7-salute!


User is offlineProfile CardPM
+Quote Post

sensui
RE: Help With Code/cutting It Short
4 Sep, 2008 - 04:05 AM
Post #5

D.I.C Head
Group Icon

Joined: 24 Aug, 2008
Posts: 132



Thanked: 20 times
Dream Kudos: 50
My Contributions
QUOTE(james_L @ 4 Sep, 2008 - 04:40 AM) *
sensui-sama!


biggrin.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 06:05AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month