使用 cin ERROR 输入多个浮点数
Inputting multiple floats using cin ERROR
我想连续输入5个float
个值,但是程序运行不正常
#include <iostream>
using namespace std;
int main()
{
float v, i, vr, vl, vc, r, xl, xc, z;
for (int i = 1; i <= 9; i++)
{
cout << "Enter the values of v,i,vr,vl,vc" << endl;
cin >> v;
cin >> i;
cin >> vr;
cin >> vl;
cin >> vc;
cout << endl << v << " " << i << " " << vr << " " << vl << " " << vc << endl;
}
return 0;
}
如果我尝试输入 1.1 2.2 3.3 4.4 5.5 ,程序只接受四个值
输出为:
1.1 2 0.2 3.3 4.4
请告诉我我哪里出错了,我该如何更正我的代码。
您在外部范围内将 i
用作 float
,然后在内部范围内将 it
用作 int
。
所以当你输入
1.1 2.2 3.3 4.4 5.5
使用
cin>>v;
cin>>i;
cin>>vr;
cin>>vl;
cin>>vc;
从2.2
只取2
然后vr变量取0.2
因此变量值变为
v=1.1
i=2
vr=0.2
vl=3.3
vc=4.4
因此剩下 5.5 因为它需要 2.2 作为 2 个输入
Solution:
将 for
循环变量更改为 j
。
您的代码中存在变量名冲突:
float v, i, vr, vl, vc, r, xl, xc, z; // Here, variable "i" is declared as a floating-point variable
for (int i = 1; i <= 9; i++) // Here, "i" is declared again, this time as an int
当有多个同名变量,但在代码的不同范围内时,编译器会使用该范围内的局部变量。这导致了错误;您希望变量 i
存储值 2.2
输入,但是,最局部的变量是 for 循环的计数器 i
。因此,编译器尝试将值存储在计数器中。由于计数器是 int
类型,因此 2.2
被分解; counter i
存储 2,vr
存储 0.2
.
这就是为什么您的编译器只接受 4 个值;第二个值输入分为两个变量。
要更正此问题,请更改 for 循环的计数器变量的名称:
float v, i, vr, vl, vc, r, xl, xc, z;
for (int j = 1; j <= 9; j++) // The name of the counter variable is changed from "i" to "j"
{
cout << "Enter the values of v,i,vr,vl,vc" << endl;
cin >> v;
cin >> i;
cin >> vr;
cin >> vl;
cin >> vc;
cout << endl << v << " " << i << " " << vr << " " << vl << " " << vc << endl;
}
或者,将变量 i
(范围在 for 循环之外的变量)的名称更改为其他名称:
float v, num, vr, vl, vc;
for (int i = 1; i <= 9; i++)
{
cout << "Enter the values of v,j,vr,vl,vc" << endl;
cin >> v;
cin >> num;
cin >> vr;
cin >> vl;
cin >> vc;
cout << endl << v << " " << num << " " << vr << " " << vl << " " << vc << endl;
}
我想连续输入5个float
个值,但是程序运行不正常
#include <iostream>
using namespace std;
int main()
{
float v, i, vr, vl, vc, r, xl, xc, z;
for (int i = 1; i <= 9; i++)
{
cout << "Enter the values of v,i,vr,vl,vc" << endl;
cin >> v;
cin >> i;
cin >> vr;
cin >> vl;
cin >> vc;
cout << endl << v << " " << i << " " << vr << " " << vl << " " << vc << endl;
}
return 0;
}
如果我尝试输入 1.1 2.2 3.3 4.4 5.5 ,程序只接受四个值
输出为:
1.1 2 0.2 3.3 4.4
请告诉我我哪里出错了,我该如何更正我的代码。
您在外部范围内将 i
用作 float
,然后在内部范围内将 it
用作 int
。
所以当你输入
1.1 2.2 3.3 4.4 5.5
使用
cin>>v;
cin>>i;
cin>>vr;
cin>>vl;
cin>>vc;
从2.2
只取2
然后vr变量取0.2
因此变量值变为
v=1.1
i=2
vr=0.2
vl=3.3
vc=4.4
因此剩下 5.5 因为它需要 2.2 作为 2 个输入
Solution:
将 for
循环变量更改为 j
。
您的代码中存在变量名冲突:
float v, i, vr, vl, vc, r, xl, xc, z; // Here, variable "i" is declared as a floating-point variable
for (int i = 1; i <= 9; i++) // Here, "i" is declared again, this time as an int
当有多个同名变量,但在代码的不同范围内时,编译器会使用该范围内的局部变量。这导致了错误;您希望变量 i
存储值 2.2
输入,但是,最局部的变量是 for 循环的计数器 i
。因此,编译器尝试将值存储在计数器中。由于计数器是 int
类型,因此 2.2
被分解; counter i
存储 2,vr
存储 0.2
.
这就是为什么您的编译器只接受 4 个值;第二个值输入分为两个变量。
要更正此问题,请更改 for 循环的计数器变量的名称:
float v, i, vr, vl, vc, r, xl, xc, z;
for (int j = 1; j <= 9; j++) // The name of the counter variable is changed from "i" to "j"
{
cout << "Enter the values of v,i,vr,vl,vc" << endl;
cin >> v;
cin >> i;
cin >> vr;
cin >> vl;
cin >> vc;
cout << endl << v << " " << i << " " << vr << " " << vl << " " << vc << endl;
}
或者,将变量 i
(范围在 for 循环之外的变量)的名称更改为其他名称:
float v, num, vr, vl, vc;
for (int i = 1; i <= 9; i++)
{
cout << "Enter the values of v,j,vr,vl,vc" << endl;
cin >> v;
cin >> num;
cin >> vr;
cin >> vl;
cin >> vc;
cout << endl << v << " " << num << " " << vr << " " << vl << " " << vc << endl;
}