-
May 6th, 2009, 02:16 PM
#1
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!
-
May 6th, 2009, 02:58 PM
#2
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.
-
May 7th, 2009, 01:04 AM
#3
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 ("%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.
-
May 7th, 2009, 01:19 AM
#4
Re: Read string by words
Originally Posted by zorro59
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.
-
May 7th, 2009, 01:29 AM
#5
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.
-
May 7th, 2009, 01:58 AM
#6
Re: Read string by words
Originally Posted by zorro59
But I cant use cin.....
Why not? std::cin is a std::istream.
-
May 7th, 2009, 02:10 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|