How to split a long input into multiple items in C++? [closed] - c++

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 8 years ago.
Improve this question
I'm new to C++ and have been trying to code a program where I enter many things at once and it splits them into different strings, ints, etc. depending on what they are but I can't figure out how.
I want to be able to input, for example, "What is 7 plus 9?" and code it so it assigns the first number (7) to int a, the second number (9) to int b, recognize the word "plus" to be the operator. This would then go to a simple calculator program which I have already coded fine and would then output the answer.
How do I code it so it can split the entire input into multiple individual items? I was thinking there must be some function for a format of input in which case I could define input as string1, string2, int a, string 3, int b, and just have it know the start/end of each by the spaces. Anything you have in mind that could do this please let me know.
Thanks

ideally a lexer and parser like antlr or lex/yacc would be great but the learning curve is steep

Related

Typical array program in C++ [closed]

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 8 years ago.
Improve this question
I want to create a program in C++ that arranges inputted words in alphabetical order. Can any one help.
I tried arranging taking first letter using for loop but so far it gives me absurd result. I only want a little hint and will do the rest on own
Yes this is easy. The standard library does all the work for you. The string has an operator< which is compared lexicographically. So really, it's a waste of time. std::sort will call operator< on its parameters.
Warning: by default, lexicographically means it will compare the ASCII values. So the exclamation mark goes before numbers, and numbers go before capital letters, and capital letters go before lowercase letters.
string myWords[10];
for (int i = 0; i < 10; i++)
cin >> myWords[i];
sort(begin(myWords), end(myWords));
If you have a vector, just do v.begin() and v.end().

what does this stanford lirbrary mean? C++ simpio.h getInteger() [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 8 years ago.
Improve this question
i'm trying to understand this library that stanford uses for their CS course but i can't just use this feature in my CS course back in my home. How do i emulate this feature? i was wondering is this a getline(cin, integer)? or is this something to do with fstream? The reason being, i'm trying to go through the lecture series at UDEMY.
Here is the definition.
int getInteger(string prompt = "");
Reads a complete line from cin and scans it as an integer. If the scan
succeeds, the integer value is returned. If the argument is not a
legal integer or if extraneous characters (other than whitespace)
appear in the string, the user is given a chance to reenter the value.
If supplied, the optional prompt string is printed before reading the
value.
Usage:
int n = getInteger(prompt);
You can download simpio.h here: http://www.eecs.wsu.edu/~cs150/prog/libs.htm
If you can't or don't want to use the library, you could study its source code.

Count char occurs in string with length 10^200 [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 8 years ago.
Improve this question
I have a string on Linux standard terminal input. Its maximal length is 10^200 (as said in program specification). I have to count, how many "3" characters are inside it (occur in this string). I couldn't do that by for loop, because there is no so big variable type, which can be used as iterator. Is there any way to analyze so big strings?
Is there any way to analyze so big strings?
Not in this universe there is not. Such an entity cannot exist in this universe1 and that which does not exist, cannot be analyzed.
1 Current estimates of this universe's total number of particles particles are in the region of 1080.
As from your comment
The data source is standard terminal input.
Then you'll need a lot of monkeys to type this in.
Though you don't need to read in what's typed at once into a big string, but you can simply analyze char by char as typed. The std::istream::get(char_type& ch) method is suitable for doing so.

In C++ what's the easiest way for a user to enter data going into a 2D array? [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 8 years ago.
Improve this question
I want the program to take user input to fill a 2D array of ints and I'm not sure what the easiest format to have the user enter the data would be (easy in terms of programming).
For example in Java I could use .split() to separate integers by spaces or commas etc., but since a string can't easily be broken up in C++, this is a problem.
I don't want to prompt the user each time for each element such as:
enter integer for location 0, 0
enter integer for location 0,10
enter integer for location 2, 0
Here is a stackoverflow answer that talks about how to tokenize a string in C++
How do I tokenize a string in C++?
I haven't tried boost but strtok is pretty easy to use but may have some multithreading issues.

Reading numbers and unevaluated statements from a file [closed]

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 8 years ago.
Improve this question
I have a general file which constructs certain shapes and I would like to read in the lines:
0 0 1
0 x(r) y(r)
where x and y are functions of r, which is a variable taken in as an argument to main, such as 2*r-4.
Then I need to read the columns downwards into arrays.
For columns with just numbers you can do something like:
file >> x[j];
However I am unsure how to get the expressions to be read into the program and then be evaluated and then be put into an array as a number.
I am having problems reading in columns with entries of different types. Ideally I would like each to be assigned like:
temp[0]=1;
temp[1]=2*r-4;
but I am not sure how to do this.
(I am not sure whether the function fscanf would work?)
You can read an entire line with file.getline(someString, maxLength). Then you can split the string so you have strings for x(r) and y(r). These strings need to be parsed as mathematical expressions. This question has good information on the subject for c++: Convert string to mathematical evaluation