944,098 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2362
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 31st, 2005
0

Is there a simplest way to work this array problem?

Expand Post »
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!
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
angel22 is offline Offline
23 posts
since Jul 2005
Jul 31st, 2005
0

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

You could do it with a switch/case statement...
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jul 31st, 2005
0

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

You can avoid nested if statements with the else if clause:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. string number[] = {
  2. "zero","one","two","three","four","five","six","seven","eight","nine"
  3. };
  4.  
  5. cout<< number[sentence[i] - '0'];
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 31st, 2005
0

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

Thank you very much!

I am going to try this!

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

thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
angel22 is offline Offline
23 posts
since Jul 2005
Jul 31st, 2005
0

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

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)..
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jul 31st, 2005
0

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

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
angel22 is offline Offline
23 posts
since Jul 2005
Aug 2nd, 2005
0

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

I can't believe Narue hasn't commented on this...
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Aug 2nd, 2005
0

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

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Aug 2nd, 2005
0

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

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Aug 2nd, 2005
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Small ATM banking program
Next Thread in C++ Forum Timeline: I need a bit of help...ASAP....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC