Switch(not_working)

Status
Not open for further replies.

Maverick340

Ambassador of Buzz
Code:
#include<iostream.h>
void main()
{
int d;
char a;
cout<<"Please enter your Date of Birth In the following Format   :";
cout<<"DD\n" ;

cin>>d;
switch(d)
{
case (1): a="One";
break;
case (2) :a="Two";
break;
case (3) :a="Three";
break;
default:a="Null 0000";
}
cout<<a;
}

Whats wrong???
 

siriusb

Cyborg Agent
U can't just assign a string of characters to a char variable. Either use the string class or use a char* variable (@rray).
And if u are going to stick with a char variable, u cannot assign to a char variable a double-quoted string/char. Double quotes specify a "\0" at the end and hence "a" is actually two chars wide.
Learn the pointer fundas to understand this.
 

kalpik

In Pursuit of "Happyness"
Try this:

Code:
#include<iostream.h>
#include<string.h>
void main()
{
int d;
char a[10];
cout<<"Please enter your Date of Birth In the following Format   :";
cout<<"DD\n" ;

cin>>d;
switch(d)
{
case (1): strcpy(a,"One");
break;
case (2) :strcpy(a,"Two");
break;
case (3) :strcpy(a,"Three");
break;
default: strcpy(a,"Null 0000");
}
cout<<a;
}

Do tell me if it works, i didnt try it myself! hehe
 
Status
Not open for further replies.
Top Bottom