2

我是 C++ 新手,我实际上是在学习并在实验部分,但是,在实验时我遇到了 cout 函数的问题。程序在编译时失败。我想知道你们是否可以帮助我:这是我写的来源。

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
 signed short int a;
 signed short int b;
 signed short int c;
 a = 25;
 b = 8;
 c = 12;

 cout << a << endl;
 cout << b << endl;
 cout << c << endl;
 cout << "What is the sum of a + b - c? The answer is: ";
 cout << a + b - c;
 cout << endl;
 cout << "Why is this?" << endl;
 cout << "This is because: ";
 cout << "a + b equals: " << a + b << endl;
 cout << "and that minus " c << " is" << a + b - c << endl;
 cout << "If that makes sense, then press enter to end the program.";

 cin.get();
 return 0;


}

我也想知道有符号和无符号是什么意思,我认为它取决于编译器?我正在使用 Visual C++ 2008 速成版。

感谢任何可以指出我的错误并帮助我的人!

4

2 回答 2

10
 cout << "and that minus " c << " is" << a + b - c << endl;
 //                       ^

您缺少一个<<.


unsigned意味着数据类型只能存储非负整数,而signed意味着它也可以存储负整数(因为它可以有一个负“符号”)。

支持的确切整数范围取决于平台。通常,aunsigned short支持 0 到 65535 范围内的值,asigned short支持 -32768 到 32767。

于 2010-10-29T20:15:28.317 回答
2
cout << "and that minus " c << " is" << a + b - c << endl;

您在字符串“和那个减号”和短 c 之间缺少一个“<<”。

有符号意味着一位专用于确定该值是否为负数,但这也意味着您不能拥有与无符号一样大的数字。默认情况下,变量是有符号的,除非您另外指定。

于 2010-10-29T20:18:56.977 回答