954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Is there a simplest way to work this array problem?

This is an array string problem where I have to ask for a sentence input that includes an integer (between 0 and 9), then the program has to change that integer to its name in words.

Like: This is test 3.

Would say: This is test three.

My program does it, but it includes many nested if statements, so I want to know if there is a shorter way to do it. Also, can I do it for any sentence length?
Or do I have to tell the user how long it has to be?

For this program i did it as if the user is going to enter a sentence 6 words long or less!

#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>


using namespace std;

int main(int argc, char *argv[])
{
   
int size=6;

   string sentence[size];
   string done;

  cout<<"enter a sentence that includes a number:"<<endl;
  for (int n=0; n<size; n++)
 cin>>sentence[n];
 
 
 
   
   for(int i=0; i<size; i++)
   {
   if (sentence[i]=="0")
   {sentence[i]="zero";
   }
   else
   {
     if(sentence[i]=="1")
           {sentence[i]="one";
           }
           else
           { 
           if (sentence[i]=="2")
             { sentence[i]="two";
             }
                else
                { if (sentence[i]=="3")
                     {sentence[i]="three";
                     }
                   else
                   {
                       if (sentence[i]=="4")
                       {sentence[i]="four"; 
                       }
                       else
                       {
                           if(sentence[i]=="5")
                           {sentence[i]="five";
                           }
                           else 
                           {
                                if(sentence[i]=="6") 
                                {sentence[i]="six";
                                }
                                else
                                {
                                    if (sentence[i]=="7")
                                    {sentence[i]="seven";
                                    }
                                    else
                                    {
                                        if(sentence[i]=="8")
                                        {sentence[i]="eigth";
                                        }
                                        else
                                        {
                                            if(sentence[i]=="9")
                                            {sentence[i]="nine";
                                            }
                                        }
                                    }
                                }
                           }
                       }
                   }            
                }
             }
           
           }
            cout<<sentence[i]<<" ";
                 
   }    
   
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
angel22
Newbie Poster
23 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

You could do it with a switch/case statement...

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

You can avoid nested if statements with the else if clause:

if ( sentence[i] == '0' )
  cout<<"zero";
else if ( sentence[i] == '1' )
  cout<<"one";
else if ( sentence[i] == '2' )
  cout<<"two";
else if ( sentence[i] == '3' )
  cout<<"three";
else if ( sentence[i] == '4' )
  cout<<"four";
else if ( sentence[i] == '5' )
  cout<<"five";
else if ( sentence[i] == '6' )
  cout<<"six";
else if ( sentence[i] == '7' )
  cout<<"seven";
else if ( sentence[i] == '8' )
  cout<<"eight";
else if ( sentence[i] == '9' )
  cout<<"nine";

Or with a switch statement:

switch ( sentence[i] ) {
case '0': cout<<"zero"; 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;
}

Alternatively, a table based approach is generally easier to get right:

string number[] = {
  "zero","one","two","three","four","five","six","seven","eight","nine"
};

cout<< number[sentence[i] - '0'];
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thank you very much!

I am going to try this!

It really is much shorter than my long nested if statements!

thanks!

angel22
Newbie Poster
23 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Narue,

Isn't this supposed to be:
if ( sentence[i][0] == '0' )
cout<<"zero";
else if ( sentence[i][0] == '1' )
..etc..?

you have:
if ( sentence[i] == '0' )

You want to compare the first character. The sentence[i] is a string not a character.

Same thing with this:
switch ( sentence[i] )
should be:
switch ( sentence[i][0] )

(It's nice to be able to correct you once instead of the opposite)..

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

You are rigth!

I tried to work with the switch statement without the two brakets and it did not work.

However, tell me when how do you refer to
this statement:

switch(sentence[i][0])

Does the second braket hold any value or not, its just that two dimensional arrays confuse me a little so i am not sure if both brakets chage value or not?

angel22
Newbie Poster
23 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

I can't believe Narue hasn't commented on this...

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

In the OP sentence was declared thus:

string sentence[size];

Therefore, in the syntax used by the OP sentence is an array of string of size size. Therefore to refer to a given char of a given string you would need to use multidimensional []s. However, why use that syntax? It's much easier to just use:

string sentence;

and then use the routine single dimension [] to refer to a given char within sentence. This is what Narue apperas to have to used in her demonstrations.

The point of the post/original question was how to convert the value of a given char or a given int into a word of English. The techniques presented can be modified to whatever syntax you need/wish.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Also note, in the OP, that

int size = 6;
string sentence[size];

is illegal as the size of a static array needs to be specified using a constant value, not a variable. To use a variable to declare memory for an array you must use dynamic memory, not static. This is just another reason to use a single string, not an array of strings. And again, since it wasn't the meat of the question, it is more for your education, than to answer the original question.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

>I can't believe Narue hasn't commented on this...
Narue has a day job that saps a lot of her time. :rolleyes: Anyway, any similarity between my examples and a complete solution is purely coincidence. If any code that I post doesn't compile, then chances are good that the intention was to convey an idea, and it's your job to integrate the idea into your code. ;)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

(I'll let it slip this time... )

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You