The No-letter cycle [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Can you explain how does it work, especially while (!(cin >> n) || (cin.peek() != '\n') ?
If I input numbers(1,2,3..) it works.If I input letters (a,b,c,d),it says me to enter one more time.
while (!(cin >> n) || (cin.peek() != '\n'))
{
cin.clear();
cin.ignore(256, '\n');
cout << "enter n ";
}

The first condition in the while loop
!(cin >> n)
checks whether the input was successful.
The second condition
(cin.peek() != '\n')
whether after the input the buffer contains the new line character '\n' that is whether the user pressed the Enter key.
For example if the buffer contains
123A\n
then if an object of an integral type is read then it can get the value 123 and the first condition will evaluate to true though as you see the input in general is invalid.
So this while loop excludes such a situation though if the user appends the input with a blank like
123_\n
where the underscore means a blank then this input also will be rejected. The only valid input is
123\n
That is the loop requires that the user entered a number without any additional symbols after the number and press the Enter key.

Related

Why std::string.size() behaves abnormally in my code? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Here in this code, when I'm entering inputs one by one, I'm getting correct output as expected(correct size of string using std::string.size()), But when I'm entering three or four inputs together(or entering input in bulk) the output (size of string) gets incremented by 2.
Ps: Look once at attached output snippets.
#include <iostream>
int main()
{
int count;
std::cin >> count;
std::cin.ignore();
while (count--)
{
std::string s;
std::getline(std::cin, s);
std::cout << s.size() << '\n';
}
return (0);
}
Edit: I have printed all inputted string and found that at the end, the extra characters are two blank-spaces, as you can see below, (though I tried so far, but still don't know the reason):
5
</h1>
</h1> 7
Clearly_Invalid
Clearly_Invalid 17
</singlabharat>
</singlabharat> 17
</5>
</5> 6
<//aA>
std::cin.ignore(); ignores characters to the EOF. When you enter one line, EOF is met. When you enter several lines, EOF is not met, but \n is met. Thus the next getline after the input 5 returns the empty string, the length is 0.
When consuming whitespace-delimited input (e.g. int n; std::cin >> n;) any whitespace that follows, including a newline character, will be left on the input stream. Then when switching to line-oriented input, the first line retrieved with getline will be just that whitespace. In the likely case that this is unwanted behaviour, possible solutions include:
An explicit extraneous initial call to getline
Removing consecutive whitespace with std::cin >> std::ws
Ignoring all leftover characters on the line of input with cin.ignore(std::numeric_limitsstd::streamsize::max(), '\n');
The other line lengths are increased perhaps by the pasting the text containing \r\n that are not processed by the MS specific text file input conversions in the middle.

What happens when I enter EOF while using cin in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I entered three inputs to the code below and got the results as follows
(1)
input: CTRL+D
result: (blank)
(2)
input: abcCTRL+D
result: (not terminated yet)
(3)
input: abc
result: abc
using namespace::std;
int main()
{
string input;
cin >> input;
cout << input << endl;
return 0;
}
I wonder why I should enter EOF twice to terminate the code in the second case
not just only one time like the first case (it terminated immediately)

Why does the compiler execute the while loop again even when the condition is not getting satisfied? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
while(ans == 'Y' || 'y')
{
cout<<"--MENU--"<<endl;
cout<<"1.Create a file"<<endl;
cout<<"2.Display that file"<<endl;
cout<<"3.Exit"<<endl;
cout<<"Enter your choice-";
cin>>ch;
switch(ch)
{
case 1:
create(); //create and display functions are written above this, which are not required
break;
case 2:
display();
break;
case 3:
exit(0);
}
cout<<"Do you want the menu again?(Y or y for Yes and anything else for a No)";
cin>>ans;
}
My expectation is:-
When the input is other than Y or y in ans in the last line, the control of the program should exit the loop and execute the next line...
However it executes while loop again. Why?
Suppose if the input is n in the last line, then the compiler should check whether ans contains the character Y or y and should exit the loop.
The condition inside the while ans=='Y'||'y' always evaluates to true.
It should actuay be: ans == 'Y' || ans == 'y'
The way it was written, it only evaluates the equality comparison for ans=='Y' and then it does a logical or with 'y' which is interpreted as true (any non 0 value in C is considered true) so the whole condition is always evaluated true, regardless of the first part. I hope this makes sense.

Wrong type of input [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have the code below, so basically my problem is: when someone input a character like 'a', it will pop a message that requires a re-input
I tried using the ASCII:
if (a >= 97 && a <= 122) but it still didn't work
double a;
cin >> a;
if (a >= 'a' && a <= 'z')
{
cout << "Wrong input, please re-input a: " << endl;
cin >> a;
}
cout << a;
I expect it to pop the message to re-input but the actual output is always 0 no matter what character I input
The state of a stream can be checked by using it directly in a condition. If all is okay it "returns" true, otherwise "false". So you can do e.g.
if (!(cin >> a))
{
// Invalid input, or other error
}
On invalid input you need to clear the state.
Note that if the input is invalid then the input will not be read, and the next time you attempt to read you will read the exact same input that failed the first time. One way to solve it is to ignore the rest of the line. Another is to read a whole line into a string that you then put into an input string stream for the parsing of the input.

Can I have a do while statement in C++ that checks for both a character and a int value before looping? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I very new at this and have an assignment in which I would like for a loop to exit if the user inputs(trans) 'e' but also end if a calculation balance(bal) is less than a constant I have set. Basically as my question states one is a character and the other an integer, will that work? I'm not trying to get people to do my homework for me, so I'm not posting all of my code or assignment, hope it makes sense.
This is the line of code I have
do {
ask user input(&trans)
e or calculation
{
while (trans != 'e'| bal < -OVR);
Just use regular unconditional loop and multiple exit conditions:
while( true ) {
char trans;
std::cin >> trans;
if( !std::cin or trans == 'e' )
break;
calculation;
if( bal > -0VR )
break;
}
So first of all you would not do unnecessary calculations, but what is more important you would make your code more readable and easier to understand - you make loop exit decision where it should be instead of pushing it into the end.