how do you include the symbol '-' in a program for c++

//DEBUG3-1
//This program contains a class to hold social security numbers
//including a member function to display them with dashes
#include<iostream>
//declaration section


class SocSecNum
{
private:
int first3;
int middle2;
int last4;
static char dash='-';
public:
void getNum(const int f,const int m, const int l);
void showNum(void);
};


static char SocSecNum::dash;
// implementation section
void SocSecNum::getNum(const int f, const int m, const int l)
{
SocSecNum::first3=f;
SocSecNum::middle2 = m;
SocSecNum::last4=l;
}


void SocSecNum::showNum(void)
{
cout<<"\nSocSecNum is "<<SocSecNum::first3<<SocSecNum::dash;
cout<<SocSecNum::middle2<<SocSecNum::dash<<SocSecNum::last4<<endl;
}


void main()
{
SocSecNum oneSocSecNum;
oneSocSecNum.getNum(333,44,5555);
oneSocSecNum.showNum();
}
ok I got it down to this for the code..but cant figure out how to declare and use the dash..I am not good with class, and inheritance stuff, please help

//DEBUG3-1
//This program contains a class to hold social security numbers
//including a member function to display them with dashes
#include<iostream>
using namespace std;
//declaration section
class SocSecNum
{
private:
int first3;
int middle2;
int last4;
static char dash;

public:
void getNum(const int f,const int m, const int l);
void showNum(void);
};

void SocSecNum::dash(char dash)
{
static char dash='-';
}
// implementation section
void SocSecNum::getNum(const int f, const int m, const int l)
{
SocSecNum::first3=f;
SocSecNum::middle2 = m;
SocSecNum::last4=l;
}
void SocSecNum::showNum(void)
{
static char dash='-';
cout<<"\nSocSecNum is "<<SocSecNum::first3<<SocSecNum::dash;
cout<<SocSecNum::middle2<<SocSecNum::dash<<SocSecNum::last4<<endl;
}
int main()
{
SocSecNum oneSocSecNum;
oneSocSecNum.getNum(333,44,5555);
oneSocSecNum.showNum();
system("pause");
Try replacing every instance of SocSecNum::dash with "-". Should work the same way. From what I can tell, you were trying to make a constant called "dash" that would display a dash when referred to... Again, "-" should work fine.
closed account (3CXz8vqX)
You could just use the ascii code for a minus... *ahem*
> but cant figure out how to declare and use the dash

Make it a const (or constexpr in C++11)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

struct SocSecNum98
{
    static const char dash = '-' ; // C++98 : header file
};

const char SocSecNum98::dash ; // C++98: implementation file

struct SocSecNum11 // C++11
{
    static constexpr char dash = '-' ;
};

int main()
{
    std::cout << SocSecNum11::dash << ' ' << SocSecNum98::dash << '\n' ;
}

Topic archived. No new replies allowed.