CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2009
    Posts
    14

    Read string by words

    I dont know how good you are going to undestand me:
    I made an encryption program. It uses space between 2 letteres. Now I want to use the generated string and read every word at a time and convert it to a letter.
    Here is the code:
    // decript.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include <sstream>
    #include <algorithm>
    #include <iterator>
    using namespace std;



    int main() {


    cout <<"Enter encripted text" <<endl;
    char str[2];
    cin >> str[1];
    char line = str[1];
    char pch;

    pch = strtok (line," ");
    while (pch != NULL)
    {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ");
    }



    system("PAUSE");
    }
    For some reason, I doesnt works.
    Please help me!

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Read string by words

    Phew, where to start....
    • Use code tags, please - read the FAQ if you don't konw what they are
    • Your variable str is an array of two characters
    • You are reading in a single character from the user, storing it at the second position of your array, then you assign this character to the variable line
    • Your call to strtok will not compile, becuase strtok expects a character string as first argument, not a single character (how would it split up a single character)
    • Why not use std::string instead of char ?
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    May 2009
    Posts
    14

    Talking Re: Read string by words

    Sorry about code tags........
    Sorry, but I'll have to do mare reasearch regarding std::string ( what is the difference?).
    Here is the code now:
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include <sstream>
    #include <algorithm>
    #include <iterator>
    using namespace std;
    
    
    
    int main() {
    
    	
      cout <<"Enter encripted text" <<endl;
      std::string str;
      cin >> str;
      std::string pch;
      bool h = true;
    
      while (h == false)
      {
        printf ("&#37;s\n",pch);
        if (pch = strtok (str, " "))
    	h = true;
    	if (pch = strtok (str, "."))
    	h = false;
    
      }
    
        
     
    	system("PAUSE");
    }
    Now I get less errors, but still doesnt works;
    Last edited by zorro59; May 7th, 2009 at 01:14 AM.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Read string by words

    Quote Originally Posted by zorro59 View Post
    Sorry, but I'll have to do mare reasearch regarding std::string ( what is the difference?).
    std::string is a class wrapping a dynamic char array. In addition to handling all the tricky memory management internally so that you don't have to, it provides numerous operators which make string operations much more intuitive. Plus it removes the possibility of overwriting the array bounds that you get with fixed-size arrays.

    The one thing it doesn't play well with is strtok. You can get equivalent functionality out of it with other methods, but since strtok changes the string in-place by inserting NULLs, you can't use it directly.

  5. #5
    Join Date
    May 2009
    Posts
    14

    Re: Read string by words

    @Lindley
    Thanks for your quick message, I found out that std::string doesnt work with strtok.......Any example of conversion(I have no idea) ???
    This works:
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    int main()
    {
    std::string myText("Hello everyone");
    std::istringstream iss(myText);
    std::string token;
    
    while(getline(iss, token, ' '))
    {
          std::cout << token << std::endl;
    }
    system("PAUSE");
    }
    But I cant use cin.....
    Last edited by zorro59; May 7th, 2009 at 01:38 AM.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Read string by words

    Quote Originally Posted by zorro59
    But I cant use cin.....
    Why not? std::cin is a std::istream.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    May 2009
    Posts
    14

    Talking Re: Read string by words

    Thank you for help, here is the code that worked:
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <fstream>
    
    using std::ifstream;
    using std::ofstream;
    using std::ios;
    using namespace std;
    int main()
    {
    	string line;
    	string im;
      ifstream myfile ("C:\\Users\\Robert\\Desktop\\test.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile,line);
    	  im += line;
        }
        myfile.close();
      }
    
      else cout << "Unable to open file"; 
    
    std::string myText(im);
    std::istringstream iss(myText);
    std::string token;
    
    while(getline(iss, token, ' '))
    {
          std::cout << token << std::endl;
    }
    system("PAUSE");
    }
    Perhaps it can help anyone....

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured