473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to put together a string

29
This is a snippet of my code that does not work:

Expand|Select|Wrap|Line Numbers
  1. while(ascii != 32) // while character is not space
  2.             {
  3.                 letter = (char) infile.get(); //retreive next character
  4.                 ascii = (int) letter;
  5.                 if (ascii == 32)
  6.                     break;
  7.                 word = word + letter;
  8.             } 
I use a text file with 3 words "this this this". The string word records the letters that are found but when it gets to the spacebar, with ascii code 32, instead prints -1 and the "y with dots over it symbol" goes into the string.

How should I fix this?
Oct 7 '10 #1

✓ answered by ashitpro

ascii value -1 represents end of file and not space.
space has ascii value 32.

There are lots of logical problems in this code.
I have fixed the few of them. see if this code runs at your end. This is g++ compliant, you may need to make few changes.

Expand|Select|Wrap|Line Numbers
  1. #include <algorithm>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <iostream>
  5. #include <map>
  6. #include <string>
  7.  
  8. //NEEDS TO BE FIXED: FINDS LETTERS NOT WORDS
  9. using namespace std;
  10.  
  11. typedef map <string, unsigned int> map_string_int;
  12. typedef multimap <unsigned int, string> mmap_int_string;
  13.  
  14. int main() //int argc, char *argv[] insert into main for test
  15. {
  16.     char letter;
  17.     int counter = 0, number, value;
  18.     short ascii = 0;
  19.     string word, filename;
  20.  
  21.     mmap_int_string::const_iterator iElementFound;
  22.  
  23.     map_string_int string_to_int;
  24.     mmap_int_string int_to_string;
  25.  
  26.     ifstream infile;
  27.  
  28.     cout << "Enter name of file: ";
  29.     getline(cin, filename);
  30.         cout<<filename;
  31.         //exit(0);
  32.     infile.open(filename.c_str(),ifstream::in); //argv[1] insert for test
  33.  
  34.     cout << endl;
  35.  
  36.     cout << "letter | ascii | word" << endl;
  37.     int out=0;
  38.     while(infile.good() && out == 0)
  39.     {
  40.         while(!infile.eof() && out == 0)
  41.         {
  42.             while(1)
  43.             {
  44.                 letter = (char) infile.get();//retreive next character
  45.                 cout << letter << " | ";
  46.                 ascii = (int) letter;
  47.                 cout << ascii << " | ";
  48.                 if (ascii == 32 || ascii == 10)
  49.                 {
  50.                     break;
  51.                 }
  52.                 if (infile.eof())
  53.                 {
  54.                     out = 1;
  55.                     break;
  56.                 }
  57.                 word = word + letter;
  58.                 cout << word << endl;
  59.             }
  60.  
  61.             //try to find word in index map
  62.             map_string_int::const_iterator indexElementFound = string_to_int.find(word);
  63.  
  64.             if (indexElementFound != string_to_int.end())
  65.             {
  66.                 //if found, create another pair in multimap
  67.                 value = indexElementFound->second;
  68.                 int_to_string.insert(make_pair (value, word));
  69.             }else{
  70.                 //if not found, create pair in both index map and multimap
  71.                 counter++;
  72.                 string_to_int.insert(make_pair (word, counter));
  73.                 int_to_string.insert(make_pair (counter, word));
  74.             }
  75.             word = ""; //clear word for next part of file
  76.         }
  77.  
  78.         //create a list by searching multimap
  79.         for(value = 0; value < counter; value++)
  80.         {
  81.  
  82.             number = int_to_string.count(value);
  83.             iElementFound = int_to_string.find(value);
  84.             if(iElementFound != int_to_string.end())
  85.             {
  86.               cout << endl << int_to_string.find(value)->second << " : " << number << endl;
  87.  
  88.             }
  89.         }
  90.     }
  91.     infile.close();
  92.  
  93.     char response;
  94.     cin >> response;
  95.  
  96.     return 0;
  97. }
  98.  

7 2589
ashitpro
542 Expert 512MB
Try printing letter after line 3 and ascii after line 4
Send the results.
Oct 8 '10 #2
donbock
2,426 Expert 2GB
What is word? Is it a string where the plus sign is overloaded to mean concatenate or is it an integral type? If it is an integral type then computing the sum of several characters is not going to be meaningful.

What is the initial value of ascii? That is, why do you expect to be able to get into the while loop?

By the way, replace the magic number "32" with character constant ' '.
Oct 8 '10 #3
Alex T
29
Here is the complete code for your convenience:

Expand|Select|Wrap|Line Numbers
  1. #include <algorithm>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <iostream>
  5. #include <map>
  6. #include <string>
  7.  
  8. //NEEDS TO BE FIXED: FINDS LETTERS NOT WORDS
  9. using namespace std;
  10.  
  11. typedef map <string, unsigned int> map_string_int;
  12. typedef multimap <unsigned int, string> mmap_int_string;
  13.  
  14. int main() //int argc, char *argv[] insert into main for test
  15. {
  16.     char letter;
  17.     int counter = 0, number, value;
  18.     short ascii = 0;
  19.     string word, filename;
  20.  
  21.     mmap_int_string::const_iterator iElementFound;
  22.  
  23.     map_string_int string_to_int;
  24.     mmap_int_string int_to_string;
  25.  
  26.     ifstream infile;
  27.  
  28.     cout << "Enter name of file: ";
  29.     getline(cin, filename);
  30.     infile.open(filename); //argv[1] insert for test
  31.  
  32.     cout << endl;
  33.  
  34.     cout << "letter | ascii | word" << endl;
  35.  
  36.     while(infile.good())
  37.     {
  38.         while(!infile.eof())
  39.         {
  40.             while(ascii != -1) // while character is not space
  41.             {
  42.                 letter = (char) infile.get();//retreive next character
  43.                 cout << letter << " | ";
  44.                 ascii = (int) letter;
  45.                 cout << ascii << " | ";
  46.                 if (ascii == -1)
  47.                     break;
  48.                 word = word + letter;
  49.                 cout << word << endl;
  50.             }
  51.  
  52.             //try to find word in index map
  53.             map_string_int::const_iterator indexElementFound = string_to_int.find(word); 
  54.  
  55.             if (indexElementFound != string_to_int.end())
  56.             {
  57.                 //if found, create another pair in multimap
  58.                 value = indexElementFound->second;
  59.                 int_to_string.insert(make_pair (value, word));
  60.             }else{
  61.                 //if not found, create pair in both index map and multimap
  62.                 counter++;
  63.                 string_to_int.insert(make_pair (word, counter));
  64.                 int_to_string.insert(make_pair (counter, word));
  65.             }
  66.             word = ""; //clear word for next part of file
  67.         }
  68.  
  69.         //create a list by searching multimap
  70.         for(value = 0; value < counter; value++)
  71.         {
  72.             number = int_to_string.count(value);
  73.             iElementFound = int_to_string.find(value);
  74.             cout << iElementFound->second << " : " << number << endl;
  75.         }
  76.     }
  77.  
  78.     infile.close();
  79.  
  80.     char response;
  81.     cin >> response;
  82.  
  83.     return 0;
  84. }
  85.  
My goal for this is to find the frequency of each word in the text file.

The text file has only the word "this".

For convenience, I have altered the code to make the output more meaningful: here it is.

Enter name of file: C:\\test1.txt

letter | ascii | word
t | 116 | t
h | 104 | th
i | 105 | thi
s | 115 | this
| -1 |


This is as far as it gets before the program stops functioning. Please assist.
Oct 8 '10 #4
ashitpro
542 Expert 512MB
ascii value -1 represents end of file and not space.
space has ascii value 32.

There are lots of logical problems in this code.
I have fixed the few of them. see if this code runs at your end. This is g++ compliant, you may need to make few changes.

Expand|Select|Wrap|Line Numbers
  1. #include <algorithm>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <iostream>
  5. #include <map>
  6. #include <string>
  7.  
  8. //NEEDS TO BE FIXED: FINDS LETTERS NOT WORDS
  9. using namespace std;
  10.  
  11. typedef map <string, unsigned int> map_string_int;
  12. typedef multimap <unsigned int, string> mmap_int_string;
  13.  
  14. int main() //int argc, char *argv[] insert into main for test
  15. {
  16.     char letter;
  17.     int counter = 0, number, value;
  18.     short ascii = 0;
  19.     string word, filename;
  20.  
  21.     mmap_int_string::const_iterator iElementFound;
  22.  
  23.     map_string_int string_to_int;
  24.     mmap_int_string int_to_string;
  25.  
  26.     ifstream infile;
  27.  
  28.     cout << "Enter name of file: ";
  29.     getline(cin, filename);
  30.         cout<<filename;
  31.         //exit(0);
  32.     infile.open(filename.c_str(),ifstream::in); //argv[1] insert for test
  33.  
  34.     cout << endl;
  35.  
  36.     cout << "letter | ascii | word" << endl;
  37.     int out=0;
  38.     while(infile.good() && out == 0)
  39.     {
  40.         while(!infile.eof() && out == 0)
  41.         {
  42.             while(1)
  43.             {
  44.                 letter = (char) infile.get();//retreive next character
  45.                 cout << letter << " | ";
  46.                 ascii = (int) letter;
  47.                 cout << ascii << " | ";
  48.                 if (ascii == 32 || ascii == 10)
  49.                 {
  50.                     break;
  51.                 }
  52.                 if (infile.eof())
  53.                 {
  54.                     out = 1;
  55.                     break;
  56.                 }
  57.                 word = word + letter;
  58.                 cout << word << endl;
  59.             }
  60.  
  61.             //try to find word in index map
  62.             map_string_int::const_iterator indexElementFound = string_to_int.find(word);
  63.  
  64.             if (indexElementFound != string_to_int.end())
  65.             {
  66.                 //if found, create another pair in multimap
  67.                 value = indexElementFound->second;
  68.                 int_to_string.insert(make_pair (value, word));
  69.             }else{
  70.                 //if not found, create pair in both index map and multimap
  71.                 counter++;
  72.                 string_to_int.insert(make_pair (word, counter));
  73.                 int_to_string.insert(make_pair (counter, word));
  74.             }
  75.             word = ""; //clear word for next part of file
  76.         }
  77.  
  78.         //create a list by searching multimap
  79.         for(value = 0; value < counter; value++)
  80.         {
  81.  
  82.             number = int_to_string.count(value);
  83.             iElementFound = int_to_string.find(value);
  84.             if(iElementFound != int_to_string.end())
  85.             {
  86.               cout << endl << int_to_string.find(value)->second << " : " << number << endl;
  87.  
  88.             }
  89.         }
  90.     }
  91.     infile.close();
  92.  
  93.     char response;
  94.     cin >> response;
  95.  
  96.     return 0;
  97. }
  98.  
Oct 9 '10 #5
donbock
2,426 Expert 2GB
Please change line 48 from
Expand|Select|Wrap|Line Numbers
  1.                 if (ascii == 32 || ascii == 10)
to
Expand|Select|Wrap|Line Numbers
  1.                 if ((ascii == ' ') || (ascii == '\n'))
Even better would be
Expand|Select|Wrap|Line Numbers
  1.                 if (isspace(ascii))

Lines 45 and 47 print the character before you check to see if it is a space. Thus, a single trailing space gets printed with each word. Is that what you wanted to do?

Don't you want to discard leading spaces too? Consider a state machine with two states: in-a-word and between-words. The initial state is between-words. Any non-whitespace character causes a transition from between-words to in-a-word. Any whitespace character causes a transition from in-a-word to between-words.
Oct 9 '10 #6
Alex T
29
Can you clarify what exactly changed in my code?
Oct 9 '10 #7
Alex T
29
Nevermind, don't. I fixed the problem. Thank you all for your help.
Oct 9 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
8
by: Grant Wagner | last post by:
I'm a bit confused by String() (typeof 'string') vs new String() (typeof 'object'). When you need to access a method or property of a -String-, what type is JavaScript expecting (or rather, what...
7
by: archway | last post by:
I know you cannot have string enumerations such as: enum myStringEnum { enumItem1 = "value 1", enumItem2 = "value 2", etc } However, I was wondering whether you had ever created something...
1
by: Jay Balapa | last post by:
Hello, If I use AppendFormat with string as a parameter. I get string cannot be converted to IFormatProvider. This occurs in 2.0 Thanks. Jay
1
by: Jim Michaels | last post by:
=> Array ( => UML:CLASS => open => 5 => Array ( => .:00000000000008EC => quiz_batteries => public
6
by: Don Lancaster | last post by:
I need to progrmatically do this inside a loop this.fh03.value = fixFloat (Harms, numPoints) ; with the numbers changing per an index. If I try curHvals = "03" ; // (derived from...
7
by: php_mysql_beginer911 | last post by:
Hi .. hope someone will help i am trying to figure it out why i cannot post string "union select" every time i try to post data which content union and select .. the page doesn't get posted and...
5
by: samatair | last post by:
I get an error message like this. Fatal error: Cannot use string offset as an array in D:\www\site\includes\change_preference.inc.php on line 310 $cityName = $row2; The above code is causing...
1
by: andrewc | last post by:
I am trying to get a 2 character string into my program in order to then determine a command based on the string. This is in an embedded application. Right at the top of my program I...
19
by: Queen Soso | last post by:
hi there, When I run this line I get an error: obj.number_type = dropdownlist1.selectedItem.value; number_type is a class type and I want to put a string value in it from a drop down list. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.