I have a problem understanding the string and its functions
for example , str.size() what does it mean ?
when I say ( str.size()<'9')
does this mean that this number 11111456 is included in the condition because it contains 8 rooms ?
The reason why I am using string is that I want to limit the user and make sure that the user would not be able to enter something except the numbers from 0 to the max unsigned int .
When I made this as a condition in if statement , it did not work because for some big numbers , I got an overflow and then I got wrong values as a result .
The question now is, which functions for string, I could use to creat my condition
(the numbers from 0 to the max unsigned int ) .
does this mean that this number 11111456 is included in the condition because it contains 8 rooms ?
I assume English is not your first language? Also, '9' is completely unrelated to 9
HassanAlmiskeen wrote:
The reason why I am using string is that I want to limit the user and make sure that the user would not be able to enter something except the numbers from 0 to the max unsigned int .
You have made the wrong choice. Just use a regular integer:
The cin.clear() statement clears the fail flag. Basically, if the stream cannot fill the instance, the fail bit is set, preventing access to the stream. This function clears the stream to make it possible to read data from it again. The cin.ignore(numeric_limits<streamsize>::max(), '\n') statement is used to remove all characters left in the buffer, so as to not get into a loop of trying to read the next invalid character if a longer string was passed. It finishes ignoring input once a newline ('\n') is entered, allowing for more input.
#include <iostream>
#include <cmath>
#include <climits>
#include <string>
#include <stdio.h>
#include <cstdlib>
using std::endl ;
using std::cout ;
using std::cin ;
std::string num ;
unsignedint con_num;
int i ;
cout << "enter a decimal numer ";
cin >> num;
for(i=0; i < num.size(); i++)
if ( !(num.at(i) >='0' && num.at(i)<= '9') || num.size() > 10)
{
cout << "enter number bigger than zero and smaller than or equal to " << UINT_MAX << endl;
break;
if(i==num.size())
{
con_num = std::atoi( num.c_str());
cout << con_num
}
#include <iostream>
#include <limits>
int main()
{
std::cout << "Enter a non-negative integer not greater than "
<< std::numeric_limits<unsignedint>::max() << ": ";
unsignedint num;
// Try to get input from the user.
// If the first character is '-', then the number is negative, so that input
// should be rejected.
// If the input fails, or if it succeeds, but there's ANYTHING other than
// a newline character left in the input buffer afterwards, then the user
// must have entered a non-digit character, so we should reject that input.
// Note that entering a number bigger than std::numeric_limits<unsigned int>::max()
// will result in 'std::cin >> num' failing.
while (std::cin.peek() == '-' || !(std::cin >> num) || std::cin.get() != '\n')
{
std::cout << "Invalid input, try again: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cout << "You entered: " << num;
}
Sample output:
Enter a non-negative integer not greater than 4294967295: Hello world
Invalid input, try again: 45.89
Invalid input, try again: -16
Invalid input, try again: 900000000000001
Invalid input, try again: 4294967296
Invalid input, try again: 42
You entered: 42
Note that this program will also accept an input like "+100" (positive 100).
If you don't want that, you can change the std::cin.peek() == '-' part to !std::isdigit(std::cin.peek()) or something like that.
However, I need to make sure that the user would not enter something bigger than this specific number 4294967295 using string functions
Why? Why do you have to use string functions for this? The code I provided will give an error message if a number that is too large is entered, why do you need to reinvent the wheel?
This is similar to @LB's code posted earlier. The cin.peek() checks to see if the first digit is a minus symbol, which would make the number negative (illegal). It doesn't actually 'eat' up the digit, though, so if it isn't a negative symbol you don't lose any data. The next is a test for the fail bit of the input stream, exactly like the while(!cin >> num) loop used earlier. Finally, the cin.get() checks for the next character. If it is a new line, it means that no data was entered, which is invalid input. These are alled 'or'ed together, so that if any fails the whole test fails.