Thread: What's the variable type for a text string?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    31

    What's the variable type for a text string?

    Sorry, im about 2 days deep in learning C++. I'm not even sure where to look this up, after trying for some time, I figured i'd just ask.

    All I've learned to use for a variable is int and char.

    The user needs to be able to cin a full sentence and have it stored.

    Thanx in advance!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    string data;
    cin >> data;
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    MK27's method won't store a sentence, it will only store a single word in the variable. Use the following to store a full sentence:
    Code:
    std::string s;
    std::getline(std::cin, s);
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    i actually tried "string" and it caused errors with my code.

    Code:
    // Char.cpp : Defines the entry point for the cConsole application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main()
    {
        int cName;
        int cStr;
        int cNin;
        int cIntel;
        int cAgi;
        int cCon;
        int cSpe;
        int cPoints;
    	
    	cStr = 0;
    	cNin = 0;
    	cIntel = 0;
    	cAgi = 0;
    	cCon = 0;
    	cSpe = 0;
        
        cPoints = (100 - cStr - cIntel - cAgi - cCon - cSpe - cNin);
        cout << "It's time to name your character! Please type your characters name: ";
        cin >> cName;
        cout << "Welcome to the game " << cName << "! You have 100 Character points to spend. These points will be applied to your characters attributes. Lets begin!"
    int cName;

    is what im trying to change. Please ignore the other issues with my code for now lol. Im still trying to figure it out. I tried:

    string cName;

    but my compiler doesn't even recognize "string" as a valid variable type. When i compile with "string" i get about 6 pages of crap but here is the jist.

    1>c:\users\*****\documents\visual studio 2008\projects\char\char\char.cpp(29) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
    Last edited by kamitsuna; 02-22-2010 at 01:15 PM.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You need:
    #include <string>
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    That worked! Ultimately this little program im writing isnt actually for anything, i just wanted to see if i could do it myself without any sort of lesson or tutorial.

    Just for my satisfaction, i got the "string cName;" to work for one word like you said bithub.
    now do i replace that with
    Code:
     std::string cName;
    cout << "It's time to name your character! Please type your characters name: ";
        std::getline(std::cin, cName);
        cout << "Welcome to the game " << cName << "! You have 100 Character points to spend. These points will be applied to your characters attributes. Lets begin!"
    THANX A TON! got it workin
    Incoming double post!
    Last edited by kamitsuna; 02-22-2010 at 01:26 PM. Reason: Answered my own question by just trying it! It worked! Thanx a ton guys!

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Since im here, and you are reading this.
    [ cPoints = (100 - cStr - cIntel - cAgi - cCon - cSpe - cNin);]

    i try to make a call for this variable multiple times after each other variable is cin by the user. But it doesnt seem to keep re-applying the math.

    You start off with 100 points. You spend 23 points on cStr. I want it to subtract cStr from cPoints, then cout the new total.
    Code:
     cout << "You have " << cPoints << " points left!" << endl;
    This is how i make the call for the total later but it just keeps saying 100.
    Any ideas?

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You're probably going to want something like:
    Code:
    std::cout << "You have " << cPoints << " points\n";
    cPoints = cPoints - cStr;
    std::cout << "Now you have " << cPoints << " points\n";
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    I think i get it. then cPoints keeps the value with the cStr subtracted....

    I'd have to add that code after ever cin right?

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Yeah, every time you want to subtract a value from the points.
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Awesome. Bithub you have been very helpful. Thank you very much.

    Keep lookin out for my posts
    I'll have a million more questions im sure.

    Just for fun, here is my code so far

    Code:
    // Char.cpp : Defines the entry point for the cConsole application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <ctime>
    #include <string>
    using namespace std;
    
    int main()
    {
        std::string cName;
        int cStr;
        int cNin;
        int cIntel;
        int cAgi;
        int cCon;
        int cSpe;
        int cPoints;
    	
    	cStr = 0;
    	cNin = 0;
    	cIntel = 0;
    	cAgi = 0;
    	cCon = 0;
    	cSpe = 0;
        
        cPoints = 100;
        cout << "It's time to name your character! Please type your characters name: ";
        std::getline(std::cin, cName);
        cout << "Welcome to the game " << cName << "! You have 100 Character points to spend. These points will be applied to your characters attributes. Lets begin!" << endl;
    	cout << "Strength: Strength is the attribute that applies to how much damage your mutant does. How much strength would you like? (1-25)" << endl;
        cin >> cStr;
        if (cStr > 25)
        {   
            cout << "You're trying to set your Strength too high. Please choose between 1 and 25";
            cin >> cStr;
        }
    	cPoints = cPoints - cStr;
    	cout << "You have " << cPoints << " points left!" << endl;	
    
    	cout << "Intelligence: Intelligence effects your ability to learn new skills and become stronger at those skills. How much cIntelligence would you like? (1-25)" << endl;
    	cin >> cIntel;
    	if (cIntel > 25)
        {   
            cout << "You're trying to set your intelligence too high...which isnt very intelligent! Please choose between 1 and 25";
            cin >> cIntel;
        }  
    	cPoints = cPoints - cIntel;
    	cout << "You have " << cPoints << " points left!" << endl;	
    
    		cout << "Agility: This affects how well your character can land attacks. How much agility would you like? (1-25)" << endl;
    		cin >> cAgi;
    	if (cAgi > 25)
        {   
            cout << "You're trying to set your agility too high! Please choose between 1 and 25";
            cin >> cAgi;
        }   
    	cPoints = cPoints - cAgi;
    	cout << "You have " << cPoints << " points left!" << endl;	
    	
        system ("PAUSE");
    }
    Seems to be workin exactly the way i wanted. Thanx again!
    Last edited by kamitsuna; 02-22-2010 at 02:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM