Thread: Variables = letters

  1. #1
    C++ Master
    Join Date
    Oct 2005
    Location
    ProgramLand
    Posts
    15

    Variables = letters

    Hey i just started C++ Programming, I have learned include, int main, variables, cout, { and }, I still have yet to learn but none of the tutorials answered my question.
    How do you make variables numbers?

    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
     blah blah blah
    blah blah blah
    if (variablename = letter)
    {
    blah blah blah;}
    when i use numbers it works, but how do i use letters?

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    You want variable numbers, or variable letters?

    c-style strings tutorial: http://www.cprogramming.com/tutorial/lesson9.html
    c++ string class tutorial: http://www.cprogramming.com/tutorial/string.html
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Different types of variables hold different types of information. If declared as a char, a variable will hold letters. If declared as an int, or a float, or almost any other type, it will hold a number. They've very interchangeable - but that's a relatively advanced topic. The best way to learn something is step by step - don't get ahead of yourself. Just continue with your tutorial series or book or whatever you're using, and you'll get to storing andusing letters very quickly.

  4. #4
    C++ Master
    Join Date
    Oct 2005
    Location
    ProgramLand
    Posts
    15
    i mean making a variable A letter. as in if your setting a block of text to rely on a code. If i use letters instead of numbers it fails to compile.

  5. #5

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well post your code and your errors. Minus specifics, it's hard to help.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    if (variablename = letter)
    To compare things you use the comparison operator "==". To assign values to a variable, you use the assignment operator "=". They are different.
    Code:
    #include <iostream>
    #include <string> //string type
    using namespace std;
    
    
    int main()
    {
     
    	char ch = 'a';  //single quotes
    	string my_greeting = "hello world"; //double quotes, #include<string>
    	int num = 10; 
    
    	if(ch == 'a')
    		cout<<ch<<endl;
    
    	if(my_greeting == "hello world")
    		cout<<my_greeting<<endl;
    
    	if(num == 10)
    		cout<<num<<endl;
    
    
    
    	return 0;
    }
    Last edited by 7stud; 11-28-2005 at 10:58 PM.

  8. #8
    C++ Master
    Join Date
    Oct 2005
    Location
    ProgramLand
    Posts
    15
    ok let me break this down this:

    Code:
     #include <iostream>	
    
    using namespace std;
    		
    int main()
    {
      int answer;
      
      cout<<"Do cows fly? yep or nope?";
      cin>> answer;
      cin.ignore();
      if ( answer == yes ) {
         cout<<"Correct!";
      }
      else if ( answer = no ) {
         cout<<"You are terribly mistaken!";
      }
      else {
        cout<<"Invalid answer";
      }
      cin.get();
    }
    doesn't work. While this:

    Code:
     #include <iostream>	
    
    using namespace std;
    		
    int main()
    {
      int answer;
      
      cout<<"Do cows fly? yep or nope?";
      cin>> answer;
      cin.ignore();
      if ( answer == 1 ) {
         cout<<"Correct!";
      }
      else if ( answer = 0 ) {
         cout<<"You are terribly mistaken!";
      }
      else {
        cout<<"Invalid answer";
      }
      cin.get();
    }
    works, how can i make the yes and no not register as a function.

  9. #9
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Read the links I posted, specifically the first half of the second link.

    Code:
     #include <iostream>	
    
    using namespace std;
    		
    int main()
    {
      string answer;
      
      cout<<"Do cows fly? yep or nope?";
      cin>> answer;
      cin.ignore();
      if ( answer == "nope" ) {
         cout<<"Correct!";
      }
      else if ( answer == "yep" ) {
         cout<<"You are terribly mistaken!";
      }
      else {
        cout<<"Invalid answer";
      }
      cin.get();
    }
    Make sure its == too. You can include <string> for more options.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> You can include <string> for more options.

    You should #include <string> even in that simple program. Compilers may not compile that code without it.

  11. #11
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Daved
    >> You can include <string> for more options.

    You should #include <string> even in that simple program. Compilers may not compile that code without it.
    I didn't know that, thanks. Dev-Cpp is so convenient.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    the char datatype can hold single CHARACTERS
    the string datatype can hold multiple CHARACTERS
    you can make an array of Char, to hold multiple CHARACTERS in things you could think of as "holes" maybe, first character starts at variablename[0] next one is at variablename[1]
    ex -
    Code:
    char varnamegoeshere[512];
    o yea and your question to basicly compare strings just make sure your using string variables to compare to other strings
    if(variablestring == "theword")
    or you can write
    if(variablestring == anotherstringvariable)

    and yea about the include <string> , i just found out a while ago as well

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 06:42 PM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. Headers and Global Variables
    By ajoo in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2004, 04:49 AM
  4. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM