So my assignment has once again brought me here because I am not able to figure this one out. The goal of the task is to return a short of the result that comes from the expression in the c-string style object(which is of my own type), it doesn't make too much sense but the code helps clear it up. I won't post my entire assignment here but I will post what I have written of the member function now. If anyone could point me in the right direction of where to start or anything like that, it would be greatly appreciated.
This is the call
1 2
myType t1 = "843 add 7";
short answer1 = t1;
And this is what i have so far, but I don't think its anywhere near right
// my first program in C++
#include <iostream>
#include <string>
struct myType {
std::string line;
myType( constchar* w ) : line{w} {
}
operator size_t() const {
return line.size();
}
};
int main()
{
myType expression = "Hello World!";
size_t s = expression;
std::cout << s << '\n';
}
You have a string. An "expression". You assume that the fifth character is first char of the name of operator. Is it in:
1 add 1
No.
One could create a stringstream from the string and attempt to read number word number (formatted input) from that stream.
Or, assuming single whitespaces, seek those whitespaces to determine the substrings that ought to be numbers or operator names. Then convert putative numbers to integers.
The string seems to be of the format <number><white-space><operator><white-space><number>. I'd suggest you first parse into 3 elements. Then check that operator is valid. If white space is required to separate the elements, then this is quite easy - as keskiverto says above - using a stringstream and extraction. If white space is not required (optional) (which it really isn't requirded in this case), then the parsing is slightly more involved to extract the operator and second number.
what do YOU think the loop at line 8 does?
if that is what you do not know how to do -- c++ has multiple ways to text-to-int. atof, stoi, fromchars, stream, probably more not even counting C's scan family.
if you must DIY, iterate the string backwards until you hit special characters... probably using isdigit()
so
"843 add 7";
it will find the 7, boring
crunches for second number, finds 3. add to int.
finds 4, multiply int by 10 and add 4 (43 now).
finds 8, multiply int by 10 and add 8 (843)
runs out of string, stops.
you should not need to manually add a 0 char. But if you are doing C style strings directly, maybe...?? Did you do something that would have lost the zero?
short myType::Calculate(myType& Math)
{
Math[ Math.size()+1 ] = '\0';
Yes, that is interesting.
* The class myType has member function size()
* The class myType has operator[]
* The class myType has some container, probably array, that has memory for at least size()+1 char's.
Why does function "Calculate", which apparently should compute a number, modify the Math object at all?
The rest of function does not access the element(?) Math[ Math.size()+1 ] nor seem to rely on null-termination of C-string. Writing that '\0' has no apparent purpose.
Logically, the construction of object should ensure that the data within object is consistent to begin with. Naturally, each method that does modify the data should see that the data remains consistent.