我正在为在 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/