Wrong type of input [closed] - c++

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.

Related

The No-letter cycle [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 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.

What have I missed in this while-loop? [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 am trying to write a simple program that reads a name as a C-style string.
The name is then printed vertically, one character per line.
Currently when the program prompts a user to enter their name, eg. Henry James, only 'Henry' is printed vertically. It stops printing at the break between the names.
char myName[ 64 ] = "";
cout << "Enter your name: ";
cin.get( myName, 64 );
int i = 0;
while ( myName [ i ] != ' ' )
{
cout << myName[ i ] << endl;
i++;
}
getch();
return 0;
I've tried putting cin.ignore() the line before cin.get(), but this ends up breaking the program. What am I missing in the while loop?
You explicitly write that your loop should stop at ' ' space character. Everything as expected :-)
If you want to print until end of the C style string, check against the terminating char which is a zero.
while ( myName [ i ] != '\0' )

One Line of Input to Multiple Variables [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 4 years ago.
Improve this question
I'm trying to take one line of input separated by spaces and assign it to four variables.
I read that you're meant to be able to do:
#include <iostream>
#include <string>
int main()
{
int i, n1, n2;
std::string s;
std::cin >> i >> s >> n1 >> n2;
}
For input: 12345 string 4 5 the result would be i = 12345, s = string, n1 = 4, n2 = 5.
But I have to press enter for each variable. I need one line to assign to the four variables.
This code works already. Compile and run it, and then when the program waits for input just type in:
"12345 string 4 5", enter and you're done.
From this online CPP tutorial:
Extractions on cin can also be chained to request more than one datum in a single statement:
cin >> a >> b;
This is equivalent to:
1 cin >> a;
2 cin >> b;
In both cases, the user is expected to introduce two values, one for variable a, and another for variable b. Any kind of space is used to separate two consecutive input operations; this may either be a space, a tab, or a new-line character.

C++ Loops forever [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 4 years ago.
Improve this question
I wrote the following loop as a part of a program for my CS homework, however regardless of the input, the program keeps looping at this exact point. What am I doing wrong?
#include <iostream>
using namespace std;
char choice;
do
{
cout << "Type 'c' for characters or type 'n' for numbers: ";
cin >> choice;
}while (choice != 'c' || choice != 'n');
A do-while statement loops as long as the while expression is true.
Your while expression is
choice != 'c' || choice != 'n'
In common English, that expression means
choice is not 'c' OR choice is not 'n'
That statement, logically, is always true. choice is always not one of those things.
In both English and C++, you would want to use and/&& in that expression.

save variable from if statement [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 7 years ago.
Improve this question
Couldn't find an answer on google because I didn't know how to phrase is.
I have a regular function as below and would like to update the variable number in the first if statement. I've tried all sorts of combos but nothing works.
int main()
{
int apple, number;
cout << "Enter you number"<< endl;
cin >> apple;
if (apple == 1){
number = 2;
}
else {
number = 3;
cout << number << endl;
}
How would I change the above so I get 2 to output to the screen?
Thanks in advance!
You need to use
if (apple == 1)
instead of
if (apple = 1)
== is used for comparison. Also to note that your code will always assign the value 2 to the variable apple as in your condition you are not comparing rather you are assigning. So in your case the output will always be 2.