By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

How to put together a string

P: 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.  

Share this Question
Share on Google+
7 Replies


ashitpro
Expert 100+
P: 542
Try printing letter after line 3 and ascii after line 4
Send the results.
Oct 8 '10 #2

Expert 100+
P: 2,400
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

P: 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
Expert 100+
P: 542
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

Expert 100+
P: 2,400
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

P: 29
Can you clarify what exactly changed in my code?
Oct 9 '10 #7

P: 29
Nevermind, don't. I fixed the problem. Thank you all for your help.
Oct 9 '10 #8

Post your reply

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