使用流的命令行浮动输入和输出
我在这个网站上的第一个问题是:
我正在研究一个教程问题,它要求我编写一个程序,从命令行输出一些输入的浮点数的乘积:这是使用流来完成的。 现在在我的教程书中建议使用以下代码:
#include <iostream>
#include <cstdio>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <cstdlib>
#include <istream>
using namespace std;
int main( int argc, char* argv[] )
{
float data[20];
int i;
float sum;
for(i=1;i<argc-1;i++){
istream cinx(81,argv[i];
cinx>>data[i];
cout<<data[i];
sum=sum+data[i];
}
cout<<"\nsum = "<<sum;
}
所以我尝试了上面的代码和许多不同的变体,直到我的头发掉光! - 但无济于事,因为它无法编译,而是收到以下错误消息:
“没有匹配的函数用于调用 `std::basic_istream >::get(char**&, int)”
任何如有建议,我们将不胜感激。
My first question on this site, here goes:
I am working on a tutorial question and it asks me to write a program that outputs the product of some entered floating point numbers from the command line: This is to be done using streams.
Now in My tutorial book it suggests using the following code:
#include <iostream>
#include <cstdio>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <cstdlib>
#include <istream>
using namespace std;
int main( int argc, char* argv[] )
{
float data[20];
int i;
float sum;
for(i=1;i<argc-1;i++){
istream cinx(81,argv[i];
cinx>>data[i];
cout<<data[i];
sum=sum+data[i];
}
cout<<"\nsum = "<<sum;
}
So I have tried the above code and many different variations until my hair is falling out! - But to no avail as it does not compile, instead I get the error message along the lines of:
" no matching function for call to `std::basic_istream >::get(char**&, int)"
Any suggestions would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢outybungalobill和Paul R的帮助。
最终编译并成功运行的代码是:
Thanks for Helping outybungalobill and Paul R.
The final code which compiled and runs successfully is:
istream 构造函数需要一个streambuf。您可能想
在开始时使用 istringstream: 和
#include
。此外,您计算的是总和,而不是乘积。
istream constructor expects a streambuf. You probably want to use istringstream:
and
#include <sstream>
in the beginning.Besides, you're computing the sum, not the product.
你少了一个括号。将此行:更改
为:
您还需要初始化
sum
- 将:更改为:
并且命令行参数的数量也减少了 1 - 将:更改
为:
You have a missing parenthesis. Change this line:
to this:
Also you need to initialise
sum
- change:to:
And you're also off by one on the number of command line arguments - change:
to: