How to allow a user to enter one or two inputs? - c++

Say you wanted a user to enter a base for a root, and another number for the root. But, if the user only provides 1 number, the 2nd input will default to 2, so a square root.
I want to know how to stop the input if 'enter' was pressed after entering only one number.
I am using c++.

you may try this code:
#include <iostream>
using namespace std;
int main() {
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
return 0;
}

You probably are familiar with std::cin - that does not distinguish between enter and space for number input; that is, if your write:
int a, b;
std::cin >> a;
std::cin >> b;
Your program will expect 2 numbers, accepting both
1
2
or
1 2
What you want to do sounds like you only want to accept the second format, and decide based on the user input whether it contains one or two numbers. For that, use std::getline:
std::string line;
std::getline(std::cin, line);
Then, you need to "tokenize" the line, i.e. split it into parts separated by spaces:
std::string::size_type pos = line.find(' ');
if (pos == std::string::npos)
{
// only 1 input
}
else
{
// more than 1 input, split, e.g. with "line.substr(...)"
}
Full code:
#include <iostream>
int main()
{
std::string line;
std::getline(std::cin, line);
std::string::size_type pos = line.find(' ');
if (pos == std::string::npos)
{
int firstnum = atoi(line.c_str());
std::cout << "One number: " << firstnum << "\n";
}
else
{
int firstnum = atoi(line.substr(0, pos).c_str());
int secondnum = atoi(line.substr(pos+1, line.size()-pos).c_str());
std::cout << "Two numbers: " << firstnum << ", " << secondnum << "\n";
}
return 0;
}
Test run with one number:
1
One number: 1
Test run with two numbers:
1 2
Two numbers: 1, 2
Here we currently ignore the possibility of the user entering more than two numbers. You could check that, again with line.find, this time starting from pos+1 (see second parameter). The above code also does not check whether the input string starts with a space, which would result in 0 being recognized as first input... so it might be a bit trickier than using std::cin (which automatically skips whitespace if used for numbers). Look into string tokenization for more resilient ways of splitting the input.

I got your mean, you can use ASCII to help you.
Below is the code:
#include<iostream>
using namespace std;
int main()
{
cout << "Enter two numbers:" << endl;
int num1;
int num2 = 2;
cin >> num1;
if (cin.get() == 32)
cin >> num2;
cout << num1 << "\t" << num2 << endl;
return 0;
}
In this way, if the user enter only one number with enter, the num2 will not be changed; If the user enter one number, then space, and enter another number, the num2 will be changed as the second num2.
You could use it as a reference to complete your sqrt function.

Related

Which flag in cin turns to false when you enter a wrong type input [duplicate]

I want to check if the input is valid, but when i do run this code I see that it checks only input for charcters. If i input a float number it will take it and going to use like integer without fractional part.
#inclide <iostream>
using namespace std;
...
int n;
cout << "Your input is: "<<endl;
cin >> n;
while (cin.fail()) {
cout << "Error. Number of elements must be integer. Try again: " << endl;
cin.clear();
cin.ignore(256, '\n');
cin >> n;
}
...
`
So, how to make this code see if the input is float?
You can try to convert the input string to a int using a std::istringstream. If it succeeds then check for eof() (after ignoring blank spaces) to see if the whole input was consumed while converting to int. If the whole input was consumed then it was a valid int.
Something a bit like this:
int input_int()
{
int i;
// get the input
for(std::string line; std::getline(std::cin, line);)
{
// try to convert the input to an int
// if at eof() all of the input was converted - must be an int
if(!line.empty() && (std::istringstream(line) >> i >> std::ws).eof())
break;
// try again
std::cout << "Not an integer please try again: " << std::flush;
}
return i;
}
int main()
{
std::cout << "Enter an integer: " << std::flush;
std::cout << "i: " << input_int() << '\n';
}
Building on Raindrop7's solution, here's the full code to do what you need:
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double m;
cout << "Your input is: "<<endl;
cin >> m;
while (cin.fail() || (m-floor(m)))
{
cout << "Error. Nubmer of elements has to be integer. Try again: " << endl;
cin.clear();
cin.ignore(256, '\n');
cin >> m;
}
int n = (int)m;
return 0;
}
Here's a sample output:
Your input is:
2.7
Error. Nubmer of elements has to be integer. Try again:
erer
Error. Nubmer of elements has to be integer. Try again:
2
The code below should be able to do what you are hoping to achieve:
#inclide <iostream>
using namespace std;
int n;
cout << "Your input is: "<<endl;
while (!(cin >> n) || cin.get() != '\n') {
cout << "Error. Number of elements must be integer. Try again: " << endl;
cin.clear();
cin.ignore(256, '\n');
}
The program asks the user to re-enter an integer if either of the following happens:
If the program is unable to extract an integer from the std::cin stream. (For example, when a character or string is entered by the user)
If, after an integer is extracted successfully, the next character in std::cin is not the new line '\n' character. (For example, when a number with a decimal point like 1.1 is entered, or when an integer followed by a character like 1a is entered.)

How to restart loop if an alphabet is given as input by user in C++

I have written a code about guessing a secret number, but I have a problem when an alphabetic character is given as an input instead of an integer. It halts the program. What how can I resist this problem.
srand(time(0));
int a,secret;
secret=rand() % 10 +3;
do{
cout<<"Guess the secret num between 1-10 + 3 : ";
cin>>a;
else if(a>secret)
{
cout<<"Secret num is smaller!!"<<endl;
}
else if(a<secret) {
cout<<"Secret num is greater !!"<<endl;
}
}
while(a!=secret)
cout<<" "<<endl;
cout<<""<<endl;
cout<<"Congratulations!!!! This is the secret num...."<<secret<<endl;
You don't have to, but if you still want to solve the problem, you can stream the line and get the line in number only.
Answered by Jesse Good here:
I would use std::getline and std::string to read the whole line
and then only break out of the loop when you can convert the entire
line to a double.
#include <string>
#include <sstream>
int main()
{
std::string line;
double d;
while (std::getline(std::cin, line))
{
std::stringstream ss(line);
if (ss >> d)
{
if (ss.eof())
{ // Success
break;
}
}
std::cout << "Error!" << std::endl;
}
std::cout << "Finally: " << d << std::endl;
}
In your case, because 0 is outside the allowable range this is really simple:
Initialize a to 0 and if a is 0 after extracting:
clear cin
ignore cin (Be careful to specify that you want to ignore up to the newline character: Cannot cin.ignore till EOF?)
Your final code should look something like this:
cout << "Guess the secret num between 1-10 + 3 : ";
cin >> a;
while (a != secret) {
if (a == 0) {
cin.clear();
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
cout << "Please enter a valid number between 1-10 + 3 : ";
}
else if (a < secret) {
cout << "Secret num is smaller!!\nGuess the secret num between 1-10 + 3 : ";
}
else if (a < secret) {
cout << "Secret num is greater !!\nGuess the secret num between 1-10 + 3 : ";
}
a = 0;
cin >> a;
}
Live Example

String Input error when using stringstream and if-else statement

I am trying to write a part of a large code where a string of 2 numbers is inputted by the user and each number is parsed from the string to do simple addition (just wanted to test the parsing for now).
I do the parsing using stringstream which seems to work but using this code to compute multiple cases is not working. Please see code and output below;
#include <iostream>
#include <sstream>
int main()
{
int t;
std::cout << "Enter the number of test cases: ";
std::cin >> t;
std::cout << std::endl;
if (t > 10 || t < 1)
{
std::cout << "Invalid number of test cases!" << std::endl;
return 0;
}
else
{
for (int x = 0; x < t; x++)
{
std::stringstream numbers;
int num_1;
int num_2;
std::cout << "Enter the two numbers (separated by a space): " << std::endl;
std::string input;
getline (std::cin, input);
numbers << input;
numbers >> num_1 >> num_2;
std::cout << num_1 << std::endl;
std::cout << num_2 << std::endl;
std::cout << num_1 + num_2 << std::endl;
}
}
return 0;
}
Code Output
CODE OUTPUT
Enter the two numbers (separated by a space):
0
-13120
-13120
Enter the two numbers (separated by a space):
1 3
1
3
4
Enter the two numbers (separated by a space):
4 5
4
5
9
Enter the two numbers (separated by a space):
6 7
6
7
13
Why is the first case not taking inputs?
Change code early in main to this:
std::cout << "Enter the number of test cases: ";
std::cin >> t; std::cin.ignore();
When you cin>>t, it reads in the number you give it and then stops. Your next input is a getline, which reads to the next end of line...which is the one you typed after the number of test cases. So you're using an empty line for your first input.
cin.ignore() tells it to skip that empty line and go on to whatever you type next.
First, using a value from cin without checking the stream state is almost as bad as using an uninitialized value. Take advantage of the streams explicit bool operator. Second, you are over complicating the code, why would you read in a string of numbers, only too parse them as numbers?
You can simply do this: with an arbitrary number of objects:
cin >> arg1 >> arg2 >> arg3 >> ...
I've modifed your code with these issues in mind:
#include <iostream>
#include <sstream>
int main()
{
int t;
std::cout << "Enter the number of test cases:\n";
if (std::cin >> t) { // succeeded in getting an int
if (t > 10 || t < 1) { // failed in getting a valid number of test cases
std::cout << "Invalid number of test cases!" << std::endl;
return 1; // return value of 0 is reserved for a successful run, returning zero after an error doesn't make sense
}
else { // succeeded in getting a valid number of test cases
for (int x = 0; x < t; x++) {
int num_1;
int num_2;
std::cout << "Enter the two numbers (separated by a space): " << std::endl;
// why use a stringstream here when you can get the same affect by just retrieving
// two integers from cin
if (std::cin >> num_1 >> num_2) { // again always check for valid input
std::cout << num_1 << std::endl;
std::cout << num_2 << std::endl;
std::cout << num_1 + num_2 << std::endl;
}
else { // failed in getting two int's
//...
// handle error
}
}
}
}
else {
//...
// handle error
}
return 0;
}

Code won't cin/ uninitialized local variable

So far my only problem with this code is that C won't initialize. I know that if I make degree_type == "C" it won't compile because I can't turn an int into a character. What's exactly wrong with this code?
#include <iostream>
using namespace std;
int main()
{
char C;
double degree;
int degree_type;
cout << "What's the Degree type?: ";
cin >> degree_type;
if (degree_type == C)
{
cout << "What's the Temperature:? ";
cin >> degree;
cout << "Your Degrees in Celsius is, " << 9 / 5 * degree + 32 << " degrees fahrenheit." << endl;
}
else
{
cout << "What's the Temperature:? ";
cin >> degree;
cout << "Your Degrees in Fahrenhait is, " << (degree - 32) * 5 / 9 << " degrees Celsius." << endl;
}
return 0;
}
You are (or were, before you changed your question) using cin to read a character. When you read one character, the next character (the Enter keypress) remains in the input buffer waiting to be read. The next time you read from cin (to get the temperature), it will immediately see the Enter keypress from the previous input and not let you type anything.
Use getline instead:
std::string str;
std::getline(std::cin, str);
degree_type = str.at(0);
Once you have done that, the test degree_type = C does not do what you think it does for two reasons:
The single equals = is assignment. For comparison, use ==.
The C is the name of a variable. For the character C, use 'C'.

User input(cin) - Default value

I can't figure out how to use a "default value" when asking the user for input. I want the user to be able to just press Enter and get the default value. Consider the following piece of code, can you help me?
int number;
cout << "Please give a number [default = 20]: ";
cin >> number;
if(???) {
// The user hasn't given any input, he/she has just
// pressed Enter
number = 20;
}
while(!cin) {
// Error handling goes here
// ...
}
cout << "The number is: " << number << endl;
Use std::getline to read a line of text from std::cin. If the line is empty, use your default value. Otherwise, use a std::istringstream to convert the given string to a number. If this conversion fails, the default value will be used.
Here's a sample program:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
std::cout << "Please give a number [default = 20]: ";
int number = 20;
std::string input;
std::getline( std::cin, input );
if ( !input.empty() ) {
std::istringstream stream( input );
stream >> number;
}
std::cout << number;
}
This works as an alternative to the accepted answer. I would say std::getline is a bit on the overkill side.
#include <iostream>
int main() {
int number = 0;
if (std::cin.peek() == '\n') { //check if next character is newline
number = 20; //and assign the default
} else if (!(std::cin >> number)) { //be sure to handle invalid input
std::cout << "Invalid input.\n";
//error handling
}
std::cout << "Number: " << number << '\n';
}
Here's a live sample with three different runs and inputs.
if(!cin)
cout << "No number was given.";
else
cout << "Number " << cin << " was given.";
I'd be tempted to read the line as a string using getline() and then you've (arguably) more control over the conversion process:
int number(20);
string numStr;
cout << "Please give a number [default = " << number << "]: ";
getline(cin, numStr);
number = ( numStr.empty() ) ? number : strtol( numStr.c_str(), NULL, 0);
cout << number << endl;