Sign in to follow this  

String Objects and cin

This topic is 4310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I have had several previous problems with my stupid program. I have smoothed 99% of them out and no longer get any error, but I don't get this one thing.... If is use cin to store to a string object, then I used Cout to print it, it doesn't print spaces, instead it splits the string? (I actually use my string pump Add() and Send() functions, but it is equvilent to cout, or is that my problem? My code:
#include <cstdlib>
#include <string>
#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
#include "StringPump.h"


using namespace std;



int main(int argc, char *argv[])
{
bool game_active = true;


StringPump sp; //Create A string Pump
sp.Add("Green",GREEN);
sp.Add("Dark Blue",BLUE);
sp.Add("Red", LIGHT_RED);
sp.Send();
sp.Send();
sp.Send();
     
           
string ra;
   do
   {
       
cin>>ra;
sp.Add("The orc charges you with a spear", LIGHT_RED);
sp.Add("You take 35 Damage", WHITE);
sp.Add("It is now your turn", BLUE);
sp.Add(ra, GREEN);              
               
sp.Sendx(4);
}while(game_active);           
          
    system("PAUSE");
    return EXIT_SUCCESS;
}

and here is the string pump file:

//String Pump .h

#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
#include <queue>
#define CONS_HANDLE GetStdHandle(STD_OUTPUT_HANDLE)

using namespace std;

enum
{
	BLACK, 
	BLUE, 
	GREEN, 
	AQUA, 
	RED, 
	PURPLE, 
	YELLOW, 
	WHITE, 
	GRAY, 
	LIGHT_BLUE, 
	LIGHT_GREEN, 
	LIGHT_AQUA, 
	LIGHT_RED, 
	LIGHT_PURPLE, 
	LIGHT_YELLOW, 
	BRIGHT_WHITE
};



     

class StringPump
{
      protected:
                queue<int> Clist;
                queue<string> Mlist;


public:
       ~StringPump(){Clear();}
       
       void SetColor(int color)
{
SetConsoleTextAttribute(CONS_HANDLE, color | FOREGROUND_INTENSITY);
}
       
       void Add(string text, int color)
       {
                  Mlist.push(text);
                  Clist.push(color);
       }
       
       void Send()
       {
       SetColor(Clist.front());
             cout << Mlist.front() << endl;
            
             SetColor(WHITE);
             system("PAUSE");
             Remove();
             system("cls");
       }
       
       void Remove()
       {
             Clist.pop();
             Mlist.pop();
       }
       
       void Clear()
       {
            while(!Mlist.empty())
            {
            Remove();
            }
       }
       
       void Force()
       {
            while(!Mlist.empty())
            {
            SetColor(Clist.front());
             cout << Mlist.front() << endl;
             Remove();
             }
       }
       
       void Sendx(int num)
       {
            if(num > Mlist.size())
            num = Mlist.size();
            
            int counter;
            for ( counter=1; counter <= num; ++counter )
            {
            SetColor(Clist.front());
             cout << Mlist.front() << endl;
             Remove();
             } 
             SetColor(WHITE);
             system("PAUSE");
             system("cls");
             }
};

Share this post


Link to post
Share on other sites
cin gets the string until it gets to a space and then stops. so it doesn't include spaces. i think there might be cin.readline() or something like that which would include spaces. or you could just add a ' ' (space) to the string.

Share this post


Link to post
Share on other sites

This topic is 4310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this