How to put together a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alex T
    New Member
    • Oct 2010
    • 29

    How to put together a string

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

    Code:
    while(ascii != 32) // while character is not space
    			{
    				letter = (char) infile.get(); //retreive next character
    				ascii = (int) letter;
    				if (ascii == 32)
    					break;
    				word = word + letter;
    			}
    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?
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    Try printing letter after line 3 and ascii after line 4
    Send the results.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      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 ' '.

      Comment

      • Alex T
        New Member
        • Oct 2010
        • 29

        #4
        Here is the complete code for your convenience:

        Code:
        #include <algorithm>
        #include <fstream>
        #include <iomanip>
        #include <iostream>
        #include <map>
        #include <string>
        
        //NEEDS TO BE FIXED: FINDS LETTERS NOT WORDS
        using namespace std;
        
        typedef map <string, unsigned int> map_string_int;
        typedef multimap <unsigned int, string> mmap_int_string;
        
        int main() //int argc, char *argv[] insert into main for test
        {
        	char letter;
        	int counter = 0, number, value;
        	short ascii = 0;
        	string word, filename;
        	
        	mmap_int_string::const_iterator iElementFound;
        	
        	map_string_int string_to_int;
        	mmap_int_string int_to_string;
        
        	ifstream infile;
        
        	cout << "Enter name of file: ";
        	getline(cin, filename);
        	infile.open(filename); //argv[1] insert for test
        
        	cout << endl;
        
        	cout << "letter | ascii | word" << endl;
        	
        	while(infile.good())
        	{
        		while(!infile.eof())
        		{
        			while(ascii != -1) // while character is not space
        			{
        				letter = (char) infile.get();//retreive next character
        				cout << letter << " | ";
        				ascii = (int) letter;
        				cout << ascii << " | ";
        				if (ascii == -1)
        					break;
        				word = word + letter;
        				cout << word << endl;
        			}
        		
        			//try to find word in index map
        			map_string_int::const_iterator indexElementFound = string_to_int.find(word); 
        		
        			if (indexElementFound != string_to_int.end())
        			{
        				//if found, create another pair in multimap
        				value = indexElementFound->second;
        				int_to_string.insert(make_pair (value, word));
        			}else{
        				//if not found, create pair in both index map and multimap
        				counter++;
        				string_to_int.insert(make_pair (word, counter));
        				int_to_string.insert(make_pair (counter, word));
        			}
        			word = ""; //clear word for next part of file
        		}
        
        		//create a list by searching multimap
        		for(value = 0; value < counter; value++)
        		{
        			number = int_to_string.count(value);
        			iElementFound = int_to_string.find(value);
        			cout << iElementFound->second << " : " << number << endl;
        		}
        	}
        
        	infile.close();
        
        	char response;
        	cin >> response;
        	
        	return 0;
        }
        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.

        Comment

        • ashitpro
          Recognized Expert Contributor
          • Aug 2007
          • 542

          #5
          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.

          Code:
          #include <algorithm>
          #include <fstream>
          #include <iomanip>
          #include <iostream>
          #include <map>
          #include <string>
          
          //NEEDS TO BE FIXED: FINDS LETTERS NOT WORDS
          using namespace std;
          
          typedef map <string, unsigned int> map_string_int;
          typedef multimap <unsigned int, string> mmap_int_string;
          
          int main() //int argc, char *argv[] insert into main for test
          {
              char letter;
              int counter = 0, number, value;
              short ascii = 0;
              string word, filename;
          
              mmap_int_string::const_iterator iElementFound;
          
              map_string_int string_to_int;
              mmap_int_string int_to_string;
          
              ifstream infile;
          
              cout << "Enter name of file: ";
              getline(cin, filename);
                  cout<<filename;
                  //exit(0);
              infile.open(filename.c_str(),ifstream::in); //argv[1] insert for test
          
              cout << endl;
          
              cout << "letter | ascii | word" << endl;
              int out=0;
              while(infile.good() && out == 0)
              {
                  while(!infile.eof() && out == 0)
                  {
                      while(1)
                      {
                          letter = (char) infile.get();//retreive next character
                          cout << letter << " | ";
                          ascii = (int) letter;
                          cout << ascii << " | ";
                          if (ascii == 32 || ascii == 10)
                          {
                              break;
                          }
                          if (infile.eof())
                          {
                              out = 1;
                              break;
                          }
                          word = word + letter;
                          cout << word << endl;
                      }
          
                      //try to find word in index map
                      map_string_int::const_iterator indexElementFound = string_to_int.find(word);
          
                      if (indexElementFound != string_to_int.end())
                      {
                          //if found, create another pair in multimap
                          value = indexElementFound->second;
                          int_to_string.insert(make_pair (value, word));
                      }else{
                          //if not found, create pair in both index map and multimap
                          counter++;
                          string_to_int.insert(make_pair (word, counter));
                          int_to_string.insert(make_pair (counter, word));
                      }
                      word = ""; //clear word for next part of file
                  }
          
                  //create a list by searching multimap
                  for(value = 0; value < counter; value++)
                  {
          
                      number = int_to_string.count(value);
                      iElementFound = int_to_string.find(value);
                      if(iElementFound != int_to_string.end())
                      {
                        cout << endl << int_to_string.find(value)->second << " : " << number << endl;
          
                      }
                  }
              }
              infile.close();
          
              char response;
              cin >> response;
          
              return 0;
          }

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Please change line 48 from
            Code:
                            if (ascii == 32 || ascii == 10)
            to
            Code:
                            if ((ascii == ' ') || (ascii == '\n'))
            Even better would be
            Code:
                            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.

            Comment

            • Alex T
              New Member
              • Oct 2010
              • 29

              #7
              Can you clarify what exactly changed in my code?

              Comment

              • Alex T
                New Member
                • Oct 2010
                • 29

                #8
                Nevermind, don't. I fixed the problem. Thank you all for your help.

                Comment

                Working...