Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
I would like to parse a sentence beginning with a number:
2 random sentece.
5 another one.
8 this is really long sentence.
Into int number holding the beginning number and the rest in std::string sentence. So in the first line, the parsing output will be number == 2 and sentence == "random sentence". The input is read from stdin, but the classical std::cin >> number >> sentence does'n work, since the parsing of string would end once it reaches a space. But I want to make the string beginning after the initial number to the end of line \n. So, how to do it in C++?
You can make use of std::getline and std::istringstream as shown below. In particular in the given program, std::getline is used to read line by line and std::istringstream is used to read first the integer and then the remaining sentence.
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string sentence;
int inputInt = 0;
//read line by line
while(std::cin >> inputInt && std::getline(std::cin, sentence))
{
std::cout<< inputInt <<"-----"<<sentence<<std::endl;
//do the check here
}
}
Demo
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a program that takes in one integer and two strings from a text file "./myprog < text.txt" but I want it to be able to do this using command line arguments without the "<", like "./myprog text.txt" where the text file has 3 input values.
3 <- integer
AAAAAA <- string1
AAAAAA <- string2
If the reading of the parameters must be done solely from the file having it's name, the idiomatic way is, I would say, to use getline().
std::ifstream ifs("text.txt");
if (!ifs)
std::cerr << "couldn't open text.txt for reading\n";
std::string line;
std::getline(ifs, line);
int integer = std::stoi(line);
std::getline(ifs, line);
std::string string1 = line;
std::getline(ifs, line);
std::string string2 = line;
Because there are little lines in your file, we can allow ourselves some repetition. But as it becomes larger, you might need to read them into a vector:
std::vector<std::string> arguments;
std::string line;
while (getline(ifs, line))
arguments.push_back(line);
There some optimizations possible, as reusing the line buffer in the first example and using std::move(), but they are omitted for clarity.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How to implement a command line interface, the command must end with semicolon. Then press enter to execute. Otherwise, press enter wraps the line. If I don't descirbe it clearly, you can refer to the mysql command line.
How to implement the above in C++? For example:
If user inputs foo;bar then str = "foo". It can have some spaces in between ;.
In C++ IO I just know:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cin >> str;
}
I don't know how to implement other input function.
The most simple approach would be to use std::getline (as adviced in comments), but with a custom delimiter (';' in your case) like this:
string command;
while (getline(cin, command, ';')) {
// process the command there
}
However, this approach has several drawbacks and is pretty limited:
it reads until any ';' is hit. If you're going to process commands complicated enough to support string literals, then you will need more complicated parsing to handle this: echo "Hello; sample text"; exit;, as two commands, but not three;
when you hit Enter, getline will wait for more input until it sees a semicolon, but it will not insert any 'user-friendly' prompt like > to let the user know that they need to supply more input or that they forgot the semicolon at the end of command.
If you're ok to go without supporting these features, getline is quite good to go. Otherwise you'll need to parse your input lines by yourself.
I guess this is what you want to do:
#include <iostream>
int main() {
std::string command;
bool flag = true;
do
{
std::string str;
std::getline(std::cin,str);
for(int i = 0; i < str.length(); i++)
{
if(str[i] == ';')
{
str = str.substr(0,i+1);
flag = false;
}
}
command += str;
} while(flag);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I receive text with special characters (such as á) so I have to manually search and replace each one with code (in this case "á")
I would like to have code to search and replace such instances automatically after user input. Since I'm a noob, I'll show you the code I have so far - however meager it may be.
// Text fixer
#include <iostream>
#include <fstream>
#include <string>
int main(){
string input;
cout << "Input text";
cin >> input;
// this is where I'm at a loss. How should I manipulate the variable?
cout << input;
return 0;
}
Thank you!
An easy method is to use an array of substitution strings:
std::string replacement_text[???];
The idea is that you use the incoming character as the index into the array and extract the replacement text.
For example:
replacement_text[' '] = " ";
// ...
std::string new_string = replacement_text[input_character];
Another method is to use switch and case to convert the character.
Alternative techniques are a lookup table and std::map.
The lookup table could be an array of mapping structures:
struct Entry
{
char key;
std::string replacement_text;
}
Search the table using the key field to match the incoming character. Use the replacement_text to get the replacement text.
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 8 years ago.
Improve this question
How could I access a text file and go through word by word. I understand how to open the file but just not how to pull out each word one by one. I think it has something to do with arrays?
Simply:
#include <fstream>
#include <iostream>
int main()
{
std::fstream file("table1.txt");
std::string word;
while (file >> word)
{
// do whatever you want, e.g. print:
std::cout << word << std::endl;
}
file.close();
return 0;
}
word variable will contain every single word from a text file (words should be separated by space in your file).
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
I am having memory trouble with my code, and figured out that my code was being read wrong. For example the last value is adding numbers, not sure why. Also the names aren't coming out right.
This is what the output is looking like:
4101,BRAEBURN02.07682e-3172.07691e-317
4021,DELICIOUS02.07682e-3172.07691e-317
4020,DELICIOUS02.07682e-3172.07691e-317
4015,DELICIOUS02.07682e-3172.07691e-317
4016,DELICIOUS02.07682e-3172.07691e-317
4167,DELICIOUS02.07682e-3172.07691e-317
4124,EMPIRE,1,1.14,145.202.07682e-3172.07691e-317
4129,FUJI02.07682e-3172.07691e-317
4131,FUJI02.07682e-3172.07691e-317
As you can see the Empire was separated properly with the exception of the the last value.
Here's my code: the cout part was just for my personal use to see if the values were being inputted properly.
int main()
{
string name;
double price;
int by_weight;
double inventory;
int plu_code;
ifstream infile;
infile.open("inventory.csv");
while(!infile.eof())
{
stringstream ss;
string line = "";
getline(infile,line);
Tokenizer tok(line, ",");
ss << line;
ss >> plu_code >> name >> by_weight >> price >>inventory;
cout << plu_code<<"" <<name<<"" << by_weight<<"" << price <<""<<inventory<<"\n";
table[plu_code] = new Product(plu_code, name,by_weight, price,inventory);
numProducts++;
}
return 0;
}
The Empire line works because it's the only one whose name contains no whitespace. When you read strings from a stream, they are delimited by whitespace, so you only get the first word. If there are more words after that, then reading a double or other numeric type will fail because the stream is still pointing at non-numeric characters. You are not testing that your input operations succeeded, but it should have been obvious from your output.
I'm not sure what effect your Tokeniser class is supposed to have here. But perhaps have a look at this SO question for tokenising commas out of the stream. You can use a single getline call with a comma delimiter to read the name, and then normal << operator for the others.
[Edit] In fact, after cleaning up your question layout I notice that the Empire line doesn't work. It's reading the rest of the line as the name, and then still outputting uninitialised values. Which suggests to me your Tokeniser doesn't do anything at all.