Christopher Terry
Christopher Terry

Reputation: 13

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

I'm doing some very basic coding for making a receipt in Visual Studio 2015 in C++. I'm having trouble creating names with spaces without being forced to use underscores (ex. Item 1 vs Item_1). I feel like it's a simple fix, but I'm extremely new to coding as a whole.

Or if anything just have the output (receipt) show Item 1 and not Item_1.

This is my current code:

#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;
}

Upvotes: 0

Views: 206

Answers (3)

Stewart Smith
Stewart Smith

Reputation: 1416

From the CPP 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.

In summary, an identifier (ie. variable name, function name, class name, etc.) cannot have a space in it. Spaces are used to help delimit tokens that make up the language.

Upvotes: 2

3442
3442

Reputation: 8576

Simply put, you can't. In most languages (and this includes C++), the rules for naming identifiers (such as variable names) are as follows:

  • The name should consist of at least 1 character.

  • The first character shall be either an underscore (_), an uppercase latin letter (A through Z), or lowercase latin letter (a through z).

  • All subsequent characters may consist of the same characters allowed for the first character, plus the decimal digits (0 through 9).

As such, we can conclude, **spaces are, by no means, allowed in identifiers*.

(Additionally, most languages lack support for names with symbols other than A through Z, a through z, 0 through 9 and the underscore (_). The exceptions are few, and, for the matter being, are not worth worrying about.)

For instance, the following are valid names in C++:

  • foo
  • foo_bar
  • MyVariable123

And the following are not:

  • 123variable
  • my integer
  • français

You should also take into account that you shouldn't use the following identifiers, since they're reserved:

  • Identifiers beginning with an underscore followed by an uppercase letter (such as _Z3var).

  • Identifiers containing any two adjacent underscores (such as __baz or some__identifier).

  • Keywords (such as int, long, if, for, etc...) are special identifiers which the language reserves for special purposes.

Upvotes: 1

Shankar Shastri
Shankar Shastri

Reputation: 1154

You Cant Have Spaces In Between Variable Name.

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

Rules To Declare CPP Variables: http://www.sitesbay.com/cpp/cpp-variable-declaration

Upvotes: 2

Related Questions