int main()
{
int x;
int z;
string y;
std::string mystr[y];
cout << "Enter first number";
cin >> x;
cout << "Calculations";
getline(cin, mystr[y]);
cout << "Enter your second number";
cin >> z;
if(mystr[y] == "addition")
{
cout << "Your answer is ";
cout << x + z;
return 0;
}
else if(mystr[y] == "subtract")
{
cout << "Your answer is ";
cout << x - z;
return 0;
}
else if(mystr[y] == "times")
{
cout << "Your answer is ";
cout << x * z;
return 0;
}
else if(mystr[y] == "division")
{
cout << "Your answer is ";
cout << z / x;
return 0;
}
}
I get this message:
..\Firstprogram.cpp:40:15: error: no match for 'operator[]' (operand types are 'std::string [1] {aka std::basic_string<char> [1]}' and 'std::string {aka std::basic_string<char>}')
else if(mystr[y] == "division")
string y;
std::string mystr[y];
// To
std::string mystr;
along with every other instance of mystr[y] and that part will be good. You do not need to prefix string with std:: either since you have usingnamespace std;
You will also have trouble with division due to integer division witch will truncate everything after the decimal place. There is still at least one other problem, but it will not stop your program from working as expected.
Thank you very much for repplying so quick! Ill try your suggestions. Also, i tried openning another program with no apparent bugs but it just instantly stopped working, any help please?