Thanks to your tutorials, I've been able to teach my self how to do a simple program that solves the determinative of a matrix (though I can't get it to work with negative numbers).
Now I'm on to strings. I want the user to assign numbers to variables in this manner:
So far, I've written this (substituting user input with string command):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
usingnamespace std;
string strplc(string mine,char let,int len) {
int place = str.find (let);
string joe = str.substr (let,len);
return joe;
}
int main() {
string command = "Gloss:1,Pos:2,Root1:3";
string glosss = strplc(glosss,"G",7);
int grow = glosss[6];
cout << grow;
return 0;
}
This should return 1. This is what happens instead:
1 2 3 4 5 6
prog.cpp: In function ‘std::string strplc(std::string, char, int)’:
prog.cpp:7: error: ‘str’ was not declared in this scope
prog.cpp:7: warning: unused variable ‘place’
prog.cpp: In function ‘int main()’:
prog.cpp:14: error: invalid conversion from ‘constchar*’ to ‘char’
prog.cpp:14: error: initializing argument 2 of ‘std::string strplc(std::string, char, int)’
The first problem is that in the function strplc, you are using an object called str; that object has never been defined. It does not exist. If you want to use the 'find' function in the string class, you must create a string. I see that you are passing in a std::string called 'mine'. Perhaps that is the string you mean to be using, in which case you should replace 'str' with 'mine'. You do the same thing on line 8, where you call 'str.substr'; str does not exist, and you cannot call the member function of an object that does not exist. Perhaps you mean 'mine.subst'; you should also note that the function subst does not take a character as its first parameter; you are trying to pass a char to it, which is wrong.
I see that you have created an int object called 'place' that you never use for anything. If you're not using it, why is it there?
Also, you have created a function that accepts a char as its second parameter, and then you are trying to pass it something that is not a char; you are trying to pass it a pointer to a const char. If you want to use the function, what you pass it must match the parameters it accepts.
I see also that on line 14 you are passing into the strplc function something called glosss; I notice that at this point, you've not actually put anything in that string and given that you're trying to create it from itself (that it, you've said that the string glosss equals something that you're trying to make by using the glosss that you haven't even made yet); I suspect you don't actually mean to do that either.
string strplc(string mine,char let,int len) {
int place = mine.find(let);
string joe = str.substr (place,len);
return joe;
}
notice find is called on 'mine' which is your string object, and in the substr call where you had 'let' it should be 'place' which is the position of the first occurence of the char sent into the function, not the char itself. You should do some checking in this function to make sure that find() succeeds too. If it fails, it returns npos.
Take a look at the function atoi. That turns a char into an int. However, since you've got a std::string, I suspect you can find a safer and better way than this.