Thread: c++ question about char

  1. #1
    Registered User Mazer's Avatar
    Join Date
    Jul 2002
    Posts
    9

    c++ question about char

    hi everyone, i just decided to learn c++ this summer so i'd be ahead of (most) people when i go back to school. i've only just started and as a first project i want to make a program that will convert text characters to their binary numbers and vice versa. the first programming language i learned was turing and it had a nice little feature that allowed you to use a char (they called them strings) kinda like an array. you could get a variable and look at each individual character within it as well as determine the length (how many characters). i was hoping to find out if c++ had something similar to this. here's how i figure my program will be:

    - get input from user
    - look at first character in user's input and compare it to a list of if statements, then assigning the matching binary number to another char that it will output to the screen once all binary numbers have been determined.

    i hope i've explained my problem well enough for you to understand it and help me. remember, i'm not looking for anyone to write my program for me (what would be the point in that? i'm doing this for myself), i'm just trying to find out if i can look at each character in a variable.

    please let me know if you have trouble understanding this... or if you've got an answer

  2. #2
    Akilla
    Guest

    Lightbulb check this out

    To access elements of a char array
    (if this is what you asked for..)
    PHP Code:
    #include <iostream.h>

    main()
    {
          
    char word[20];

          
    cout <<  "Enter a word: " ;
          
    cin >> word;
          
    cout << "First letter is " << word[0] << endl;
          
    cout << "Second letter is " << word[1] << endl;

    be sure to use some kind of a getchar() to see the output
    before the program closes.

    COOL PROGRAMS @ www.akilla.tk

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Getting the base 10 values shouldn't be too hard. You will have to write your own base 10 to binary converter.

    Here's how to get the base 10 value.

    int foo;
    char abc;
    abc='a';
    foo=(int) abc;

  4. #4
    Registered User Mazer's Avatar
    Join Date
    Jul 2002
    Posts
    9
    Originally posted by golfinguy4
    [B]Getting the base 10 values shouldn't be too hard. You will have to write your own base 10 to binary converter.
    uh... not really. i've found a number of websites that'll convert ascii to binary, i'm just seeing if i can write a program to do it. oh and thanks akilla

  5. #5
    Unregistered
    Guest
    goldfinguy was correct when you have the value stored as an integer convert that to binary
    decimacl 2 binary conversion.
    store each bit as an item in an array as you go then output the array.

  6. #6
    Registered User Mazer's Avatar
    Join Date
    Jul 2002
    Posts
    9

    um... what?

    sorry, i don't understand what you mean by all that... stuff? here's what i was thinking, user inputs a letter, let's call the variable 'text' (and it's a char). now we go to a big ol' if statement that checks through all the letters of a through z (lowercase for now, uppercase later). when it finds the right letter it assigns the correct binary number to the variable. i decided to use the same variable to save trouble but that seems to have made me more trouble. i try to compile and get errors that say 'too many characters in constant'. can you tell me what i'm doing wrong? here's an example of one of the if statements:

    Code:
    char textChange;
    
    int main()
    {
         cout << "Please press a letter.\n";
         cin << textChange;
         if (textChange == 'a')
              textChange = '01100001';
    return 0;
    }

  7. #7
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    cin >> textChange; // >> <- << //from stdin to var...

    textChange = 01100001
    Note that 01100001 is an octal number.

    ' ' are used with single characters and " " are used with strings.

  8. #8
    Registered User Mazer's Avatar
    Join Date
    Jul 2002
    Posts
    9
    sorry about the cin part, i have it typed correctly in my source code. but how can i change it from a single character to a string?

  9. #9
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Do you mean:
    char foo[80];
    cin>> foo;
    if(!strcmp(foo,"a") {
    strcpy(foo, "0110011"); // Whatever.
    }

  10. #10
    Registered User Mazer's Avatar
    Join Date
    Jul 2002
    Posts
    9
    i'm gonna have to say no... because your code is very confusing to me. what i'm trying to do is check the text the user inputs, and then change the value of the variable to the binary number depending on what letter it is. but i keep getting the 'too many characters in const' error so i guess i need to know how to make the char a string. in turing this was simple but in c++ everything is new to me

  11. #11
    Unregistered
    Guest

    howto

    In order to make your char value hold more than one character, you can use an array of chars. this is done by doing this:
    char text[20];
    this will allow the user to input a word of up to 20 (0 through 19) characters. Remember that with arrays they start at index 0. Also the last index (20) will contain the \n terminator.

  12. #12
    Registered User Mazer's Avatar
    Join Date
    Jul 2002
    Posts
    9
    gotcha! but does this mean i can't just make a string? like:
    Code:
    string text = 'blah!'
    or does the char array count as a string?

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    7
    std::string x = "blah";

    reference for std::string methods:
    http://cppreference.com/cppstring.html

  14. #14
    Unregistered
    Guest
    If you want to manipulate the characters in the string, your best bet is to use an array. It is very similar anyways.

  15. #15
    Registered User Mazer's Avatar
    Join Date
    Jul 2002
    Posts
    9

    Angry

    ahh... you gotta love the frustrations of programming! eliminate 29 old errors, get 4 new ones. here's my big ol' problem up the a$$. the if statements i have are in a function and i guess i can't have strings as functions. here's what i'm talking 'bout:

    Code:
    #include <iostream.h>
    
    char text[9];
    int binNum, found, notfound = 0;
    
    
    std::string binarize (char textChange[9])
    {
    	////   a to z   ////
    	if (textChange[0] == 'a')
    		textChange = '01100001';
    		else if (textChange[0] == 'b')
    			textChange = '01100010';
    		else if (textChange[0] == 'c')
    			textChange = '01100100';
    		else if (textChange[0] == 'd')
    			textChange = '01100100';
    		else if (textChange[0] == 'e')
    			textChange = '01100101';
    		else if (textChange[0] == 'f')
    			textChange = '01100110';
    		else if (textChange[0] == 'g')
    			textChange = '01100111';
    		else if (textChange[0] == 'h')
    			textChange = '01101000';
    		else if (textChange[0] == 'i')
    			textChange = '01101001';
    		else if (textChange[0] == 'j')
    			textChange = '01101010';
    		else if (textChange[0] == 'k')
    			textChange = '01101011';
    		else if (textChange[0] == 'l')
    			textChange = '01101100';
    		else if (textChange[0] == 'm')
    			textChange = '01101101';
    		else if (textChange[0] == 'n')
    			textChange = '01101110';
    		else if (textChange[0] == 'o')
    			textChange = '01101111';
    		else if (textChange[0] == 'p')
    			textChange = '01110000';
    		else if (textChange[0] == 'q')
    			textChange = '01110001';
    		else if (textChange[0] == 'r')
    			textChange = '01110010';
    		else if (textChange[0] == 's')
    			textChange = '01110011';
    		else if (textChange[0] == 't')
    			textChange = '01110100';
    		else if (textChange[0] == 'u')
    			textChange = '01110101';
    		else if (textChange[0] == 'v')
    			textChange = '01110110';
    		else if (textChange[0] == 'w')
    			textChange = '01110111';
    		else if (textChange[0] == 'x')
    			textChange = '01111000';
    		else if (textChange[0] == 'y')
    			textChange = '01111001';
    		else if (textChange[0] == 'z')
    			textChange = '01111010';
    		else if (textChange[0] == ' ')
    			textChange = '00100000';
    
    		std::string binaryNumber = textChange
    		return binaryNumber;
    }
    
    int main()
    {
    	cout << "Please enter a letter to be Binarized.\n";
    	cin >> text;
    	binarize (text);
    	cout << "\nThis is the binary value of your letter: " << text;
    	int counter = 0;
    loop: counter++;
    goto loop;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  2. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  3. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM