为什么这个基本cin会阻止我的程序编译
Why does this basic cin prevent my program from compiling?
我已经包含并正在使用标准名称空间,当我将文件名硬编码到中时,程序运行得很好,但当我放入cin-VS时,会出现奇怪的错误。为了清楚起见,我特别提到cin>>sodokuFile行。
cout << "Assignment 2nn";
ifstream ins;
cout << "Please enter the Sokoku filen";
string sodokuFile;
cin >> sodokuFile;
ins.open(sodokuFile.c_str());
if(ins.is_open())
{
int num;
//counting numbers displayed horizontally
int counth = 0;
//counting numbers displayed vertically
int countv = 0;
while (ins >> num)
{
cout << num << " ";
counth++;
//placing vertical lines
if(counth %3 == 0)
{
cout << "| ";
}
//making line breaks for new rows
if(counth == 9)
{
cout << "nn";
counth = 0;
countv++;
//horizontal lines
if(countv %3 == 0)
{
cout << "_________________________________________n";
}
}
}
}
else
{
cout << "File does not existn";
return 0;
}
return 0 ;
以下是编译器错误中唯一看起来有用的内容错误C2679:binary">>":找不到接受类型为"std::string"的右侧操作数的运算符(或者没有可接受的转换)
您需要放置
#include <string>
在文件顶部,因为string
标头声明了operator>>(istream&, string&)
。
相关文章:
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 我的程序没有从文件中读取并输入数据
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 为什么我的程序不能显示斐波那契级数?
- 为什么我的程序在使用预留后没有加速?
- 为什么如果我添加这一行,我的程序会不断询问值
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- 为什么我的 c++ 程序检查不是初始化的变量?
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 在 fork() 之后,我在我的程序中不断得到相同的 pid
- C++ - 为什么我的程序在再次调用函数后关闭?
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 我需要如何更改我的程序以使用打开/关闭原则?
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较