cin >>"现在的a是">> a >> b >> c >> str;为什么报错了
时间: 2023-08-07 13:05:37 浏览: 128
这段代码报错是因为 `cin` 无法直接将字符串输入变量。正确的写法应该是将字符串输入给 `str`,然后依次输入整型变量 `a`、`b`、`c`。
正确的代码应该是:
```cpp
cin >> str >> a >> b >> c;
```
这样就可以正确地将字符串输入给 `str`,并且依次输入整型变量 `a`、`b`、`c`。
相关问题
写出3组Plus函数的声明与实现,类型分别为int,double,string。 主函数如下,注意只需要实现Plus函数和包含相关的头文件。 int main() { int n; int a,b; double c,d; std::string str1,str2; std::cin>>n; while(n--) { std::cin >> a >> b ; std::cin >> c >> d; std::cin >> str1 >> str2; std::cout << myplus(a, b) << endl; std::cout << myplus(d, c) << endl; std::cout << myplus(str1, str2) << endl; } return 0; } Sample Input 1 2 3 2.5 3.5 women day Sample Output 5 6 womenday Hint PS:Please add std:: before using the string type.
int类型的声明与实现:
声明:
```
int myplus(int a, int b);
```
实现:
```
int myplus(int a, int b)
{
return a + b;
}
```
double类型的声明与实现:
声明:
```
double myplus(double a, double b);
```
实现:
```
double myplus(double a, double b)
{
return a + b;
}
```
string类型的声明与实现:
声明:
```
std::string myplus(std::string str1, std::string str2);
```
实现:
```
std::string myplus(std::string str1, std::string str2)
{
return str1 + str2;
}
```
完整代码如下:
```
#include <iostream>
#include <string>
int myplus(int a, int b)
{
return a + b;
}
double myplus(double a, double b)
{
return a + b;
}
std::string myplus(std::string str1, std::string str2)
{
return str1 + str2;
}
int main()
{
int n;
int a, b;
double c, d;
std::string str1, str2;
std::cin >> n;
while (n--)
{
std::cin >> a >> b;
std::cin >> c >> d;
std::cin >> str1 >> str2;
std::cout << myplus(a, b) << std::endl;
std::cout << myplus(d, c) << std::endl;
std::cout << myplus(str1, str2) << std::endl;
}
return 0;
}
```
当我将22通过cin输入给a时,为什么a是2
当你使用 `cin` 从标准输入流中读取数据时,它会将输入的数据按照空格、制表符或换行符进行分割,然后将其存储到对应的变量中。
所以,当你输入 `22` 时,它会被视为一个字符串,接着 `cin` 会尝试将其转换为 `int` 型数值。由于字符串 `"22"` 可以被转换为整数 `22`,所以 `cin` 会将其存储到变量 `a` 中。
如果你想将多个整数从一行输入到多个变量中,可以使用空格或制表符进行分隔。例如:
```c++
int a, b;
cin >> a >> b;
```
这样就可以将一行输入中的前两个整数分别存储到变量 `a` 和 `b` 中。如果你想将换行符也作为分隔符,可以使用 `getline` 函数。例如:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
int a, b;
string line;
getline(cin, line);
sscanf(line.c_str(), "%d %d", &a, &b);
cout << a << " " << b << endl;
return 0;
}
```
这里使用 `getline` 函数将一行输入读入到字符串 `line` 中,接着使用 `sscanf` 函数将字符串中的两个整数解析出来,并存储到变量 `a` 和 `b` 中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)