#include <iostream>
#include <string>
using namespace std;
int main() {
//declaring cin inputs
string userName;
string userMiles;
string userSteps;
//declaring constant for number of steps/mile
const int stepsPerMile = 2000;
//getting user's name
cout << "What is the user's name?";
cin >> userName;
cout << endl;
//getting miles walked
cout << "How many miles did " << userName << " hike today?";
cin >> userMiles;
cout << endl;
//getting other steps taken
cout << "How many other steps did " << userName << " take today?";
cin >> userSteps;
cout << endl;
string stepsTakenFromMiles;
string totalSteps;
stepsTakenFromMiles = userMiles * stepsPerMile;
totalSteps = stepsTakenFromMiles + userSteps;
cout << userName << " took " << totalSteps << " steps throughout the day." << endl;
return 0;
}
I am trying to multiply userMiles (string) by stepsPerMile (const int) and I keep getting an error that they are not matching operand types. I have to use stepsPerMile as a const int and cannot change it. How do I change my code to allow both of these inputs to be multiplied?
Read in userMiles
and userSteps
as integers, not as strings. operator>>
can read in many different types, not just std::string
. Try using int
instead, to match your stepsPerMile
constant, eg:
#include <iostream>
#include <string>
using namespace std;
int main() {
//declaring cin inputs
string userName;
int userMiles;
int userSteps;
//declaring constant for number of steps/mile
const int stepsPerMile = 2000;
//getting user's name
cout << "What is the user's name?";
cin >> userName;
cout << endl;
//getting miles walked
cout << "How many miles did " << userName << " hike today?";
cin >> userMiles;
cout << endl;
//getting other steps taken
cout << "How many other steps did " << userName << " take today?";
cin >> userSteps;
cout << endl;
int stepsTakenFromMiles;
int totalSteps;
stepsTakenFromMiles = userMiles * stepsPerMile;
totalSteps = stepsTakenFromMiles + userSteps;
cout << userName << " took " << totalSteps << " steps throughout the day." << endl;
return 0;
}