【问题标题】:How can i make this c++ code work?我怎样才能使这个 c++ 代码工作?
【发布时间】:2017-09-20 05:24:09
【问题描述】:

这个 c++ 代码给了我错误,我不知道如何删除这些错误。
代码:

#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
#include <iomanip>
using namespace std;
string& RaiseItToUpperCase(string& w)
{
int len = w.length();
for (int index =0; index <len; index++)
    w[index]= toupper(w[index]);
return w;
}
void LoadData()
{
string filename;
while(true)
{

    cout<<"Enter Data file:";
    cin>>filename;
    cout<<"Filename entered"<<filename<<endl;
    ifstream myfile(filename.c_str());
    if(!myfile.good())
    {

        cout<<"Please Enter a Valid text File"<<endl;
        continue;
    }
    static std::string const targetExtension ("txt");
    if (!(filename.size() >= targetExtension.size() 
    && std::equal (filename.end() - targetExtension.size(),
    filename.end(),
    targetExtension.begin() ) ))
    {
        cout<<"File is not txt"<<endl;
        continue; 
    }
    break;
}
string i = filename;
string o;
cout<<"Enter an output file name:"<< endl;
cin>>o;
ofstream output;
ifstream input(filename);
output.open(o.c_str());
int charc =0;
int numw =0;
int longl =0;
int shortl =10000;
while (input>>1)
{
    numw++;
    charc = charc + i.length() +1;
    if (i.length() > longl)
    {
        longl = i.length();
    }
    if (i.length() < shortl)
    {
        i =RaiseItToUpperCase(i);
        output << i;
        if(input.get() ==32)
        {
            output<<" ";
        }
        else
        {
            output<<"\n";
        }
    }
    charc = charc - 1;
    output<<"\nWord Counter Summary\n"<<endl;
    output<<"Total Number of Words:"<<numw<<endl;
    output<<"Total Number of Characters:"<<charc<<endl;
    output<<" Largest Word Size:"<<longl<<endl;
    output<<" Smallest Word Size"<<shortl<<endl;
}
}
int main ()
{
LoadData();
return 0;
}

这是 c++ 文件流程序,我正在尝试运行此代码,但它给了我错误,我无法弄清楚如何删除此错误
所以谁能告诉我如何删除这些错误并让这段代码运行

更新

这是错误:

错误 1 ​​错误 C2679: 二进制 '>>' : 找不到运算符 'int' 类型的右手操作数(或者没有可接受的 转换)c:\users\acer\documents\visual studio 2012\projects\consoleapplication15\consoleapplication15\sour‌​ce.cpp 52 1 ConsoleA‌​应用程序15

提前致谢

【问题讨论】:

  • 它给出了什么错误?
  • 错误 1 ​​错误 C2679: 二进制 '>>' : 未找到采用 'int' 类型右侧操作数的运算符(或没有可接受的转换) c:\users\acer\documents \visual studio 2012\projects\consoleapplication15\consoleapplication15\source.cpp 52 1 ConsoleApplication15
  • 还有其他错误
  • while (input &gt;&gt; 1) 你试图将input 中的内容放入号码1

标签: c++ fstream ofstream


【解决方案1】:

你想在while (input&gt;&gt;1) 做什么? 如果你想读出一些东西:

  • 如果你想读取像int8_t(或int16_t/int32_t等)这样的整数: int8_t number = 0; while (input >> number)
  • 如果您想阅读char char ch; while (input >> ch)

【讨论】:

    猜你喜欢
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2018-01-21
    • 2023-03-23
    相关资源
    最近更新 更多