Jump to content
  • Advertisement
Sign in to follow this  
bluefox25

? about reading characters

This topic is 4084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to be able to place each word into a variable so that I can test each variable for integers(I got to convert integers into '*' except when the integer is next to letter(james5 bond 007= james5 bond *** ) How do you hold each individual word in a variable? Would you use a c-string or string variable? Suppose: char aWord[100]; string words; cout << "Enter text:"; Assume user types in "Hi Mom!" If I use getline(aWord,100) this method, each element will contain a character correct?(aWord[0]=H,aWord[1]=i) If I use the string variable, as getline(cin,words), would each element of word be a character or word? Would it be words.at(0)='h', or words.at(0)="Hi"? Is my thinking way off? Any help greatly appreciated. Am I thinnking too hard?

Share this post


Link to post
Share on other sites
Advertisement
there is a handy C function called strtok which basically allows you to read in a buffer one word at a time. Then using the tokenized words you could perform a sscanf to see if the string is a pure float, if it is then the entire buffer can be changed to **** characters and appended to the end of a string. Its a little complicated to explain but this is the jist of it. Also when using strtok the delimiter is skipped, in this case the " " token separates our words so we need to add them back in after we process the token. (delimiter is fancy word for "word break indicator")

You can search any of these C functions and their cousins from http://www.cplusplus.com/reference/clibrary/

psuedo
Read file into buffer
loop(for all words in buffer)
scan token for float
if float found change token to all * chars
append token to the end of our message
end
/psuedo


char Buffer[]; //this is a buffer containing the file, type or format may need to be changed to fit your needs
char* Token;
bool First = true;
int place_holder;
std::string Message;

while((Token = strtok((First ? Buffer : NULL), " ")) != NULL)
{
First = false;
if(sscanf(Token, "%i", place_holder))
{
for(int i = 0; i < strlen(Token); i++) //strlen gets the number of actual characters in a string
{
Token = '*';
}
}
Message += std::string(Token);
Message += " "; // here we add the space back in as mentioned above
}




that should work just fine, you may need to rework it depending on if your reading from a string buffer or file.

Note: The weird inline condition in the loop is there because the first time strtok is called the buffer name must be provided after that NULL is used to indicate the next word of the same string

hope I helped [grin]

Share this post


Link to post
Share on other sites
Thanks, now if my teacher will accept it, thats another atory!! We haven't gone over any of that stuff yet. Just went over arrays,strings,file I/O. The assignment goes something like this:

Write a program that reads in a line of text and outptus the line with all the digits in all integer numbers replaced with '*'.

For example,

My userID is john17 and my 4 digit pin is 1234 which is secret.

Output:

My userID is john17 and my * digit pin is **** which is secret.

Not that if a digit is part of a word, then the digit is not changed to an '*' (asterisks). For example, john17 is NOT changed to john**. Include a loop that allows the user to repeat this calculation again until the users decides to end the program.

Share this post


Link to post
Share on other sites
Please don't use strtok in c++, it's left over from C and there are usually far better ways of doing the same thing. Problems with strtok include using c-strings, not being reentrant, and in my opinion unusual in terms of memory ownership.

You can read individual words in c++ by doing


std::string myString
std::cin>>myString

Share this post


Link to post
Share on other sites
Can't I just do this instead?
#include <iostream>

using namespace std;

int main(){
string word;
cin >> word;
=========================

I did this but when I cout << word.at(0) for example, it only gives me the first character. Shouldn't it be the first word?

Share this post


Link to post
Share on other sites
Well, std::cout << word.at(0) pretty much does what it means: it outputs the character at position zero in the word. If you want to output the word, then std::cout << word instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!