Thread: Cin auto spliting why?

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    69

    Cin auto spliting why?

    Well Im making a console based app with a bunch of tools for me to use, and right before I started getting it to check the commands I noticed it's treating string diffrently then I expected. If I type in "BLAH" it echos back "<<: !BLAH" as expected, but if I do "BLAH DUR" it echos back "<<: !BLAH <<: <<: !DUR". Instead of "<<:!BLAH DUR". It seems like its spliting automaticly where there is a space. Be warned, im not the best coder. Here's the code.

    main.cpp
    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    #include "textf.h"
    #include "main.h"
    using namespace std;
    TEXTFUNC btext;
    
    int main()
    {	
    	btext.init();
    	return 0;
    }
    textf.cpp
    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "textf.h"
    using namespace std;
    
    void TEXTFUNC::init()
    {	
    	//Assign <<: to harrow before its used
    	harrow = "<<: ";
    	startup();
    }
    void TEXTFUNC::startup()
    {
    	system ("cls");
    	ban("Blade Tools By: Jonathan Bastnagel");
    	getinput();
    }
    void TEXTFUNC::getinput()
    {	
    	string inputchar;
    	ba("");
    	cin>>inputchar;
    	exec(inputchar);
    }
    
    void TEXTFUNC::exec(string execstring)
    {
    
    	ban("!" + execstring);
    	//ban("Exec" + execstring.c_str());
    	getinput();
    }
    void TEXTFUNC::ban(std::string textstring)
    {
    	cout<<harrow<<textstring<<"\n";
    }
    void TEXTFUNC::bn(std::string textstring)
    {
    	cout<<textstring<<"\n";
    }
    void TEXTFUNC::ba(std::string textstring)
    {
    	cout<<harrow<<textstring;
    }
    void TEXTFUNC::b(std::string textstring)
    {
    	cout<<textstring;
    }
    main.h
    Code:
    #ifndef MAIN_H
    #define MAIN_H
    
    class MAIN
    {
    
    public:
    
    };
    
    #endif
    textf.h
    Code:
    #ifndef TEXTF_H
    #define TEXTF_H
    
    class TEXTFUNC
    {
    
    public:
    		//Assigns default values to variables
    	void init();
    		//Starts up, shows about info
    	void startup();
    		//Grabs keyboard input, send's to exec to be checked, and executed
    	void getinput();
    		//Checks inputed characters, and does required functions
    	void exec(std::string execstring);
    		//Outputs text with <<: and new line
    	void ban(std::string textstring);
    		//Outputs text with <<:
    	void ba(std::string textstring);
    		//Outputs text with new line
    	void bn(std::string textstring);
    		//Outputs text
    	void b(std::string textstring);
    
    private:
    		//<<: Arrow string
    	std::string harrow;
    
    };
    
    #endif
    EDIT: I just noticed I accidently put it in C Programming instead of C++ Programming
    Last edited by bladerunner627; 09-25-2005 at 03:21 PM. Reason: Accidently put it in C Programming instead of C++ Programming

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    With cin, the extraction operator only gets upto the first whitespace.

    You want to use getline.

    Code:
    getline(cin, string_name);

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    Quote Originally Posted by Enahs
    With cin, the extraction operator only gets upto the first whitespace.

    You want to use getline.

    Code:
    getline(cin, string_name);
    Wow, thanks that works much better. .

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    Quote Originally Posted by bladerunner627
    Wow, thanks that works much better. .
    I also have another question. I see alot of people using char instead of string. Can someone explain y, which one's better, and the proper defines of char if possible. I haven't understood how to properly use char, but all these functions want char, so I guess its an industry standard.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I also have another question. I see alot of people using char instead of string. Can someone explain y, which one's better, and the proper defines of char if possible. I haven't understood how to properly use char, but all these functions want char, so I guess its an industry standard.
    There are C strings and C++ strings. C strings are arrays of characters, and all the string functions in the C Standard Library expect you to use this convention. C++ strings are classes with functions designed to make working with strings more intuitive. If you're using C++, it's probably best to use STL strings (the string class). If you need to use a function that expects a (char *) as a parameter, you can easily get the C Strings equivalent by using the string.c_str() functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 11:14 AM
  2. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  3. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  4. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM
  5. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM