Reputation: 65
For example:
int input;
cout << "Please enter how many burgers you'd like" << endl;
cin >> input;
What would be the easiest way to shorten 'input' and only accept the first two bits digits.
To continue the example:
User enters: 432534. Value of input: 43.
User enters: 12342342341234123412341235450596459863045896045. Value of input: 12.
(Edited: to say 'digits' instead of 'bits')
Upvotes: 2
Views: 1554
Reputation: 979
I think this method will be easy to understand.
int input;
cout << "Please enter how many burgers you'd like" << endl;
cin >> input;
char cTemp[50];
itoa(input, cTemp, 10);
char cResult[3] = {cTemp[0], cTemp[1], '\0'};
int output = atoi(cResult);
Upvotes: 0
Reputation: 47784
Take input
as string and convert the 1st two character as int
#include <sstream>
#include <string>
#include<iostream>
int main()
{
std::string input ;
std::cin>>input;
std::stringstream iss(input.substr(0,2));
int num;
iss >> num;
std::cout<<num;
}
Upvotes: 0
Reputation: 3691
I'm surprised nobody has mentioned fscanf
. While C++ purists may object, this requires substantially less code (and substantially better error checking) than cin
in this case.
int res = 0;
std::cout << "Please enter how many burgers you'd like" << std::endl;
if (fscanf(stdin, "%02d", &res) != 1) {
std::cerr << "Invalid Input" << std::endl;
}
int c;
do {
c = fgetc(stdin);
} while (c != '\n' && c != EOF);
std::cout << "Got " << res << std::endl;
Upvotes: 1
Reputation: 61910
Read the first two digits and form an integer:
int digit1 = std::cin.get() - '0';
int digit2 = std::cin.get() - '0';
int input = digit1 * 10 + digit2;
Then discard the rest of the input:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
To handle a negative sign, you could do something like this:
int input = 1;
int digit1 = std::cin.get();
if (digit1 == '-') {
input = -1;
digit1 = std::cin.get();
}
digit1 -= '0';
int digit2 = std::cin.get() - '0';
input *= (digit1 * 10) + digit2;
As commented below, this does not work if the user enters anything except two numbers as the first two characters. This is easy enough to check by reading and using std::isdigit
to test. It's up to you to keep going or throw an error of some sort.
This also doesn't work if the user enters one digit only. If you need that to work as well, you can either read a whole string and use its size or check for EOF.
There is also no error checking on the input operations themselves, but there should be in real code.
Upvotes: 3
Reputation: 949
Read a string and extract the first 2 chars:
std::string input;
cout << "Please enter how many burgers you'd like" << endl;
cin >> input;
int first2;
if (input.size()>=1)
{
if (input[0] == '-')
std::cerr << "Negative num of burgers";
else
first2 = atoi(input.substr(0,2).c_str());
}
else
std::cout << "Null number";
Upvotes: 0
Reputation: 51355
Let me answer the question by rewriting it for you:
How do I read a string from the standard input and write the first 2 characters to the standard output?
Upvotes: -3
Reputation: 27133
int i;
cin >> i;
while (i >= 100 || i <= -100) {
i = i / 10; // remove and ignore the last digit
}
This won't work for really large numbers, due to overflow in the integer. I'm just including this as a very simple algorithm.
Upvotes: 0
Reputation: 22529
I think std::string
operations can get you home.
std::string inputstr;
cout << "Please enter how many burgers you'd like" << endl;
cin >> inputstr;
inputstr = inputstr.substr(0, 2);
int input
input = std::stoi(inputstr); // C++11
input = atoi(inputstr.cstr()); // pre-C++11
Documentation:
http://en.cppreference.com/w/cpp/string/basic_string/stol
http://en.cppreference.com/w/cpp/string/byte/atoi
Upvotes: 5