Can't find spaces

This is a discussion on Can't find spaces within the C++ Programming forums, part of the General Programming Boards category; This code is supposed to find a space in a character array and put the seperated string at the space ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    Can't find spaces

    This code is supposed to find a space in a character array and put the seperated string at the space into seperate strings, but doesn't quite work right... It will find any other letter except a space.

    Code:
    void Input(char Input[])
    {
    	int x;
    	int iArg;
    	string sArg;
    	string sArg2;
    	sArg = "";
    	sArg2 = "";
    	char space;
    	space = ' ';
    	iArg = 0;
    	for(x = 0;x < strlen(Input);x++)
    	{
    		if(iArg == 0)
    		{
    			if(Input[x] == space)
    			{
    			iArg = 1;
    			}
    			else
    			{
    				sArg = sArg + Input[x];
    			}
    		}
    		if(iArg == 1)
    		{
    			sArg2 = sArg2 + Input[x];
    		}
    	}
    	cout << sArg << endl;
    If I substitute space = ' '; with space = 'h';, it works fine and splits the string at the first h it sees. Anyone know whats wrong?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I put that code into a simple console app and it worked fine for me. What is your test string and how are you calling the function? I used "Test this." and it gave me:
    Code:
    Test
     this.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    Yes... it does do that. But thats not quite what its supposed to do. I put in "hey you", it gives me

    hey
    you

    if I use a space. It does seem to seperate them, but I have no idea why.

    If I use another char, i.e. 'h', it will put out this when I put in "lookhere"

    look

    which is what its supposed to do (splits it at h and gives me everything in front of it)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,259
    Basically you're taking a C string and converting it into 1 or more C++ STL strings based on a separator character?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    Sure... maybe... but thats not the problem. The problem is that it doesn't seperate them properly at the space. If I tell it to output SPACE! like so:

    Code:
    for(x = 0;x < strlen(Input);x++)
    	{
    		if(Input[x] == ' ')
    		{
                                    cout << "SPACE!" << endl;
    		}
    	}
    it won't do it. If I replace ' ' with some letter, like 'd', it will output SPACE!.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,259
    Sounds strange indeed.

    This is what I suggest:
    write test output to a textfile, then check the contents of the textfile with a hex editor.

    You need to know what exactly is going on.
    You could even try substituting 32 or 0x20 for ' ' and test.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,259
    and put the seperated string at the space into seperate strings
    hmm... it is a little confusing here.
    It looks like your intention is merely to output the part of the string before the space.

    In that case, wouldnt something as simple as:
    Code:
    #include <iostream>
    #include <cstring>
    
    void Input(char str[]) {
    	int len = strlen(str);
    	for (int i = 0; i < len; i++) {
    		if (str[i] == ' ')
    			break;
    		else
    			std::cout<<str[i];
    	}
    }
    
    int main() {
    	Input("Test this.");
    	std::cin.get();
    	return 0;
    }
    work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    that works, but why won't mine? It does the same thing. I don't want just the string before the space, I want both before and after(I haven't finished that part yet, its only supposed to get whats before it). There is no problem with getting them. The only problem is that the statement
    Code:
    if (Input[x] == ' ')
    does not return true if Input[x] is a space. Perhaps seeing where it is used in my .cpp will be some help.

    Code:
    char cmd[256];
    done = false;
    
    while(!done == true)
    {
    	cin >> cmd;
    	Input(cmd);
    Code:
    void Input(char Input[])
    {
    	int x;
    	int iArg;
    	string sArg;
    	string sArg2;
    	sArg = "";
    	sArg2 = "";
    	char space;
    	space = 'h';
    	iArg = 0;
    	for(x = 0;x < strlen(Input);x++)
    	{
    		if(iArg == 0)
    		{
    			if (Input[x] == ' ')
    			{
    			iArg = 1;
    			}
    			else
    			{
    				sArg = sArg + Input[x];
    			}
    		}
    		if(iArg == 1)
    		{
    			sArg2 = sArg2 + Input[x];
    		}
    	}
    cout << sArg << endl;
    }
    Though, somehow, it was working once before, but I changed it temporarily for other things.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,259
    Try and explicitly confirm your hypothesis that if (Input[x] == ' ') does not return true for a space.
    One way would be to immediately output something when a space is supposed to have been reached, and then ensure that the buffer is flushed by using endl.

    Incidentally, I would advise you to name your string str, or at least use a name other than Input since Input is already used as a function name.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    Actually I've just tried something... nothing at all is wrong with the function Input. Somethings wrong in the main loop.

    This does not work if I type in "try this" when the program runs.
    Code:
    char cmd[256];
    done = false;
    
    while(!done == true)
    {
    	cin >> cmd;
    	vInput(cmd);
    
    	//if(cmd == "q")
    	//{
    	//	done = true;
    	//}
    }
    But it will if I specify the string in the code.
    Code:
    char cmd[256];
    done = false;
    
    while(!done == true)
    {
    	cin >> cmd;
    	vInput("try this");
    
    	//if(cmd == "q")
    	//{
    	//	done = true;
    	//}
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,259
    oh, sorry.

    my mistake, I should have spotted that too.

    The problem is that cin>>varname reads into varname until the first whitespace character.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    Ah... thanks. Any easy way to overcome that?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,259
    Read this reference page

    A possibility would be to try out cin.getline() instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the >> operator will stop char input into cmd whenever a non-leading whitespace char is encountered. To put the phrase "try this" in cmd you will need to use getline(). Since cmd is a C style string the syntax could be something like this:

    cin.getline(cmd, 255);

    There is a potential hitch if you call >> in your program before you call getline(). That is >> won't clear the terminating whitespace char from the input buffer when it encounters it. Therefore, if the terminating char for >> is the newline char, and the terminating char for getline() is also the newline char (and it is when default terminating char is used as in the syntax above), then getline() will see the newline char as the first input char and not put anything at all into cmd. Therefore, whenever you use both >> and getline() in the same program it is a god idea to clear the input buffer before the call to getline(). Techniques to do that are extensively discussed in the board, but if you can't readily find it, just ask.

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    Yay! Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 10:13 AM
  2. Tabs or Spaces
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 04-08-2007, 12:45 PM
  3. Can't find DirectX!!!
    By Queatrix in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2006, 08:50 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM

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