Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using 'char' variable 1

Status
Not open for further replies.

MarkJ2

Technical User
Sep 25, 2001
17
0
0
US
I am writing a program using classes. In the header file I want to declare a 'char' variable as private (lets call the variable 'flag'). Then in the member class function 'print' depending on whether the time is 0 thru 11 or 12 thru 23 ( I am using military time) I want the 'char' variable 'flag' to hold the string either 'AM' or 'PM'.

Once I am in the 'print' function I am not sure how to assign either 'AM' or 'PM' to the char variable. Is it..
flag = "AM" or flag == "AM" or flag == 'AM'.
I have tried several different ways and nothing is working.

Thanks
 
hi

u cannot assign a string to a char. do the following.

have that flag as a char array and not char.

char flag[3];

to assign some value to it, do this,

strcpy(flag,"AM"); (to copy AM or PM to flag).

to check if it is AM or PM, do this,

strcmp(flag,"AM"); (this will return zero if the string are equal or a non zero value).

hope this helps. feel free to write again if it's not clear.

luv
Karthik.
 
MarkJ -

Like Karthik implies, you need to reserve storage for the "AM" or "PM" string, plus a 0 to terminate the string. If you use a variable of type char (not an array of char), it can only hold 1 character and can't participate in any string functions. If you create a 3-character array it then becomes a two-character string, and you can concatenate it, etc. using the standard C str functions.

Chip H.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top