使用流的命令行浮动输入和输出

发布于 2024-12-09 19:46:40 字数 877 浏览 0 评论 0原文

我在这个网站上的第一个问题是:

我正在研究一个教程问题,它要求我编写一个程序,从命令行输出一些输入的浮点数的乘积:这是使用流来完成的。 现在在我的教程书中建议使用以下代码:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

游魂 2024-12-16 19:46:40

感谢outybungalobill和Paul R的帮助。

最终编译并成功运行的代码是:

#include <iostream>
#include <sstream>

using namespace std;

int main(  int argc, char* argv[] ) 
{  

          float data[20];
          int i;
          float sum = 0.0f;

          for(i=1;i<argc;i++){


              istringstream cinx(argv[i]);

              cinx>>data[i];
              cout<<data[i];

              sum=sum+data[i];
         }
         cout<<"\nsum = "<<sum<<"\n";

   system ("PAUSE");
   return 0;
}

Thanks for Helping outybungalobill and Paul R.

The final code which compiled and runs successfully is:

#include <iostream>
#include <sstream>

using namespace std;

int main(  int argc, char* argv[] ) 
{  

          float data[20];
          int i;
          float sum = 0.0f;

          for(i=1;i<argc;i++){


              istringstream cinx(argv[i]);

              cinx>>data[i];
              cout<<data[i];

              sum=sum+data[i];
         }
         cout<<"\nsum = "<<sum<<"\n";

   system ("PAUSE");
   return 0;
}
习ぎ惯性依靠 2024-12-16 19:46:40

istream 构造函数需要一个streambuf。您可能想

istringstream cinx(argv[i]);

在开始时使用 istringstream: 和 #include

此外,您计算的是总和,而不是乘积。

istream constructor expects a streambuf. You probably want to use istringstream:

istringstream cinx(argv[i]);

and #include <sstream> in the beginning.

Besides, you're computing the sum, not the product.

2024-12-16 19:46:40

你少了一个括号。将此行:更改

          istream cinx(81,argv[i];

为:

          istream cinx(81,argv[i]);

您还需要初始化 sum - 将:更改

      float sum;

为:

      float sum = 0.0f;

并且命令行参数的数量也减少了 1 - 将:更改

      for(i=1;i<argc-1;i++){

为:

      for(i=1;i<argc;i++){

You have a missing parenthesis. Change this line:

          istream cinx(81,argv[i];

to this:

          istream cinx(81,argv[i]);

Also you need to initialise sum - change:

      float sum;

to:

      float sum = 0.0f;

And you're also off by one on the number of command line arguments - change:

      for(i=1;i<argc-1;i++){

to:

      for(i=1;i<argc;i++){
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文