So I have this piece of code:
ifstream sFile;
sFile.open(argv[1]);
stringstream ss;
unsigned char aChar;
string aString;
while (sFile >> noskipws >> aChar) {
ss << noskipws << aChar;
}
ss >> noskipws >> aString;
cout << noskipws << aString << endl;
My file contains:
"SHE SMELLS"
In stdout, all I see is "SHE". I've wasted so much time trying to figure this out, can someone help me understand why this is happening?
Also, will this be able to read in characters from the extended ascii alphabet?
Look at this page for noskipws:
http://www.cplusplus.com/reference/iostream/manipulators/noskipws/
In the example it shows the problem you are having, it is fixed by using:
iss.seekg(0);
This should work if you do this:
sFile.seekg(0);
before your while loop.
Hope that is right / helps.
Related
I can get the file to save right but I can't seem to get multiple words to write to .txt, like if I type "Hi purple" it just writes "Hi", here is code
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
system("color F0");
string name0;
cout << "Please enter a file name, no spaces/special characters" << endl;
cin >> name0;
name0 = name0+".txt";
system("cls");
cout << " FISHSOFT" << endl;
cout << "The best text editor in the world" << endl << endl;
string text;
cin >> text;
ofstream myfile;
myfile.open (name0.c_str() , ios::out | ios::trunc);
myfile << text;
myfile.close();
system("PAUSE");
return 0;
}
Use std::getline to read an entire line of input including spaces.
cin >> text; will read one whitespace delimited token from the input stream. One word in == one word out.
std::getline(cin, text); will read a whole line. Reading more than that is tricky, but typically a loop around multiple calls to getline.
Suggestion: Save yourself time and fire up your IDE's debugger to see what's happening in your code before posting a question. Almost always faster, and if not, you can make much better, tighter-focused questions.
std::cin is able to get several parameters at once.
That mean you may write:
std::cin >> name0 >> name1 >> name2;
// input: a1 a2 a3
// make: name0: a1, name1: a2, name3:a3
By default, the space is the separator between parameters.
To avoid this behavior, you could use getLine:
std::getline(std::cin, name0);
There are 2 ways to get the string with spaces and special character.
cin.getline(name);
gets(name);
Hope this will serve your purpose.
I am working on a trivial problem, but I cant figure it out. The program should put out the part after "simon says" if the beginning of str1 is "simon says". if i run it like in the following code, it works, but if i enter the string over cin >> str1; myself, it doesnt. Does anybody have a tip for me? (and yes, this is a kattis problem)
int main()
{
string str1("simon says write a program");
//cin >> str1;
string str2 ("simon says");
if (str1.compare(0,10,str2,0,10) == 0){
cout << str1.substr(11,str1.size());
}
return 0;
}
It's because std::cin gets whitespace-separated strings. If you will try to read a string from the standard input using
std::cin << str1;
// something here
std::cin << str2;
And you will enter "simon says", "simon" will land in the str1 and "says" will go to the str2. To read a whole line you should use
std::getline()
With using cin >> str1, if you print str1 for the sentence "Simon write something", you will see that str1 has the value "Simon".
To not cut the sentence to the first space, Replace cin >> str1 by getline(cin, str1)
I'm trying to insert white space in std::stringstream in this way.
std::stringstream sstr;
sstr.str("");
sstr << " ";
sstr << 10;
and then setting it as a label like that
label->setString(sstr.str().c_str());
but it's only giving me 10, space is not included. I've followed many links to solve problem but of no use. Following link suggests to use getline() but in my case I cannot do that :
stringstream doesn't accept white space?
I've also tried to use std::noskipws but it also not work :
sstr << std::noskipws << " ";
Any help will be appreciated.
std::stringstream should not remove your whitespace when used like this. Are you sure that it is not the label-object that is trimming your string and removing the whitespace?
Try debugging or printing out your string before setting it to the label.
You can use put method to append a single char:
std::stringstream sstr;
sstr.str("");
sstr.put(' ');
sstr.put(10);
I think you're using it wrong:
string labelstr;
std::stringstream sstr;
sstr.str("");
sstr << " ";
sstr << 10;
ss >> noskipws >> labelstr;
label->setString(labelstr);
http://www.cplusplus.com/reference/ios/noskipws/
Your code works as expected for me. Here's a runnable example: http://cpp.sh/8d6.
The culprit must be setString trimming your input. Or possibly some other function that reads the string later.
This is the code in the main() portion of the program:
int numFiles;
cout << "How many signal files are there?";
cin >> numFiles;
char singalFiles[numFiles][100];
string backgroundFile;
for (int i=0;i<numFiles;i++){
string singalFile;
cout << "Please input the name of singal file" << i << ".";
cin >> singalFile;
singalFile >> char singalFiles[i][100];
string backgroundFile;
cout << "Please input the name of background file" << i << ".";
cin >> singalFile;
backgroundFile >> char backgroundFiles [i][100];
}
This is the code I am writing as part of a research project. I was wondering if somebody could help me with this. I am very new to c++ and do not know how to get the strings to write into a char array.
I am having trouble reading the strings into the char array so they can be stored there. That is, I am trying to read each of the strings called backgroundFile and signalFile into the char arrays backgroundFiles and singalFiles.
The definition char singalFiles[numFiles][100]; may be an issue as standard C++ requires the size of an array be constant. Some compilers accept this as an extension, but you shouldn't rely on it.
But as easy alternative, you can use vectors and strings:
vector<string> singalFiles(numFiles);
Then you can easily read the data:
//cin >> singalFile; ==> combine with the next line
// singalFile >> char singalFiles[i][100];
cin >> singalFiles[i];
You don't even have to reserve for the size in advance. You could as well do:
vector<string> singalFiles; // the size of a vector is dynamic anyway !
...
cin >> singalFile; // as you did before
signalFiles.push_back(signalFile); // add a new element to the end of the vector.
This question already has answers here:
Need help with getline() [duplicate]
(7 answers)
Closed 7 years ago.
This is probably a very simple problem but forgive me as I am new.
Here is my code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string name;
int i;
string mystr;
float price = 0;
cout << "Hello World!" << endl;
cout << "What is your name? ";
cin >> name;
cout << "Hello " << name << endl;
cout << "How old are you? ";
cin >> i;
cout << "Wow " << i << endl;
cout << "How much is that jacket? ";
getline (cin,mystr);
stringstream(mystr) >> price;
cout << price << endl;
system("pause");
return 0;
}
The problem is that when asked how much is that jacket? getline does not ask the user for input and just inputs the initial value of "0". Why is this?
You have to be careful when mixing operator>> with getline. The problem is, when you use operator>>, the user enters their data, then presses the enter key, which puts a newline character into the input buffer. Since operator>> is whitespace delimited, the newline character is not put into the variable, and it stays in the input buffer. Then, when you call getline, a newline character is the only thing it's looking for. Since that's the first thing in the buffer, it finds what it's looking for right away, and never needs to prompt the user.
Fix:
If you're going to call getline after you use operator>>, call ignore in between, or do something else to get rid of that newline character, perhaps a dummy call to getline.
Another option, and this is along the lines of what Martin was talking about, is to not use operator>> at all, and only use getline, then convert your strings to whatever datatype you need. This has a side effect of making your code more safe and robust. I would first write a function like this:
int getInt(std::istream & is)
{
std::string input;
std::getline(is,input);
// C++11 version
return stoi(input); // throws on failure
// C++98 version
/*
std::istringstream iss(input);
int i;
if (!(iss >> i)) {
// handle error somehow
}
return i;
*/
}
You can create a similar function for floats, doubles and other things. Then when you need in int, instead of this:
cin >> i;
You do this:
i = getInt(cin);
Its because you have a '\n' left lying on the input stream from a previous call.
cin >> i; // This reads the number but the '\n' you hit after the number
// is still on the input.
The easiest way to do interactive user input is to make sure each line is processed independently (as the user will hit enter after each prompt).
As a result always read a line, then process the line (until you get familiar with the streams).
std::string line;
std::getline(std::cin, line);
std::stringstream linestream(line);
// Now processes linestream.
std::string garbage;
lienstream >> i >> garbage; // You may want to check for garbage after the number.
if (!garbage.empty())
{
std::cout << "Error\n";
}
Ignore some characters until line feed is reached.
cin.ignore(256, '\n')
getline (cin,mystr);