C++输入和输出问题
C++ Input and Output problems
我的问题是,当我编译和运行程序时,随着循环的继续,小时没有列出 1,2,3 的"小时",而且每行的循环计算都相同。
这就是程序的样子http://postimg.org/image/htk194eah/
计算是错误的,小时假设为 1,2...5
我希望它看起来像这样http://postimg.org/image/pnkvab1j1/
这是我到目前为止所拥有的:
int main()
{
// Variables
int speed;
int time;
int distance;
// Obtain the speed
cout << "Please input the speed of the vehicle " ;
cin >> speed;
while(speed < 0) // while statement
{
cout << "Please refrain from using a negative number ";
cin >> speed;
}
cout << "Please enter the time, represented in hours, travelled" <<endl;
cin >> time;
// Obtain the time
while(speed < 1)
{
cout<< "Please use a number greater than 1 " <<endl;
cin >> time;
}
// Calculation
distance = speed * time;
cout << endl;
cout << "Hour(s) " << "t" << "Distance Travelled" << endl;
cout << "____________________________________________" << endl;
// "for" Loop statement
for(int count =1; count <= time; count++)
{
cout << " " << "tt" << speed*time << endl;
}
system ("PAUSE");
return 0;
}
当您在 for 循环中打印时,速度 = 20 且时间 = 5 始终,因此它始终打印 100(在您给出的示例中)。
您希望打印速度*计数(在本例中)。
再次打印小时",这是一个空字符串,您要打印计数。
cout << count << "tt" << speed*count << endl;
这是正确的程序。你的程序非常好。但是你有一个非常小的错误。
#include<iostream.h>
main()
{
// Variables
int speed;
int time;
int distance;
// Obtain the speed
cout << "Please input the speed of the vehicle " ;
cin >> speed;
while(speed < 0) // while statement
{
cout << "Please refrain from using a negative number ";
cin >> speed;
}
cout << "Please enter the time, represented in hours, travelled ";
cin >> time;
// Obtain the time
while(speed < 1)
{
cout<< "Please use a number greater than 1 ";
cin >> time;
}
// Calculation
cout << endl;
cout << "Hour(s) " << "t" << "Distance Travelled" << endl;
cout << "____________________________________________" << endl;
// "for" Loop statement
for(int count =1; count <= time; count++)
{
cout << count << "tt" << speed * count << endl;
}
system ("PAUSE");
return 0;
}
相关文章:
- 可能我知道为什么这段代码没有给出任何输出吗?
- 有没有办法我可以在 C++ 中使用 if 语句来说明如果一个字符串不是两个名称的 eqal,它会给出一个输出,ATM i
- 为什么带有逻辑和运算符以及增量运算符的 c++ 程序给出的输出与预期不同?
- C++向量排序给出0作为输出
- 奇怪的数字随机出现在输出中.(C++)
- 如何从Visual Studio中的给出.pyd输出中构造Python代码
- 我将 c++ 矢量数据集转换为 c 数组.我将相同的数据集传递到一个应该给出相同输出的函数中
- 向量<对<双精度,双精度>>给出问题
- 为什么这段代码给出的输出与预期不同?
- C 反向一个不给出预期输出的数组
- 矢量在超出边界时给出0作为输出
- 我很确定我犯了一个愚蠢的错误,但是这段代码给出的输出太长而无法阅读
- C++ 中的侵蚀函数不给出任何输出
- 为什么字符串变量"sum"不存储任何数据,程序不给出任何输出
- 输入层类型:图像数据在窗口咖啡cpp中给出空白输出
- 在队列中,插入和删除后,被删除的元素也会出现在输出中.为什么
- 服务器未收到UDP数据包,花费数小时试图找出问题所在
- 程序给出 0 作为输出 (C++)
- C++ 输出找出问题
- 在成功运行10小时后,在发布模式下从windows应用程序中获得异常,如何检测出问题