Thursday, July 9, 2009

Turbo C++! How can i make the char statement recognize two characters at the same time?

This is for my assignment in our basic programming class. we are required to make a program like those in television. Where the computer displays a list of channels then prompts the user to input a channel (it could be a letter or number). then the name of the channel will be displayed on the screen. i used the data type "char" so that i could work on both letters and numbers. but char only recognizes a single character. how can i make "char" recognize two characters on a single variable? by the way i am using borland C++(TURBO C++ IDE) thanks..

Turbo C++! How can i make the char statement recognize two characters at the same time?
I guess you could use either a character array or a string. If you know you only need two characters then I would go with the character array.





char channel[2];


channel[0] = '1';


channel[1] = '0';


element 0 is the first character and element 1 is the second.
Reply:I too would use a char array.


char channel[2];





channel[0] = '1';


channel[1] = '0';





OR


channel[0] = '7';





The use a string array for the channel names


string name [10] will hold an array of 10 strings.


so name[0]='ABC"; etc





Then use the atoi function to change the char array to a number. (atoi is a function that takes a constant char pointer (char array variable) and converts the entire char array to its integer equivalent)


So, if the char array is ['1','0'] it would be 10 in int


Then that 10 can correspond to the 10th index in the string array, which could be whatever, CBS.





Like this


int x = atoi(channel);


String tempName = name[x]; //get the string name





Meaning you have two arrays where there index corresponds to one another.


No comments:

Post a Comment