Reading numbers and unevaluated statements from a file [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 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

Related

How to compare a list of data from a text file to an array? [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 4 years ago.
Improve this question
I am making a program to calculate student's score. I have data stored inside a text file (fstream) that contains student id and their answer (True/False). Then, I want the program to compare the student's answer inside the file with the correct one (stored inside array) and if it is matched, score will +1 and if not score will -1. (Comparing T/F char by char)
So, for the correct answer of the test I store it inside array.
eg: char ans[5]={'T','F','T','F','T'}
How to compare each line of data inside the file (student's answer) with the array (correct answer)? I just need some hints, right now I don't even know how to start the code yet.
You can read files using file streams in <fstream>.
Once the file open, you can use >> exactly as you would do from the console.
You can then read the student's answers as a string (e.g."FFTTF"). To verify the results, you need to do loop, comparing successively each character of the string at index [i] with the item of your array at the same index.
Increment the score by one for every successful match and you ahve the score.
Note: I won't produce code in order not to spoil your homework. If you edit your question showing what you have tried, you'd get more responses

How to split a long input into multiple items 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'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

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.