Simple question regarding variables

This is a discussion on Simple question regarding variables within the C++ Programming forums, part of the General Programming Boards category; I recently got into C++ through my school. I was bored the other day and decided to make a simple ...

  1. #1
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50

    Question Simple question regarding variables

    I recently got into C++ through my school. I was bored the other day and decided to make a simple Mad Libs program. I have one question though... How do I assign a variable to hold an entire word? Obviously in a Mad Libs program, the user inputs words and those words are plugged into a story for comical results.

    And obviously declaring variables through char doesn't work because it only returns a single letter.

    Any ideas?

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Use the string class.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string input;
        cout << "Enter a word." << endl;
        cin >> input;
        cout << "You entered " << input << endl;
        return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Code:
    #include <string>
    
    string variable; // This will store unlimited amount of chars
    
    getline(cin, variable); // To take input so ' ' spaces are also stored

  4. #4
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    I have #include <string> but it isn't recognizing string. The error I'm getting is...

    error C2065: 'string' : undeclared identifier

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Indeed. Welcome to namespaces.

    You have a few options to choose from.
    1) using namespace std;
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    ...
    2) using std::string;
    Code:
    #include <iostream>
    #include <string>
    
    using std::string;
    ...
    3) Full qualification of names
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string input;
        ...
        return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    All three methods give me quite a few errors that look like...

    error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    <wildguess>
    Don't use <iostream.h>. Use <iostream>.
    </wildguess>

    This episode of mind-reading was brought to you by the letter Q and the number 5. To prevent future attempts at mind-reading, post some code with your error messages.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    Code:
    // stupid libs
    
    #include <string>
    #include <iomanip.h>
    #include <iostream>
    
    int choice;
    
    using std::string;
    
    void intro()
    {
    	cout<<"                     ------------Stupid Libs v1.0------------"<<endl<<endl;
    	system("pause");
    	cout<<" "<<endl;
    	cout<<"You will be prompted for various types of words, \ninput them or suffer a punch in your crappy face."<<endl<<endl;
    	system("pause");
    	cout<<" "<<endl;
    	cout<<"First... lets go over the basics, just incase you forgot that garbage already."<<endl;
    }
    
    void words()
    {
    	cout<<" "<<endl;
    	system("pause");
    	cout<<"			Parts of Speech"<<endl;
    	cout<<" "<<endl;
    	cout<<"Noun:  A person, place or thing."<<endl;
    	cout<<" "<<endl<<endl;
    	cout<<"Proper Noun:  A paticular thing, event, or a \nname of a person.  Often capitalized."<<endl;
    	cout<<" "<<endl<<endl;
    	cout<<"Pronoun:  Indicate the subject of the sentence, \nsuch as it, he, her, we or they."<<endl;
    	cout<<" "<<endl<<endl;
    	cout<<"Adjective:  Describes or modifies a noun, often \nhas a suffixe such as -able, -ous, -er, or -est."<<endl;
    	cout<<" "<<endl<<endl;
    	cout<<"Verb:  An action, such as run, jump or take."<<endl;
    	cout<<" "<<endl<<endl;
    	cout<<"Adverb:  Describes or modifies a verb, such as \nvery, rapidly or unrelentlessly."<<endl;
    	cout<<"\n"<<endl;
    	system("pause");
    }
    
    void libchoice()
    {
    	cout<<"\n\n\n\n\n\n\n\n\n"<<endl;
    	cout<<"Choose lib 1, 2 or 3."<<endl;
    	cout<<" "<<endl;
    	cout<<"Input lib choice now: ";
    	cout<<"\n\n\n\n\n\n\n\n\n\n"<<endl;
    	cin>>choice;
    
    	cout<<" "<<endl;
    }
    
    void liba()
    {
    
    	string a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r;
    
    cout<<"Adjective : ";
    cin>>a;
    cout<<""<<endl;
    cout<<"Adjective : ";
    cin>>b;
    cout<<""<<endl;
    cout<<"Adjective : ";
    cin>>c;
    cout<<""<<endl;
    cout<<"Adjective : ";
    cin>>d;
    cout<<""<<endl;
    cout<<"Liquid : ";
    cin>>e;
    cout<<""<<endl;
    cout<<"Noun : ";
    cin>>f;
    cout<<""<<endl;
    cout<<"Noun : ";
    cin>>g;
    cout<<""<<endl;
    cout<<"Noun : ";
    cin>>h;
    cout<<""<<endl;
    cout<<"Noun : ";
    cin>>i;
    cout<<""<<endl;
    cout<<"Noun : ";
    cin>>j;
    cout<<""<<endl;
    cout<<"Noun : ";
    cin>>k;
    cout<<""<<endl;
    cout<<"Noun : ";
    cin>>l;
    cout<<""<<endl;
    cout<<"Noun : ";
    cin>>m;
    cout<<""<<endl;
    cout<<"Number : ";
    cin>>n;
    cout<<""<<endl;
    cout<<"Plural Noun : "; 
    cin>>o;
    cout<<""<<endl;
    cout<<"Plural Noun : ";
    cin>>p;
    cout<<""<<endl;
    cout<<"Plural Noun : ";
    cin>>q;
    cout<<""<<endl;
    cout<<"Plural Noun : ";
    cin>>r;
    cout<<""<<endl;
    system("pause");
    cout<<""<<endl;
    cout<<"            Proverbs"<<endl;
    cout<<"Too many "<<o<<" spoil the "<<e<<"."<<endl; 
    
    cout<<"People who live in "<<a<<" houses shouldn't throw "<<p<<"."<<endl; 
    
    cout<<"The shortest distance between two "<<q<<" is a straight "<<6<<"."<<endl;
    
    cout<<"Love of "<<r<<" is the root of all evil."<<endl; 
    
    cout<<"A "<<b<<" stone gathers no "<<g<<"."<<endl;
    
    cout<<"A "<<h<<" a day keeps the "<<i<<" away."<<endl; 
    
    cout<<"A "<<j<<" in time saves "<<n<<"."<<endl;
    
    cout<<"You can't keep a "<<c<<" man down."<<endl; 
    
    cout<<"You can't teach a "<<d<<" "<<k<<" new tricks."<<endl; 
    
    cout<<"The way to a man's "<<l<<" is through his "<<m<<"."<<endl;
    }
    
    void libb()
    {
    	cout<<"Lib b"<<endl;
    }
    
    void libc()
    {
    	cout<<"Lib c"<<endl;
    }
    
    void main()
    {
    
    	for (int run=0; run < 1; run++)
    	{
    	intro();
    	words();
    	libchoice();
    		if (choice == 1)
    			liba();
    		else if (choice == 2)
    			libb();
    		else if (choice == 3)
    			libc();
    		else
    			cout<<"Invalid choice, please pick between 1, 2 or 3."<<endl;
    			libchoice();
    
    	}
    }
    Sorry, theres what I have.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Add these as well:
    Code:
    using std::cin;
    using std::cout;
    using std::endl;
    Or alternately you can include standard namespace:
    Code:
    using namespace std;

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >#include <iomanip.h>
    And this should be:
    #include <iomanip>

  11. #11
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Flakster
    I recently got into C++ through my school. I was bored the other day and decided to make a simple Mad Libs program. I have one question though... How do I assign a variable to hold an entire word? Obviously in a Mad Libs program, the user inputs words and those words are plugged into a story for comical results.

    And obviously declaring variables through char doesn't work because it only returns a single letter.

    Any ideas?
    If a char holds a single letter, to hold a word you need lots of letter. You may use the string class that does lots of work for you, or you can learn the basic stuff which would be using and char array
    Code:
    char letter = 'z';
    char word1[] = "a word";//an initialized array
    char word[10];//a array with random not-initialized values and size 10 chars
    The class string internaly stores an array and does all needed manipulation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Question!!
    By gameuser in forum C++ Programming
    Replies: 2
    Last Post: 06-06-2009, 05:42 PM
  2. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  3. simple question about variables and loops
    By InvariantLoop in forum C Programming
    Replies: 2
    Last Post: 01-26-2005, 08:47 AM
  4. Data Storage Question, and Dynamic variables?
    By Zeusbwr in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2004, 11:01 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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