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

标签 c++ visual-studio-2015 spaces

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

或者如果有任何输出(收据)显示 Item 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;
}

最佳答案

来自菲律宾共产党 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.

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

关于c++ - 无法在变量名称中放置空格 - Visual Studio 2015 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39542850/

相关文章:

c# - 如何将 Paypal 与 WinForm 桌面应用程序集成

c++ - OpenGL 不渲染颜色

typescript - Angular2 - 导入多个模块

java - 删除从输入文件到 spring 批处理程序的所有行中的尾随空格

python - 有没有一个文本编辑器会自动判断是否缩进空格或制表符?

android - 如何请求 URL 中有空格的 JSON?

c++ - 抛出异常错误 'terminate called...'

c++ - 命令模式 : Where to create the Command items?

android - visual studio - cordova - 在 Android 设备上运行我的应用程序

c++ - 在 Ubuntu 12.04 上运行一个在 Windows 上的 MS Visual Studio 中用 c++11 编写的项目