无法在变量名中放置空格 - Visual Studio 2015 (C++)

Unable to put spaces in variable names - Visual Studio 2015 (C++)

我正在用 C++ 编写一些非常基本的代码,以便在 Visual Studio 2015 年制作收据。我无法在不被迫使用下划线的情况下创建带空格的名称(例如,项目 1 与 Item_1)。我觉得这是一个简单的修复,但我对整个编码非常陌生。

或者如果有任何输出(收据)显示项目 1 而不是 Item_1。

这是我当前的代码:

#include "stdafx.h"
#include <iostream>

using namespace std;


int main()
{
    double Item_1;
    double Item_2;
    double Item_3;
    double Total;

    Item_1 = 2.50;
    Item_2 = 0.75;
    Item_3 = 12.98;
    Total = Item_1 + Item_2 + Item_3;

    cout << "Thank you for shopping at StuffMart" << endl;
    cout << "Item_1 = " << Item_1 << endl; 
    cout << "Item_2 = " << Item_2 << endl;
    cout << "Item_3 = " << Item_3 << endl;
    cout << "Total = " << Total << endl;

    system("pause");
    return 0;
}

变量名之间不能有空格。

example: 

int shan kar;//Wrong Declaration
int int;//Wrong Declaration You Cant Have Keywords in place of variables


int shankar;//Valid Declaration
int shan_kar;//Valid Declaration

声明 CPP 变量的规则: http://www.sitesbay.com/cpp/cpp-variable-declaration

来自菲律宾共产党language reference documenation:

An identifier is an arbitrarily long sequence of digits, underscores, lowercase and uppercase Latin letters, and most Unicode characters (see below for details). A valid identifier must begin with a non-digit character (Latin letter, underscore, or Unicode non-digit character). Identifiers are case-sensitive (lowercase and uppercase letters are distinct), and every character is significant.

总之,标识符(即变量名、函数名、class名等)中不能有space。空格用于帮助分隔构成语言的标记。

简单地说,你不能。在大多数语言中(包括 C++),命名标识符(例如变量名)的规则如下:

  • 名称应至少包含 1 个字符。

  • 第一个字符应为下划线 (_)、大写拉丁字母(AZ)或小写拉丁字母 (az).

  • 所有后续字符可以由第一个字符允许的相同字符加上十进制数字(09)组成。

因此,我们可以得出结论,**标识符中绝不允许空格*。

(此外,大多数语言不支持使用除 AZaz09 和下划线 (_)。例外情况很少,就目前而言,不值得担心。)

例如,以下是 C++ 中的有效名称:

  • foo
  • foo_bar
  • MyVariable123

以下不是:

  • 123variable
  • my integer
  • français

您还应该考虑到您不应使用以下标识符,因为它们是保留的:

  • 以下划线开头后跟大写字母的标识符(例如_Z3var)。

  • 包含任意两个相邻下划线的标识符(例如__bazsome__identifier)。

  • 关键字(如intlongiffor等)是语言保留的特殊标识符用于特殊目的。