如何使用CIN将输入整数值获取到char类型变量中
How to get input integer value into char type variable using cin?
void defineEdge()
{
char vertex1, vertex2;
int charToInt = 0;
while (charToInt != -1)
{
cout << "Define an edge by listing a pair of vertices (-1 to stop): ";
cin >> vertex1 >> vertex2;
cin.ignore(256, 'n');
charToInt = vertex1 - '0';
// getVertexIndex() eventaully returns the index representing
// [index of vertex1][index of vertex2] in 1-D array
// Assign 1 to represent the directed connection between 2 vertices
graphMatrix[getVertexIndex(vertex1, vertex2)] = 1;
}
}
这是代表图形类的矩阵的成员函数部分。矩阵是未加权的有向图的邻接矩阵。矩阵被动态分配为1-D数组,并使用行主要顺序访问所需的索引。
我试图获得下面的顶点来定义边缘:
通过列出一对顶点(-1停止(来定义边缘:A B通过列出一对顶点(-1停止(来定义边缘:b a通过列出一对顶点(-1停止(来定义边缘:-1 A
但是,每当我输入-1
时,我都会出现调试错误运行时检查失败#3-the变量'vertex2Index'正在不被初始化。
我想知道是否有适当的方法可以获取整数值(I.G. -1(char变量(i.g. vertex1(
编辑:还有另一个用于存储顶点的整数列表,因此我想获得Vertex1和vertex2,以使用GetVertexindex((获得相应索引的索引。
您可以按照评论中建议的使用cin.peek()
。尽管如此,我觉得阅读字符串并使用stoi()
会更简单。顺便说一句,您是否故意不跳过最后一个条目(以-1为vertex1(?从您的实施中不清楚。
相关文章:
- 是否可以在C++中获取 CHAR 的有效十六进制地址?
- 获取"rw_ssid"中成员"长度"的错误请求,该成员属于非类类型"char*"
- 获取未知分段错误"(地址)在 std::__cxx11::basic_string<char, std::char_traits<char>,..., std::分配器<cha
- 无法获取已填充堆栈<char>的顶部元素
- 为什么要使用引用来获取 *char?当我们使用指针时,不使用引用不是更好吗?
- 从函数获取 char* 后的条件 gdb 断点
- 如何使用CIN将输入整数值获取到char类型变量中
- 使用安全模式从DLL(通过(INTPTR)参数传递到该函数)获取char **
- (c#)从c dll获取char **时的accessViolationException
- 在不复制的情况下从Ostream中获取char*
- 如何在C中获取char阵列的第一个X元素?提升效果是否有改进的方法
- 从返回int的函数中获取char的正确方法
- 从另一个函数中获取char[]的好方法.用c/c++开始思考
- 从txt文件中获取char并在标准输出C++上进行编写
- 如何获取char数组的列数
- 获取char输入,检查它是否有效,如果无效,清除整个输入
- 如何获取char数组中元素的地址
- Fopen在c++中获取char *错误
- 从 std::string & std::wstring 中获取 char 整数值
- 为什么 'std::string' 的赋值运算符按值而不是 'const' 引用来获取 'char'?