Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在数组中返回负字符? 我正在为学校设计一个C++项目,我试图从一个文本文件到一个数组中写入一个矩阵。源数据中有负数和两个5x5矩阵。我一直有不同的角色(╧ 或=)而不是负数: void main() { char c; char nbRow , nbCol; int location = 0; int MatrixArray[10][10]; int negNbr; int k = 0; int l = 0; cin.get(c); nbRow = c; cout << "Number of rows: " << nbRow << endl; cin.get(c); cin.get(c); nbCol = c; cout << "Number of columns: " << nbCol << endl; while (!cin.eof()) { cin.get(c); //cout << (int)c << endl; if (!isblank(c) && (isdigit(c) || c == 45)) { cout << c << " | test1 " << " | "; if (c == 45) { cin.get(c); negNbr = (char)c; negNbr = negNbr * -1; cout << (char)negNbr; MatrixArray[k][l] = (char)negNbr;// *-1; } else //if (isdigit(c)) { MatrixArray[k][l] = (char)c; } cout << " |test2 " << k << l << "| " << (char)MatrixArray[k][l] << endl; if (l == 4) { k++; l = 0; } else l++; } } void main() { 字符c; char nbRow,nbCol; int位置=0; 整数矩阵阵列[10][10]; int negNbr; int k=0; int l=0; cin.get(c); nbRow=c; cout_C++ - Fatal编程技术网

如何在数组中返回负字符? 我正在为学校设计一个C++项目,我试图从一个文本文件到一个数组中写入一个矩阵。源数据中有负数和两个5x5矩阵。我一直有不同的角色(╧ 或=)而不是负数: void main() { char c; char nbRow , nbCol; int location = 0; int MatrixArray[10][10]; int negNbr; int k = 0; int l = 0; cin.get(c); nbRow = c; cout << "Number of rows: " << nbRow << endl; cin.get(c); cin.get(c); nbCol = c; cout << "Number of columns: " << nbCol << endl; while (!cin.eof()) { cin.get(c); //cout << (int)c << endl; if (!isblank(c) && (isdigit(c) || c == 45)) { cout << c << " | test1 " << " | "; if (c == 45) { cin.get(c); negNbr = (char)c; negNbr = negNbr * -1; cout << (char)negNbr; MatrixArray[k][l] = (char)negNbr;// *-1; } else //if (isdigit(c)) { MatrixArray[k][l] = (char)c; } cout << " |test2 " << k << l << "| " << (char)MatrixArray[k][l] << endl; if (l == 4) { k++; l = 0; } else l++; } } void main() { 字符c; char nbRow,nbCol; int位置=0; 整数矩阵阵列[10][10]; int negNbr; int k=0; int l=0; cin.get(c); nbRow=c; cout

如何在数组中返回负字符? 我正在为学校设计一个C++项目,我试图从一个文本文件到一个数组中写入一个矩阵。源数据中有负数和两个5x5矩阵。我一直有不同的角色(╧ 或=)而不是负数: void main() { char c; char nbRow , nbCol; int location = 0; int MatrixArray[10][10]; int negNbr; int k = 0; int l = 0; cin.get(c); nbRow = c; cout << "Number of rows: " << nbRow << endl; cin.get(c); cin.get(c); nbCol = c; cout << "Number of columns: " << nbCol << endl; while (!cin.eof()) { cin.get(c); //cout << (int)c << endl; if (!isblank(c) && (isdigit(c) || c == 45)) { cout << c << " | test1 " << " | "; if (c == 45) { cin.get(c); negNbr = (char)c; negNbr = negNbr * -1; cout << (char)negNbr; MatrixArray[k][l] = (char)negNbr;// *-1; } else //if (isdigit(c)) { MatrixArray[k][l] = (char)c; } cout << " |test2 " << k << l << "| " << (char)MatrixArray[k][l] << endl; if (l == 4) { k++; l = 0; } else l++; } } void main() { 字符c; char nbRow,nbCol; int位置=0; 整数矩阵阵列[10][10]; int negNbr; int k=0; int l=0; cin.get(c); nbRow=c; cout,c++,C++,achar在默认情况下通常是一个signed char(取决于您的编译器),因此它当然可以保存负数-从-128到+127 但是没有“负字符”,因此当您尝试打印它时,它将被解释为无符号字符,范围从0到255。根据您的字符集,与>128关联的字符看起来像您描述的“有趣的图片” 要查看负值,需要使用int,它将以数字形式打印-例如-127cin.get读取输入的单个字符。如果调用cin.get(c)并在控制台中键入123,c将最终包含char'1',而不是数字1或数字123或诸如此类的内容。如果随后将

a
char
在默认情况下通常是一个
signed char
(取决于您的编译器),因此它当然可以保存负数-从-128到+127

但是没有“负字符”,因此当您尝试打印它时,它将被解释为
无符号字符
,范围从0到255。根据您的字符集,与>128关联的字符看起来像您描述的“有趣的图片”


要查看负值,需要使用
int
,它将以数字形式打印-例如
-127
cin.get
读取输入的单个字符。如果调用
cin.get(c)
并在控制台中键入
123
c
将最终包含
char
'1'
,而不是数字
1
或数字
123
或诸如此类的内容。如果随后将其转换为
int
,您将得到
int
49
,因为
'1'
是ASCII码点
49

从控制台读取整数的正确方法是使用
>
运算符:

int i;
std::cin >> i;

使用
>
运算符,如果在控制台中键入
123
i
将以值
123
结束提示:“char”数据类型是无符号的,因此无法从中获取负数…..如果我使用int,那么我的cin.get不起作用,我会得到以下错误:没有重载函数的实例“std::basic_istream::get[带有_Elem=char,_Traits=std::char\u Traits]"匹配参数LySyes,在这个过程中的某个点,你必须把字符串输入转换成数值数据。CIN采用字符串/字符。你的矩阵需要数字。你必须做转换。@ MRCON: C++中的char是实现定义的。在x86和x64系统上通常是签名的。谢谢你的纠正,PlinyTheElder。。你确实是对的。我的假设很差!这是不正确的。这是因为字符是否可以包含负值。如果你想要有符号字符,请使用
signed char
。当我打印为int时,我得到的是ASCII值,而不是数值(例如,48而不是0)。如果我从一开始就将变量声明为int而不是char,那么我的cin.get不起作用,我会得到一个错误它是-128到+127,正如其他人所说,它是有符号的还是无符号的都是实现定义的。@PlinyTheElder--no。类型
char
可以表示与
有符号的char
相同的值范围
unsigned char
,这取决于
char
是有符号的还是无符号的。
signed char
unsigned char
的范围必须至少分别为[-127127]和[0255],尽管它们可以更大。@PeteBecker:不正确。它是[-128,+127]对于有符号字符。只需将一个有符号字符增加255次即可轻松检查。请注意,您的范围[-127127]奇数个值。我似乎无法用我的脚本来处理,我对C++非常陌生,CIN用于控制台的输入。我正试图从一个文件中读取它,我一直在尝试修改它,但是我不能很好地使它工作。谢谢你的帮助。代码> STD::CIN < /C>从进程的标准输入流中读取。默认情况下,TH。在控制台。可以将其重定向到从文件读取,但通常用于从文件读取。它的界面类似于
std::cin
。我的项目要求之一:“禁止使用iostream以外的任何其他头库。”要使用ifstream,看起来我需要包含FSSTREAM头,但这对我不起作用。@CurryMustard如果不能使用
,则必须重定向输入,因此从
std::cin
读取的内容实际上是文件的内容。这取决于您的操作系统/shell。在Windows cmd.exe中,它是uld看起来像
program.exe