How to make it only read ONE word in C++ [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here's my code:
char *reason = strtok(NULL, "\n");
std::string kickreason = "No reason";
if(reason)
kickreason = reason;
How do I make the "kickreason" only read 1 word? So if the user types in "haha lol XD" it will only read "haha"?

Put the string in a std::istringstream and use the normal input operator >>:
std::istringstream iss("haha lol XD");
std::string word;
iss >> word;
std::cout << "First word is \"" << word << "\"\n";

stringstream::operator>>(string)? that's where you need to look at.
like this:
istringstream str("no reason");
string oneword = "";
str >> oneword;

how about using strtok()
#include <stdio.h>
#include <iostream>
int main(int argc, char *argv[]) {
char sentence[] = "haha lol XD";
char * word;
std::cout << "Your sentence: " << sentence << std::endl;
word = strtok (sentence," ");
std::cout << "First word: " << word << std::endl;
return 0;
}
The output is
Your sentence: haha lol XD
First word: haha

Related

Counting number of words in a file using c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have been asked to develop program to count no. of lines and words in the file, this is my trial, my teacher said that I cant use >> operator for counting words and comparing but I could not handle it.
#include <iostream>
#include <fstream>
using namespace std;
int numberLines = 0;
int numberWords = 0;
void numberOfLines(){
cout<<"number of lines is " << numberLines << endl;
}
void numberWords(){
cout << "number of words is " << numberWords <<endl;
}
int main(){
string line;
char a = '';
ifstream myfile("files.txt");
if(myfile.is_open()){
while(!myfile.eof()){
getline(myfile,line);
cout<< line << endl;
numberLines++;
}
if ( a == ' '){
NumberWords++;
}
}
myfile.close();
}
numberOfLines();
numberOfWords ();
}
What you can do is add a 3rd argument to getline(). This lets it pull data from the stream until it hits a character. Doing getline(cin, line, ' ')takes all the data until the next and puts it into line. Your code might look like:
while(getline(inFile, line))
{
++numlines;
stringstream lineStream(line);
while(getline(lineStream, line, ' '))
{
++numWords;
}
}
The outer loop goes through the file and stores each line into line, then the inner goes through that line and counts each space. which correlates to a word.

Enter a string and divide it in words [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I need to code a program based which can compare words of a sentence or paragraph to a database, as if it were a text corrector. My problem here is that I have to enter on the console the text I want to correct as a string and then divide it in words stored in a vector of strings in C++. I tried a thousand ways but I cannot get it done.
Here is the code I last tried:
std::cout << "Enter the text: ";
std::string sentence;
std::vector<std::string> vText;
while (getline(std::cin, sentence)){
std::stringstream w(sentence);
std::string word;
while(w >> word)
vText.push_back(word);
}
When I execute this code, I got nothing, as if the program did nothing. Can you help me please?
This is the final thing (a piece):
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
int main(){
std::cout << "Introduzca una frase: ";
std::string frase;
std::vector<std::string> vTextoAnalizar;
while (getline(std::cin, frase)){
std::stringstream w(frase);
std::string palabra;
while(w >> palabra)
vTextoAnalizar.push_back(palabra);
}
for (int i=0;i<vTextoAnalizar.size();i++){
std::cout << vTextoAnalizar[i] << std::endl;
}
return 0;
}
Firstly, welcome to stack exchange. Your question has been down voted as you have not made a reasonable effort to ask a good question.
Please take particular note of How to create a Minimal, Complete, and Verifiable example.
I think what you are trying to do is something like this:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
std::vector<std::string> read_text() {
std::cout << "Enter the text: ";
std::string sentence;
std::string word;
std::vector<std::string> vText;
while(getline(std::cin, sentence)){
std::stringstream ss(sentence);
while ( getline( ss, word, ' ' ) ) {
if (word.compare("quit") == 0)
return vText;
vText.push_back(word);
}
}
}
int main() {
std::vector<std::string> test_vector = read_text();
std::cout << "Vector : " << std::endl;
for (int i=0;i<test_vector.size();i++){
std::cout << test_vector[i] << std::endl;
}
}
This will split on spaces, and add your words to the vector at the end of each sentence. I imagine that there are more intelligent ways of parsing, but this should get your test code working.
Hope that helps.

Understanding stringstream [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have never used stringstream before and was given a sample code but with no explanation of what was happening in the code. If someone could explain each line's purpose that would be great. I have looked in multiple places but cant seem to pin down the second line.
#include <sstream> // i know this line includes the file
stringstream ss(aStringVariable);// this line in particular
ss >> aVariable;
getline(ss, stringVariable2HoldValue, ‘|’);
There's a constructor for std::stringstream that takes a std::string as a parameter and initializes the stream with that value.
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::stringstream ss("foo bar");
std::string str1, str2;
ss >> str1 >> str2;
std::cout << "str1: " << str1 << std::endl;
std::cout << "str2: " << str2 << std::endl;
}
This code initializes a stringstream, ss, with the value "foo bar" and then reads it into two strings, str1 and str2, in the same way in which you would read from a file or std::cin.

convert a sentence to ascii code in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
i want to convert a sentence like 'good boy' to ascii code . i know the code that is a loop and print the ascii code of each character of sentence but i don't want this. i want that the ascii code of sentence (all characters alltogether) in long for example 1259788712..
You can use string to handle it.
#include <iostream>
#include <sstream> // use stringstream
using namespace std;
// turn int into string
string IntTOstring(int);
int main(void)
{
string sIn,sOut;
// input
sIn = "good boy";
sOut="";
for (int i=0 ; i<sIn.length() ; i++ ) {
// get one char from sIn each time
int temp=sIn.c_str()[i];
// turn int into string & save in sOut
sOut += IntTOstring(temp);
}
cout << sOut << endl;
return 0;
}
// use stringstream to convert int to string
string IntTOstring(int i){
stringstream ss;
ss << i;
return ss.str();
}

Represent a file with 0x00 as a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I want to represent my file as a string but I have a problem the file contains a 0x00 and my string gets terminated right there.How to solve this?
If you can't use the null as the termination character you have very few options:
a) write the string size prior to the string:
005Hello
011Hello\0World
b) use fixed length strings
c) prepend non-terminating nulls with a special char like '\'. If '\' appears in your string, write it twice "\". Reverse the logic when reading them back.
I have a txt file like this in binary 0x61 0x61 0x61 0x00 0x62 0x62 0x62
"txt file" in binary ? - I don't know what does it mean .
But if you have values separated by spaces you can try using std::vector of std::string
(which doesn't use null termination)
std::ifstream fin("input.txt");
std::vector<std::string> v;
std::copy(std::istream_iterator<std::string> (fin),
std::istream_iterator<std::string> (),
std::back_inserter(v) );
std::vector<std::string>::iterator it =v.begin();
for(;it!=v.end();++it)
std::cout<< *it<<" ";
Be sure to read your file in binary mode:
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
std::string read_file_as_string(const char* filename) {
std::ifstream input(filename, std::ios::binary);
if (!input) {
std::perror("Great failure");
std::exit(1);
}
std::stringstream contents;
contents << input.rdbuf();
return contents.str();
}
int main() {
std::string s = read_file_as_string("Derp.exe");
std::cout << "s.size() = " << s.size() << '\n';
for(unsigned char c : s) {
std::cout << std::hex << static_cast<unsigned int>(c) << ' ';
}
std::cout << '\n';
}