Unformatted vs. formatted input.

Pages: 12
Accoring to what i have read, unformatted input is more efficient that formatted input. I wonder how big the difference actually is and if it is crucioal or not for bigger programs and projects. I am learning atm and have a code example below:

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
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>

using namespace std;

int main () {

	char name[100];
	char id[7];
	int age;
	
	ifstream fin;
	ofstream fout("info.txt");

	cout << "Enter your name: ";
	cin.getline(name, 100, '\n');
	cout << "Enter your id: ";
	cin.getline(id, 7, '\n');
	cin.ignore(1000, '\n');
	cout << "Enter your age:";
	cin >> age;

	fout << "Name: " << name << endl << "ID: " << id << endl <<
		"Age: " << age << endl;
	fout.close();

	fin.open("info.txt");
	if(!fin) {
		cout << "The file: info.txt, could not be opened." << endl;
		exit(EXIT_FAILURE);
	}
	cout << "--------Info for person " << id << ": --------" << endl;
	cout << fin.rdbuf();
	fin.close();

	system("pause");
	exit(EXIT_SUCCESS);
	return 0;
}


As you can see, I use the istream functions for avoid formatted input. But I find it harder to make it work, if the user inputs too much data in the stream.
If the user inputs too much data for the charr array id, which only can take 6 characters since one element is reserved for '\0', data is left in the stream for the input for integer variable age. Because of this, the user will not be able to input the age since the extra data that was input before, is stored in it automatically and then the program exits.

It would be easier and more lazy to just use formatted input with the >> operator, since it stops after hitting the first white character automatically after the reading.

What is the best for simple input? cin >> or cin functions for efficiency? How can I empty the cin stream to solve the problem above?
Last edited on
I don't know what you mean by formatted vs. unformatted input.

If you use >>, it basically has the same effect as calling getline except it uses a different end-of-string marker by default.

But the speed of any user input is going to be bottlenecked by the user (people don't move anywhere near as fast as computers), so optimizing this kind of thing doesn't really do much for you.



Basically: There's little/no difference. Don't worry about it.
According to my book, you use the operator for formatted input and the other functions for unformatted input. Is that not correct

Anyways I solved the problem with the operator instead and with a few ignore calls.

I still do not understand the difference of how to do formatted and unformatted input, since you questioned it.
Last edited on
Formatted input/output is ASCII input that needs to be converted to internal binary representation and vice versa.
Unformatted data is just raw binary data.

If you have a user directly feeding you input it will almost always be formatted, since it will be in ASCII format (most likely).

This also explains why unformatted is faster, since the binary values are just directly stored in the variable without undergoing further parsing.
Ok, I think that I got it now. So for example (just to clarify):

int n = 0;
char ca[100];

cin >> n; // Is formatted input since it converts the ACSII chars from the keyboard to an internal int in the program

cin>>ca; // Is unformatted input since the ASCII chars are stored directly into the char array.

It does not matter which function im using, it all depends on the variable type and the input source right?
Uh, I don't think that's what blacksheep said, he said if the user enters something through cin it will be formatted, if the user 'enters' a binary representation of what he wanted to send through a stream it will be unformatted (perhaps through a file written to in binary, or similar)
Last edited on
So in other words all three of these functions: cin.operator>>, cin.getline() and cin.get() will always get formatted data in ASCII char code, as long as the cin stream is connected to the Standard Input/keyboard?

(If you wonder why I am so confused about this it's my book that said that, the >> operator is used for formatted input and the other functions for unformatted input.)

Thanks for all the help.
Last edited on
I don't know, but that is what I would have assumed.
YOU NEED TO CLEAR THE STREAM. You can do this using cin.sync()
I believe the reason it tries to stress the formatted and unformatted is because when you cin >> a variable, cin reads from the keyboard, formats it, then pushes it to the variable. The other two read directly from the buffer and waits for the delimiter to push it to the variable. I'm not 100% sure about how it works on the back end, but I know cin does "magic" that the other two don't. This is why if a character is left in the buffer, the cin.get and getline will capture that and if it's a delimiter, it'll move right on to the next line.

Edit: I believe cin.sync() is something that's to be avoided. I heard it does different (?) things on each system. I don't use it, but I'll have to look into it now.
Last edited on
Formatted input/output is ASCII input that needs to be converted to internal binary representation and vice versa.
Unformatted data is just raw binary data.


But he's working with strings here... not integers/floats... so the data he's trying to get is ASCII anyway.

But whatever... he marked it as solved so he must've figured out his problem. I still don't understand what he was talking about though. =x
Last edited on
Disch the main problem for me is that I am trying to get the whole picture of how the input works and the differences between the functions whether it is unformatted or not. Since I am a beginner I do not know much about this and it seems like my book did not explain ths so well to me so it just confused me. But I will keep coming back and read your replies when I forget what you said.

The reason fir why I marked at as solved is that I solverd the problem above by only using cin and avoid cin.getline() and by using ignores.

However, any who think htat they can explain this even further or who know good lins about how this works in detail, please tell me. I am still struggling to understand it since I want to udnerstand everything in detail and make sure that I do not think wrong.
@Volatile Pulse
No, it shouldnt. It is supposed to sync the buffers, which means anything left in the buffer after the user has inputted, should be erased. try running these two codes. Enter more than 1 word into each of them. You will notice they react differently.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string line;
    cout<< "Name: "<< endl;
    cin>> line;
    cin.sync();
    cout<< "Date"<< endl;
    cin>> line;
    cin.sync();
    cout<< "Whatever..."<< endl;
    cin>> line;
    cin.sync();
    return 0;
}


Whithout Sync:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string line;
    cout<< "Name: "<< endl;
    cin>> line;
    cout<< "Date"<< endl;
    cin>> line;
    cout<< "Whatever..."<< endl;
    cin>> line;
    return 0;
}


Compile and run those. Enter somthing like "Marry went to the market" into both compiled programs. The 2nd one will skip the next two inputs, while the first will wait for each.

What cin does is get only 1 peacie of data. Using cin, you only get 1 string at a time. This means it will recognize spaces as a sparator for each string. This is the same for any piece of data (whether it be integers, floats, or strings). Because of this, we should use getline(cin, variable) to get a string of an entire line. cin input can be useful in other ways. A good example is if you have a database full of names. All you have to do to get each one is put it into a file, separate them with spaces (naturally), and whenever you need them just ifstream in; in>> strvar until the end of the file.

Because we sync the istream after getting input with cin, whatever data is still in the buffers is deleted to achieve the sync. Therefore, then we ask for input again using cin, there is nothing in the buffer to get and so it prompts the user.
Last edited on
I've never heard of .sync, I use .clear followed by .ignore
I've never heard of .sync, I use .clear followed by .ignore


Just like me. Only that .clear does not sound familiar.
Last edited on
@IWishIKnew
You describe the behaviour in VC++. This behaviour is not guaranteed by the standard and is not how cin.sync() works in GCC.
Last edited on
I just tested it, they do different things:

1
2
3
std::cin >> var;
std::cin.clear();
std::cin.ignore();


will stop infinite loops if the input entered doesn't match the input type of var, but doesn't flush the buffer.

1
2
std::cin >> var;
std::cin.sync();


flushes buffer, but doesn't stop infinite loops from invalid input



Last edited on
Ignore takes two arguments:
istream& ignore ( streamsize n = 1, int delim = EOF );


There are many "best" ways to do it, but for general keyboard input:
cin.ignore(80, '\n');
will be all you need.

What does sync do exactly (or generally :))?
Last edited on
It syncs the istream input buffer.
It syncs the istream input buffer.

What is that supposed to even mean?

(for reference, fflush(stdin) is undefined, cin.sync() is implementation-defined and thus useless in portable code - in many compilers it does exactly nothing)
Last edited on
Pages: 12