Help with storing letters in varibles

This is a discussion on Help with storing letters in varibles within the C++ Programming forums, part of the General Programming Boards category; Hi again I also need help with storing letters in varibles thanks again...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    18

    Exclamation Help with storing letters in varibles

    Hi again I also need help with storing letters in varibles thanks again

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    char is the data type for storing single characters...
    Code:
    char x = 'c'; 
    //variable x holds the letter 'c'

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    18
    thanks agian Bench82 how u hold multipule charaters

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by cgsarebeast
    thanks agian Bench82 how u hold multipule charaters
    characters stored contigiously (is that a word? - I mean "stored next to each other in memory" anyway) are generally known as strings... Since you're in the C++ forum, I'll assume you're not using C.. here is the C++ way to do it, using the C++ <string> library :
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
       string s = "Hello, World";
       cout << s;
       cin.get();
    }
    There's a different way to do it in C, which involves character arrays - that works in C++ too.. but in C++, use the method above unless you have a compelling reason to do otherwise

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    18
    o ic thanks but I'll give u an example:

    Code:
     #include <iostream>
    
    using namespace std;
    
    int main ()
    
    {	
    	cout<<"Name 1.0 \n"; 
        cout<<"Please Press Enter \n";
    	cin.get ();
    	
        int x;
    
    	cout<<"Please Input your name: ";
    	cin>> x;
    	cin.ignore ();
    	cout<<"Is this your name?: "<< x <<"\n";
    	cin.get ();
    
    }
    (I know thats the example of the "enter your number" thing on this site but with words changed, I'm useing it 2 learn....)

    well ne ways I want that code 2 display a letter instead of a number and I dont know what varible 2 use or even if u use one

    thanks.....

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    18
    oh and yes I'm useing C++

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Use the string variable that Bench82 showed you. He showed output, but it should be easy to change to input if you know how to input a number.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    18
    thanks, but with that how could the user change it like if I put a set value than if I put the value a sally than if there names not sally it would work?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Just replace int in your code with string (and add the #include <string>). They are both types that can be used in the same way in this case.

    Then try it out and ask questions about the exact code if you get stuck.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    18
    YaY thanks Daved...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 02:12 AM
  2. need to count individual letters in input
    By epox in forum C Programming
    Replies: 12
    Last Post: 05-22-2006, 07:32 AM
  3. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 12:39 PM
  4. problems storing values in varibles
    By stodd04 in forum C Programming
    Replies: 7
    Last Post: 02-08-2005, 11:56 AM
  5. Switching letters
    By XiReDDeViLiX in forum Windows Programming
    Replies: 4
    Last Post: 06-06-2002, 07:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21