Edit1->Text (Iterate over text -Rather Urgent

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

Join Date: May 2007
Posts: 6
Reputation: jenco is an unknown quantity at this point 
Solved Threads: 0
jenco jenco is offline Offline
Newbie Poster

Edit1->Text (Iterate over text -Rather Urgent

 
0
  #1
Jul 26th, 2007
The task is to put text in ab Edit box & then to iterate over it. The result should be in a block graph. If there is a vowel =="A" then block one should rise, if =="B" then block two should rise to represent it.

I can make this work for the first letter in the edit box with the following code:
  1. /* {
  2.   int Index;
  3.   Index=1;
  4.  
  5.   for(Index=1;Index<=5;Index++)
  6.   {
  7.  
  8.   if(Edit1->Text=="A")
  9.   {
  10.   Shape1->Height= (Shape1->Height+5);
  11.   Shape1->Top = Shape1->Top -5;
  12.   }
  13.   if(Edit1->Text=="E")
  14.   {
  15.   Shape2->Height= (Shape2->Height+5);
  16.   Shape2->Top = Shape2->Top -5;
  17.   }
  18.   if(Edit1->Text=="I")
  19.   {
  20.   Shape3->Height= (Shape3->Height+5);
  21.   Shape3->Top = Shape3->Top -5;
  22.   }
  23.   if(Edit1->Text=="O")
  24.   {
  25.   Shape4->Height= (Shape4->Height+5);
  26.   Shape4->Top = Shape4->Top -5;
  27.   }
  28.   if(Edit1->Text=="U")
  29.   {
  30.   Shape5->Height= (Shape5->Height+5);
  31.   Shape5->Top = Shape5->Top -5;
  32.   }
  33.   }*/
BUT this only works when the letter is at Index=1. I need to scan a sentence so that the result of the graph shows how many A's how many E's how many I's how many O's how many U's.

How do I get it to read over the text & not just the first letter?
Last edited by Ancient Dragon; Jul 26th, 2007 at 2:21 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,735
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: 281
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Edit1->Text (Iterate over text -Rather Urgent

 
0
  #2
Jul 26th, 2007
Presumably Edit1->Text is a string of some type. Assuming this is a Windows based program, then it's probably a C style string. If that assumption is correct then each letter in the string can be accessed by using the [] operator, like this:

Edit1->Text[x];

where x ranges from 0 to strlen(Edit1->Text) - 1.

If Edit1->Text is some form of a string other than a C style string, then some other form of access may be necessary.

NB: you might need to copy Edit1->Text to another string like this:
char input[256];
strcpy(input, Edit1->Text);

and then iterate through input if this

Edit1->Text[x];

doesn't work.
Last edited by Lerner; Jul 26th, 2007 at 1:35 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 6
Reputation: jenco is an unknown quantity at this point 
Solved Threads: 0
jenco jenco is offline Offline
Newbie Poster

Re: Edit1->Text (Iterate over text -Rather Urgent

 
0
  #3
Jul 26th, 2007
Hi there,

Its C++, using Boreland 5 as a compiler, so its really visual C++, but using C++ as basis. I'm afraid this didn't work, do you have any other ideas.

The problem is just to iterate over a line of text & then show the vowels in a bar chart.

Any suggestions would be greatly appreciated.

Thanks

jenco


Originally Posted by Lerner View Post
Presumably Edit1->Text is a string of some type. Assuming this is a Windows based program, then it's probably a C style string. If that assumption is correct then each letter in the string can be accessed by using the [] operator, like this:

Edit1->Text[x];

where x ranges from 0 to strlen(Edit1->Text) - 1.

If Edit1->Text is some form of a string other than a C style string, then some other form of access may be necessary.

NB: you might need to copy Edit1->Text to another string like this:
char input[256];
strcpy(input, Edit1->Text);

and then iterate through input if this

Edit1->Text[x];

doesn't work.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,735
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: 281
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Edit1->Text (Iterate over text -Rather Urgent

 
0
  #4
Jul 26th, 2007
Edit->Text should be a word or a phrase, say you entered ther phrase--three blind mice. Again that word/phrase should be a string of some type, in Borland it might be called a TString or some such. Whatever. That string type should have an overloaded [] operator associated with it to allow you to access each letter in the word or phrase (and it should have a method for you to extract the embedded C style string if you wish, maybe c_str() or some such, if you really wanted to). With that you can iterate over each letter in the input one by one. When refering to a hard coded letter in C/C++ you use single quotes, like this, 'A', rather than double quotes like this, "A", which are used to denote a string called a literal string (it isn't quite the same thing as a routine C style string, but it's close). Therefore, I'd:

1) Find the length of the string in Edit1 using whatever method you have available.
2) Convert the string into all caps or all lower case letters so I didn't have to worry about 'A' vs 'a', etc.
3) Create a container to hold the number of A, E, I, O and Us found in the string
4) Iterate over the entire string incrementing the appropriate counter in the container based on the letter found.
5) Display the results graphically by iterating over the container of counters representing the number of A, E, I, O and Us.

So maybe something like this:
  1. //copy TString or whatever into a C style string
  2. char input[256];
  3. strcpy(input, Edit1->Text.c_str());
  4. //find it's length
  5. int len = strlen(input);
  6. //convert it to all upper case letters
  7. for(int j = 0; j < len; ++j)
  8. input[j] = toupper(input[j]);
  9.  
  10. //declare container of counters and initialize to zero
  11. int counters[5] = 0;
  12.  
  13. //iterate over input finding vowels
  14. for(int j = 0; j < len; ++j)
  15. {
  16. //I'd use a switch statement instead of a series of if statements
  17. switch(input[j])
  18. {
  19. case 'A': //notice single quotes, not double
  20. counter[0]++;
  21. break;
  22. case 'E':
  23. counter[1]++;
  24. break;
  25. //etc
  26. }
  27. }
  28.  
  29. //use a loop and the graphical interface to create pretty bars representing number of A, E, I, O and Us found in input which in turn was Edit1->Text.

I don't like the idea of having to learn the basic principles of looping, usage of the [] operator to access individual char of a string, and retrieval of and manipulation of data from edit boxes in addition and creation of colored bars to display results. But if that's what you have to do, then so be it.
Last edited by Lerner; Jul 26th, 2007 at 6:11 pm.
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC