| cantdocplusplus (11) | |
|
hi | |
|
Last edited on
|
|
| Chervil (812) | |||
|
What this program is doing is to read the first seven values from the file. It then stores them into a particular location in the array. The value which has just been read is also displayed. The error is that depending upon the number chosen by the user, the same data will be stored in a different place. What it should be doing is to first read all of the data from the file, and place it into the corresponding rows and columns. Only after that has been completed should the user be asked which row they wish to see displayed. I don't know whether I explained that very well. The main point is that the number selected by the user should not take any part in the process of reading the data. It should only affect the output. Sample code:
| |||
|
Last edited on
|
|||
| cantdocplusplus (11) | |
|
Thank you this has been so helpful!!! I also wanted to multiply "value two" by another number and then divide it, however I keep getting the following error.. Error C2677: binary '*' : no global operator found which takes type 'std::string' (or there is no acceptable conversion) Do you know any way to get around this error? Thank you! | |
|
|
|
| Chervil (812) | |||
|
I'm glad that was useful. I didn't know what sort of data was in the input file, I just saw you had an array of strings. If you want to do any sort of calculations with the string, it needs to first be converted into a numeric type, such as double or int.There are several different ways to do that. You could use the functions atof or atoihttp://www.cplusplus.com/reference/cstdlib/atof/ http://www.cplusplus.com/reference/cstdlib/atoi/ The code might be something like this: double n = atof(str[1][ix].c_str());Note the use of .c_str() to convert the C++ string to a plain character string.You might instead use a stringstream.
I find stringstream very useful, it's worth becoming familiar with. http://www.cplusplus.com/reference/sstream/ Note - if all of the data in the text file consists of numbers, then you should probably not be using an array of strings to handle them. Right from the start, read and store each value as a number. (But if you have a mixture of strings and numbers, then there are several different ways to look at this). Hope this helps. | |||
|
Last edited on
|
|||