issues with string

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 ) .


I am a beginner , so please try to be detailed .




Last edited on
HassanAlmiskeen wrote:
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 ?
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:
1
2
3
4
5
6
7
unsigned int x;
while(!(std::cin >> x))
{
    std::cout << "That wasn't a valid number" << std::endl;
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
Last edited on
L B


You are right , English is not my first language . I know my English is bad I wish you understand me well.

Your code is working very well and the problem that I had in mine is not in yours, so could you explained these things that you did use

cin.clear()

cin.ignore()

I am curious to know why They work .




The condition that I had in my code previously was this

con_num (the int after converting from string)

if ( con_num <= (unsigned int) UINT_MAX && con_num >= 0 )


so when I enter 4294967295 which is the max unsigned int , It flips to be -1 or something. (overflow)


This issue is not occurred in yours . I am surprised .

Last edited on
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.
NT3


This is awesome .

Thank you .
I still have a problem .


This while statement would not allow the user to enter what I do not want

Great

Then , If the user enter the numbers that I want , how can use these numbers for manipulations !
Write code after the while loop that uses the number. You can put it all inside a larger loop ("nesting") if you need multiple numbers ;)
Great ,

Thank you L B and NT3

The problem with the solution that you came up with is that , it can not catch 4.4 for example .

I went to my professor to discuss this today.

Now, everything is working fine.

No characters , no symbols , nothing except integer numbers with 10 or less digits

However, I need to make sure that the user would not enter something bigger than this specific number 4294967295 using string functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52


   #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 ;
        
        unsigned int 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 

            }
        
        

Last edited on
Building off of LB's solution, you could also do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <limits>

int main()
{
    std::cout << "Enter a non-negative integer not greater than "
              << std::numeric_limits<unsigned int>::max() << ": ";
    unsigned int 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.
HassanAlmiskeen wrote:
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?
I do not want to invent anything

I am just doing the requirements for my assignment .

You know , in school we do not do what we want .We just have to follow the instructions that we have.

Your code was really wonderful and great . You are a professional programmer I believe.

There is a little problem with some numbers such that 4.4 .

If the user enter this number . I would get I wrong value as result because after I filter the numbers from the user , I would do some math .

I am sorry .

Last edited on
long double main

Your code looks very good .

It catches all the kinds of errors .

Could you explain this line of the code , please ?

1
2
   while (std::cin.peek() == '-' || !(std::cin >> num) || std::cin.get() != '\n')




I appreciate your help guys .

Thank you
Last edited on
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.
Topic archived. No new replies allowed.