Is there a simplest way to work this array problem?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Is there a simplest way to work this array problem?

 
0
  #1
Jul 31st, 2005
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!
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.  
  12. int size=6;
  13.  
  14. string sentence[size];
  15. string done;
  16.  
  17. cout<<"enter a sentence that includes a number:"<<endl;
  18. for (int n=0; n<size; n++)
  19. cin>>sentence[n];
  20.  
  21.  
  22.  
  23.  
  24. for(int i=0; i<size; i++)
  25. {
  26. if (sentence[i]=="0")
  27. {sentence[i]="zero";
  28. }
  29. else
  30. {
  31. if(sentence[i]=="1")
  32. {sentence[i]="one";
  33. }
  34. else
  35. {
  36. if (sentence[i]=="2")
  37. { sentence[i]="two";
  38. }
  39. else
  40. { if (sentence[i]=="3")
  41. {sentence[i]="three";
  42. }
  43. else
  44. {
  45. if (sentence[i]=="4")
  46. {sentence[i]="four";
  47. }
  48. else
  49. {
  50. if(sentence[i]=="5")
  51. {sentence[i]="five";
  52. }
  53. else
  54. {
  55. if(sentence[i]=="6")
  56. {sentence[i]="six";
  57. }
  58. else
  59. {
  60. if (sentence[i]=="7")
  61. {sentence[i]="seven";
  62. }
  63. else
  64. {
  65. if(sentence[i]=="8")
  66. {sentence[i]="eigth";
  67. }
  68. else
  69. {
  70. if(sentence[i]=="9")
  71. {sentence[i]="nine";
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }
  81.  
  82. }
  83. cout<<sentence[i]<<" ";
  84.  
  85. }
  86.  
  87.  
  88. system("PAUSE");
  89. return EXIT_SUCCESS;
  90. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Is there a simplest way to work this array problem?

 
0
  #2
Jul 31st, 2005
You could do it with a switch/case statement...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Is there a simplest way to work this array problem?

 
0
  #3
Jul 31st, 2005
You can avoid nested if statements with the else if clause:
  1. if ( sentence[i] == '0' )
  2. cout<<"zero";
  3. else if ( sentence[i] == '1' )
  4. cout<<"one";
  5. else if ( sentence[i] == '2' )
  6. cout<<"two";
  7. else if ( sentence[i] == '3' )
  8. cout<<"three";
  9. else if ( sentence[i] == '4' )
  10. cout<<"four";
  11. else if ( sentence[i] == '5' )
  12. cout<<"five";
  13. else if ( sentence[i] == '6' )
  14. cout<<"six";
  15. else if ( sentence[i] == '7' )
  16. cout<<"seven";
  17. else if ( sentence[i] == '8' )
  18. cout<<"eight";
  19. else if ( sentence[i] == '9' )
  20. cout<<"nine";
Or with a switch statement:
  1. switch ( sentence[i] ) {
  2. case '0': cout<<"zero"; break;
  3. case '1': cout<<"one"; break;
  4. case '2': cout<<"two"; break;
  5. case '3': cout<<"three"; break;
  6. case '4': cout<<"four"; break;
  7. case '5': cout<<"five"; break;
  8. case '6': cout<<"six"; break;
  9. case '7': cout<<"seven"; break;
  10. case '8': cout<<"eight"; break;
  11. case '9': cout<<"nine"; break;
  12. }
Alternatively, a table based approach is generally easier to get right:
  1. string number[] = {
  2. "zero","one","two","three","four","five","six","seven","eight","nine"
  3. };
  4.  
  5. cout<< number[sentence[i] - '0'];
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Re: Is there a simplest way to work this array problem?

 
0
  #4
Jul 31st, 2005
Thank you very much!

I am going to try this!

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

thanks!
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Is there a simplest way to work this array problem?

 
0
  #5
Jul 31st, 2005
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)..
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Re: Is there a simplest way to work this array problem?

 
0
  #6
Jul 31st, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Is there a simplest way to work this array problem?

 
0
  #7
Aug 2nd, 2005
I can't believe Narue hasn't commented on this...
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Is there a simplest way to work this array problem?

 
0
  #8
Aug 2nd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Is there a simplest way to work this array problem?

 
0
  #9
Aug 2nd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Is there a simplest way to work this array problem?

 
0
  #10
Aug 2nd, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC