将字符串乘以 const int C++
Multiplying string by a const int C++
#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;
}
我正在尝试将 userMiles(字符串)乘以 stepsPerMile(const int),但我不断收到错误消息,提示它们与操作数类型不匹配。我必须将 stepsPerMile 用作 const int 并且无法更改它。如何更改我的代码以允许将这两个输入相乘?
读入 userMiles
和 userSteps
作为整数,而不是字符串。 operator>>
可以读取许多不同的类型,而不仅仅是 std::string
。尝试使用 int
来匹配您的 stepsPerMile
常量,例如:
#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;
}
一个不错的程序我检查过了。很酷的节目,我喜欢。我想解决这个问题。不要将 userMiles
设为字符串,而是将其设为像这样的 int
#include <iostream>
#include <string>
using namespace std;
int main() {
//declaring cin inputs
string userName;
int 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;
}
只需更改您的标识符
int userMiles, userMiles;
因为在 C++ 中你不能对字符串使用乘法运算符,这实际上可以用在其他编程语言中,例如 python 比如当你将一个字符串乘以一个数字时,字符串将重复但是在 C++ 中,您不能对字符串使用算术运算符,但“+”除外,它可用于连接 2 个或更多字符串。
This is a c++ course for beginners check it hope it helps
#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;
}
我正在尝试将 userMiles(字符串)乘以 stepsPerMile(const int),但我不断收到错误消息,提示它们与操作数类型不匹配。我必须将 stepsPerMile 用作 const int 并且无法更改它。如何更改我的代码以允许将这两个输入相乘?
读入 userMiles
和 userSteps
作为整数,而不是字符串。 operator>>
可以读取许多不同的类型,而不仅仅是 std::string
。尝试使用 int
来匹配您的 stepsPerMile
常量,例如:
#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;
}
一个不错的程序我检查过了。很酷的节目,我喜欢。我想解决这个问题。不要将 userMiles
设为字符串,而是将其设为像这样的 int
#include <iostream>
#include <string>
using namespace std;
int main() {
//declaring cin inputs
string userName;
int 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;
}
只需更改您的标识符
int userMiles, userMiles;
因为在 C++ 中你不能对字符串使用乘法运算符,这实际上可以用在其他编程语言中,例如 python 比如当你将一个字符串乘以一个数字时,字符串将重复但是在 C++ 中,您不能对字符串使用算术运算符,但“+”除外,它可用于连接 2 个或更多字符串。 This is a c++ course for beginners check it hope it helps