Class displays a greek letter

After running the following program, it displays the Greek letter alpha (regardless of the name entered). Please help me to find out why. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <iomanip>
using namespace std;

class Business
{
    char name;

public:
    Business(char name) {Business::name=name;}
    void showBusiness();
};

void::Business::showBusiness (){

    cout<<"Name: "<<name<<".\n\n";
}


int main()
{
    int name;

    cout<<"Enter the name of the business: ";
    cin>>name;
    cout<<endl<<endl;

    Business bus(name);
    bus.showBusiness ();

    return(0);
}
Last edited on
Hi,


On line 7 name is a char. On line 22 the variable is an int.

Edit in italics:

When you run the code using the gear icon at the top right, it displays the character that corresponds with the ASCII code of whatever number you enter, unless that number is bigger than what a char handle.

Char isn't a good type for a name, perhaps you need std::string?
Last edited on
Thank you for your help. What is the easiest way to input into std::string name?
(When I switched the int name to char name in line 22 the program only displays the first letter of the name entered.)

Have a look in the reference material top left of this page.

http://www.cplusplus.com/reference/string/basic_string/


It's a good thing to learn what all the types are, and what you can do with them, and their limitations.

There is also a tutorial at the top left of this page.

http://www.cplusplus.com/doc/tutorial/
Thank you for your help.
Topic archived. No new replies allowed.