| | |
Is there a simplest way to work this array problem?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 23
Reputation:
Solved Threads: 0
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!
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)
#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; }
You can avoid nested if statements with the else if clause:
Or with a switch statement:
Alternatively, a table based approach is generally easier to get right:
C++ Syntax (Toggle Plain Text)
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";
C++ Syntax (Toggle Plain Text)
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; }
C++ Syntax (Toggle Plain Text)
string number[] = { "zero","one","two","three","four","five","six","seven","eight","nine" }; cout<< number[sentence[i] - '0'];
I'm here to prove you wrong.
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)..
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)..
•
•
Join Date: Jul 2005
Posts: 23
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
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.
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.
>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 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.
![]() |
Similar Threads
- Array problem (C++)
- class array problem! (C++)
- Array declaration problem (C++)
- Large Array Problem (PHP)
- How do I create a program using an Array ? (C++)
- Array limit (C)
Other Threads in the C++ Forum
- Previous Thread: Small ATM banking program
- Next Thread: I need a bit of help...ASAP....
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






