Issues with getline

I've been making a little binary converter over the past week just to see if I could. It's working, too! :D

Now the problem is that when I get input from the user using cin, it leaves out anything after a space. For example, if I enter in "Hello world" it will only convert "Hello".

To get around this, I tried to use getline, but that does the same thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	cout << "Convert to or from binary? 0=Convert to, 1=Convert from."<<endl; 
	//cout outputs to the screen
	cin >> Mode; //cin takes input from the user via their keyboard
	if(Mode == 0) //Mode 0 = convert to binary
	{
		string OutString; //makes a new string variable to hold the final result
		cout << "Please enter what you would like to convert: " << endl;
		cin.ignore();
		getline(cin,Input); //breaks it
		//cin >> Input;
		stringstream (Input) >> Input2;

Input and Input2 are both of the string datatype.

I looked at the basic I/O page on this site and that shows an example of using cin, but that example works with spaces. Why does mine not work with spaces? How do I fix this?

Thanks :)
Try typing your input in double-quotes.
Input: "This is a test"
Output: 01110100 01101000 01101001 01110011

Missing a few letters :P still not working.
Try using string.find to get the location(s) of the space characters before converting, then store the positions in an array or something. Then, remove the space characters from the string and convert to binary. Add back in the space character to its proper location, which has a binary equivilant of 00100000, back into the converted string and send the binary conversion to the output.
forget about iostream and use gets from stdio.h

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

int main()
{
 char szBuffer[80];

 puts("Enter Some Text, then press [ENTER]");
 gets(szBuffer);
 printf("%s",szBuffer);

 return 0;
}
Last edited on
I tried using gets, and that ALMOST works. It gets everything EXCEPT the spaces for some reason.
So if I gave it "this is a test" it outputs "01110100 01101000 01101001 01110011 01101001 01110011 01100001 01110100 01100101 01110011 01110100" which, converted, is "thisisatest".

D:.

I can't use string.find because just inputting something into the string ignores the spaces - like I said, the moment it finds a single space it omits everything after and including the space.

Thanks for the help so far though :D

I've seen other programs use cin with no issues, so why do I have issues?
I entered 'ab cd' (without quotes) and got this (see below)...

10000110 01000110 00000100 11000110 00100110

Is that what you want???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <string.h>

void BinaryOut(char c)
{
 int iMask;

 for(unsigned int i=0; i<8; i++)
 {
     iMask=1<<i;
     printf("%u",!!(iMask & c));
 }
}

int main()
{
 char szBuffer[256];

 gets(szBuffer);
 for(unsigned int i=0; i<strlen(szBuffer); i++)
 {
     BinaryOut(szBuffer[i]);
     printf(" ");
 }

 return 0;
}

/*
ab cd
10000110 01000110 00000100 11000110 00100110
*/


My program's 5.5k.

From re-reading your posts, its quite clear that your problem isn't getting the spaces between the words, but whatever you are doing to output the 1s and0s are skipping space characters. What is a space; 32 I think. My program above is outputting a 32 for the 3rd char, which is ...

1
2
3
4
5
6
7
8
9
10
0   X  2 ^ 0  =  0
0   X  2 ^ 1  =  0
0   X  2 ^ 2  =  0
0   X  2 ^ 3  =  0
0   X  2 ^ 4  =  0
1   X  2 ^ 5  =  32
0   X  2 ^ 6  =  0
0   X  2 ^ 7  =  0
===================
                 32
Last edited on
No, it's not my output. If I debug in C++ express and look at what's in the Input variable, it only has one word. I'm sure it's something wrong with the way I get input.

EDIT: Just saw that's with gets and not cin. Lemme use gets again and see if I can fix it.
It seems it is a problem with the output. For some reason whenever it gets to 32 it skips that character.
Here is my code that does the conversion and outputs it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
for(unsigned i = 0; i <= /*Input.length()-1*/sizeof(Input); i++) 
		{
			//cout << "Converting " << Input[i] << endl;
			unsigned short Character = (unsigned)Input[i]; 
			unsigned short Bin=0;
			char EightBit[] = {'0','0','0','0','0','0','0','0'}; 
			//cout << "Character == " << Character << endl;
			unsigned short Temp = 1; 
			while(Character > 0)
			{
				Bin = Character % 2;
				Character = Character / 2;
				if(Bin == 1 || Character == 1 || Character == 0) EightBit[sizeof(EightBit)-Temp] = '1'; 
				Temp++;
				if(Temp == sizeof(EightBit))
				{
					for(unsigned short j = 0; j < 8; j++) OutString+= EightBit[j];
					OutString+= " ";
					Temp = 1;
				}
				if(Character == 0) break; 
			}
		}


Whenever it gets to a space, it seems to just forget about writing to OutString. It does all the conversion but just doesn't write to it. How does it make sense that it works for ever other character except 32?
Last edited on
If you post your entire program I'll try to figure it out. It would be a learning experience for me, because I don't use any of the high level iostream stuff for console i/o, but rather the older C based stdio stuff, which, if you ran my program, you would see works as expected. However, I guess I ought to become more familiar with it, since that's what everybody but me uses. On the other hand, I mostly do Windows GUI coding, and iostream isn't much use there, but stdio.h is.

Use cin.get. http://www.cplusplus.com/reference/iostream/istream/get/

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
  char buffer[100];

  std::cin.get(buffer, 100);
  std::cout << buffer;
  return 0;
}


When I executed this code and entered "asdf asdf" it printed it out both words successfully.
Topic archived. No new replies allowed.