where it says "numOrdered = (int)input;" It shows me an error in Dev C++ that says " `struct std::string' used where a `int' was expected". How do I fix this. Not sure what I'm doing wrong. Thanks!
Heres the code:
using namespace std;
int main()
{
int maxOrder[] = {10, 50, 100, 99999};
int shipCharge[] = {10, 15, 5, 0};
string input;
int numOrdered;
cout << "Enter the number ordered(or stop to end the program): ";
cin >> input;
while (input != "end")
{
numOrdered = (int)input;
if (numOrdered <= maxOrder[0])
cout << "Shipping charge is" << shipCharge[0] * numOrdered << endl << endl;
else if (numOrdered <= maxOrder[1])
cout << "Shipping charge is" << shipCharge[1] * numOrdered << endl << endl;
else if (numOrdered <= maxOrder[2])
cout << "Shipping charge is" << shipCharge[2] * numOrdered << endl << endl;
else if (numOrdered <= maxOrder[3])
cout << "Shipping charge is" << shipCharge[3] * numOrdered << endl << endl;
cout << "Enter the number ordered(or type end to close the program): ";
cin >> input;
}
system("pause");
return 0;
}
Copyright © 2024 QUIZLS.COM - All rights reserved.
Answers & Comments
You should declare input as an int, not as a string. You are trying to assign a string value (input) to an int variable (numOrdered).
Unless you are trying to cast the input value as an int with the (int)input syntax, in which case you've done it wrong. But you probably shouldn't be doing that anyway. Declare input as an int and check it for valid input. (Never trust user input)
cast int to a string like that doesn't work in C++ too well. Look up atoi. which is string to integer.
http://cplusplus.com/reference/clibrary/cstdlib/at...