I tried to compare std::cin and scanf, I expected them to have same behavior, but actually not:
#include<iostream>
using namespace std;
int main()
{
int i;
cin>>i;
return 0;
}
This receives an input from user, no problem.
But
#include<stdio.h>
int main()
{
int i;
scanf("%d\n",&i);
return 0;
}
I input a integer, program doesn't terminate even if I press "enter" multiple times, unless I enter another integer number.
Note there's an "\n" in scanf's format string.
So I tried to add one statement
printf("%d\n", i);
Well it prints out the 1st number I just input. This is correct but weird, why after scanf, the program requires me to enter any character rather then \n, to exit the program?
I tried both VC and GCC, same problem. What is that \n indicating?
scanf("%d\n",&i);
Let's read the manpage of scanf :
Whitespace character: the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace). A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none).
What it means is scanf will search for a integer followed by an optionnal whitespace. that's why he's waiting for you to use two times enter. If you used "%d\n\n", it'll have to be three times. And so on.
If you want only an interger, use scanf("%d",&i);
scanf("%d\n",&i); is equivalent to std::cin >> i >> std::ws;.
If you want the same behaviour for scanf, remove \n: scanf("%d",&i);
This is caused by the fact that any whitespace character in scanf means "skip input until non-whitespace is found"
Related
In C++ , Is this a valid/efficient way to input a series of characters ?
As in :
char abc[100];
cout<<"Enter your address:";
cin>>abc;
This says :
However, cin extraction always considers spaces (whitespaces, tabs, new-line...) as terminating the value being extracted, and thus extracting a string means to always extract a single word, not a phrase or an entire sentence.
Since the above statement is given for Strings and not character arrays , I am confused .
Will this code work properly ?
Note : I am using TurboC++
If you want to read only non-whitespace characters, you can use:
char abc[100];
cout<<"Enter your address:";
cin >> abc;
However, that is risky. If the sequence of non-whitespace characters consists of 100 or more characters, you will run into the problem of accessing memory out of bounds, which will lead to undefined behavior.
If you want to read everything up to a the newline character, you can use:
char abc[100];
cout<<"Enter your address:";
cin.getline(abc, 100);
This has the added advantage that it will not read more than 99 character, saving the last space for the null terminator.
More details on istream::getline() can be found at http://en.cppreference.com/w/cpp/io/basic_istream/getline.
I'm writing a simple program that asks a user to input a letter. And then I want him to input a phrase. When it's just a single string I don't have a problem.
char c;
string s;
cin >> c;
cin >> s;
But when I want a phrase
cin >> c;
getline(cin, s);
When I run the program after the user inputs a letter and hits enter I don't get a chance to input the phrase. Of course if I type the character and enter a phrase after a space the program works fine.
On the contrary when I input a character and then enter a single string after a space using the first method, it won't record the string.
Why does it do this?
When ever you write a character and press enter, actually 2 char are send to the buffer: the character itself and \n as the result of the enter key press. Your cin >> c; reads the first character only while \n still remains in the buffer. Since std::getline() reads everything before it encounters a \n character and since \n is the first character it encounters it doesn't read anything and the program terminates. Add a std::cin.ignore(); (to ignore the \n) before std::getline and the code will work.
Since c only takes a character, the enter key that the user presses is given to s so anything other than that is not recorded. Only press a single key and no enter for the character. Or else just use a string anyway.
I'm doing an exercise in C++ Primer and basically I am using a switch statement to count the number of vowels in a text that I input.
I input the text using a while loop.
while(cin >> ch)
and proceed with the cases, a, e, i, o, u, incrementing an integer variable for the respective cases. Now the next part of the question says also count the spaces, tabs and newlines.
I tried doing
case ' ':
and so forth using '\t' and '\n'. But it seems like it doesn't compute these cases. I also tried just using a default and using an if else statement
default:
if(ch == ' ')
++space;
etc. But this doesn't proceed either. I also tried putting in the integer values of ' ', '\t', '\n'. What am I doing wrong here? Also, I know that if I use isspace() I can count the combined total but I need to compute each one individually. I'm not sure why the equality test won't do the job.
By default, formatted input from streams skips leading whitespace. You need to either disable skipping of leading whitespaces or use one of the functions which won't skip spaces:
std::cin >> std::noskipws; // disables skipping of leading whitespace
char c;
while (std::cin.get(c)) { // doesn't skip whitespace anyway
...
}
As said by Dietmar, white spaces are skipped by default. You could use cin.getline() to provide your own string delimiter instead of white space characters. I would say this is a generally easier way of reading input compared to using cin.get().
Here is a code snippet. I'm confused as to how the buffering internally works.
while(true)
{
cout << "Enter a character: ";
cin.ignore(3, '\n');
ch = cin.get(); // ch is char type
cout << "char: ch: " << ch << endl;
}
Actually cin.ignore(3, '\n') ignores the first three characters and then gets the next immediate character. Till that point its fine. Since, I kept this in a while loop, I was trying to check the behavior of ignore() and get(). For instance, the output for which I checked was
Enter a character: abcd
char: ch: d
Enter a character: efgh
char: ch: e
Enter a character: ijkl
char: ch: i
Enter a character: mnopq
char: ch: m
Enter a character: char: ch: q
Enter a character:
Just to check the buffering, intentionally I was give 4 characters instead of 1. In the first case, its fine and got it. From second, the ignore doesn't seem to work. When I entered 5 characters, I din't get the behavior.
Need explanation on this. :)
According to documentation of std::cin.ignore(streamsize n = 1, int delim = EOF):
Extracts characters from the input sequence and discards them, until either n characters have been extracted, or one compares equal to delim.
http://www.cplusplus.com/reference/istream/istream/ignore/
You are putting abcd\n onto stdin. Your first ignore(3,'\n') removes abc and your get() fetches d. \n remains in the buffer.
Then you add efgh\n to the buffer which now contains \nefgh\n. Your next ignore() reads either 3 characters or a newline, whatever comes first. Since your newline is first in the buffer, only the newline is ignored.
You probably want to empty the stdin buffer before asking for more input. You can achieve this either by modifying your get() call, or by adding a second ignore() call before asking for more input.
cin.ignore(3, '\n') ignores up to three characters, stopping after it finds the end of a line (i.e. a \n character).
After the first line of input, the buffer will contain 5 characters, abcd\n. So ignore ignores abc, and get gets d, leaving \n.
After the second line, it contains \nefgh\n. So ignore just ignores the end-of-line character, and get returns e.
If you want to discard the rest of line after extracting the character, then use ignore again:
cin.ignore(numeric_limits<streamsize>::max(), '\n');
I have a question about the difference between these two pieces of code:
char buffer5[5];
cin.get(buffer5, 5);
cout << buffer5;
cin.get(buffer5, 5);
cout << buffer5;
and
char buffer4;
while (cin.get(buffer4))
{
cout << buffer4;
}
In the first piece of code, the code gets 5 characters and puts it in buffer5. However, because you press enter, a newline character isn't put into the stream when calling get(), so the program will terminate and will not ask you for another round of 5 characters.
In the second piece of code, cin.get() waits for input to the input stream, so the loop doesn't just terminate (I think). Lets say I input "Apple" into the input stream. This will put 5 characters into the input stream, and the loop will print all characters to the output. However, unlike the first piece of code, it does not stop, even after two inputs as I can continuously keep inputting.
Why is it that I can continuously input character sequences into the terminal in the second piece of code and not the first?
First off, "pressing enter" has no special meaning to the IOStreams beyond entering a newline character (\n) into the input sequence (note, when using text streams the platform specific end of line sequences are transformed into a single newline character). When entering data on a console, the data is normally line buffered by the console and only forwarded to the program when pressing enter (typically this can be turned off but the details of this are platform specific and irrelevant to this question anyway).
With this out of the way lets turn our attention to the behavior of s.get(buffer, n) for an std::istream s and a pointer to an array of at least n characters buffer. The description of what this does is quite trivial: it calls s.get(buffer, n, s.widen('\n')). Since we are talking about std::istream and you probably haven't changed the std::locale we can assume that s.widen('\n') just returns '\n', i.e., the call is equivalent to s.get(buffer, n, '\n') where '\n' is called a delimiter and the question becomes what this function does.
Well, this function extracts up to m = 0 < n? n - 1: 0 characters, stopping when either m is reached or when the next character is identical to the delimiter which is left in the stream (you'd used std::istream::getline() if you'd wanted the delimiter to be extracted). Any extracted character is stored in the corresponding location of buffer and if 0 < n a null character is stored into location buffer[n - 1]. In case, if no character is extracted std::ios_base::failbit is set.
OK, with this we should have all ingredients to the riddle in place: When you entered at least one character but less than 5 characters the first call to get() succeeded and left the newline character as next character in the buffer. The next attempt to get() more characters immediately found the delimiter, stored no character, and indicated failure by setting std::ios_base::failbit. It is easy to verify this theory:
#include <iostream>
int main()
{
char buffer[5];
for (int count(0); std::cin; ++count) {
if (std::cin.get(buffer, 5)) {
std::cout << "get[" << count << "]='" << buffer << "'\n";
}
else {
std::cout << "get[" << count << "] failed\n";
}
}
}
If you enter no character, the first call to std::cin.get() fails. If you enter 1 to 4 characters, the first call succeeds but the second one fails. If you enter more than 4 characters, the second call also succeeds, etc. There are several ways to deal with the potentially stuck newline character:
Just use std::istream::getline() which behaves the same as std::istream::get() but also extracts the delimiter if this is why it stopped reading. This may chop one line into multiple reads, however, which may or may not be desired.
To avoid the limitation of a fixed line length, you could use std::getline() together with an std::string (i.e., std::getline(std::cin, string)).
After a successful get() you could check if the next character is a newline using std::istream::peek() and std::istream::ignore() it when necessary.
Which of these approaches meets your needs depends on what you are trying to achieve.