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.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I know some people will say this question is repeated but I really couldn't find a useful answer.
Let's say I have the following program:
#include <iostream>
#include <string>
using std::cout;
int main(){
std::string something;
cout<<"Type something";
std::cin>>something;
}
How can I use setw() so the output will look like this?
Type something "then after some whitespaces for example 10 the user will start typing"
I tried to use setw() in output:
#include <iostream>
#include <string>
#include <iomanip>
using std::cout;
int main(){
std::string something;
cout<<std::left<<std::setw(24)<<"Type something";
std::cin>>something;
}
The expected output was supposed to be:
The actual output is:
I can't reproduce what you say, the code you have showed works fine for me.
#include <iostream>
#include <string>
#include <iomanip>
int main(){
std::string something;
std::cout << std::left << std::setw(24) << "Type something"; // prints "Type something "
std::cin >> something;
return 0;
}
That said, you could simply output a string with the desired number of spaces:
#include <iostream>
#include <string>
int main(){
std::string something;
std::cout << "Type something ";
// alternatively:
// std::cout << "Type something" << std::string(10, ' ');
std::cin >> something;
}
One possible solution might be using any special character after setw
Sample Code:
int main()
{
std::string something;
cout<< "Type something" << setw(24) << ":";
std::cin>>something;
}
Output:
Type something :inputString
References:
iomanip setw() function in C++ with Examples
Setw C++: An Ultimate Guide to Setw Function
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I recently started learning C++. I must create a program that asks the user for their name and a file name after; then displays the file name with type (eg .cpp) 1st and the name after. Must be able to accept negative values.
I'm using repl.it to write the code 1st then pasting in to a .cpp file to compile. Then will compile it with the makefile using the command make hello and check that the generated program compiles and runs correctly.
Sample run:
What's your name? John
What's the name of the output file? gen
gen.cpp:
#include <iostream>
int main() {
std::cout << "Hello John!\n";
}
Attempt:
#include <iostream>
using namespace std;
int main ()
{
string a;
cout << "What's your name?\n";
string b;
cout<< "Whats the name of this file?";
getline (cin,b);
cout<<"//"<<b;
getline (cin, a);
cout << "Hello " << a;
return 0;
}
Except it displays in the console:
What's your name?
Whats the name of this file? //e.g.c
//c
The output is wrong, so what mistake did I make? What's the correct method to get the expected values?
Edit: This is what the assignment says:
As mentioned in comments, your calls to getline() are in the wrong places. Try this instead:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string username;
cout << "What's your name?\n";
getline (cin, username);
string filename;
cout << "Whats the name of this file?";
getline (cin, filename);
cout << "//" << filename << ".cpp" << endl;
cout << "Hello " << username << endl;
return 0;
}
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 3 years ago.
Improve this question
I am trying to read values from a .txt file to a vector (which is a member of a class) in C++, but despite the .txt having around 1000 lines, the vector is of size 0. I inserted a 'cout', and I know the file is opened and closed. I'm not sure what I could be doing wrong for the code not to read the contents of the .txt.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include "option_class.h"
using namespace std;
int main(){
double cprice = 0.0;
int i = 0;
string line;
ifstream is;
is.open("/Users/<USER>/Desktop/SPY.txt");
if (!is){
cout << "Unable to open file" << endl;
return(0);
}
while(!getline(is, line).eof()){
is >> cprice;
option1.price.push_back(cprice);
}
is.close();
cout << "Closing file" << endl;
}
Have you tried something simpler:
while (is >> cprice)
{
option1.price.push_back(cprice);
}
The operator>> will skip whitespace, which includes newlines. There is no need to read a line at a time.
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.
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