length of integer or numeric values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sheriff
    New Member
    • May 2007
    • 11

    length of integer or numeric values

    Hi frds,

    As i am getting an input value of integer (for ex: 1.413) and i want to limit the number of characters in the input value to 5

    for ex:

    1.413
    14.13
    141.3

    Code:
    double y;
    cout<<"Enter the new value of displacement: ";
    cin>>y;

    the ouput

    Enter the new value of displacement:_ _ _ _ _


    it shud not accept the input more than 5 characters

    if it is a string i can limit by using length () or size() function

    i cant convert them as a string and use it further

    wat can i do?
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    Originally posted by sheriff
    Hi frds,

    As i am getting an input value of integer (for ex: 1.413) and i want to limit the number of characters in the input value to 5

    for ex:

    1.413
    14.13
    141.3

    Code:
    double y;
    cout<<"Enter the new value of displacement: ";
    cin>>y;
    use cin.getline() to take the input in a array then use strtof function to convert to float.


    the ouput

    Enter the new value of displacement:_ _ _ _ _


    it shud not accept the input more than 5 characters

    if it is a string i can limit by using length () or size() function

    i cant convert them as a string and use it further

    wat can i do?
    use cin.getline() to get the input, using this you can limit the input.
    then use strtof to convert to float
    have a look at this

    Comment

    • sheriff
      New Member
      • May 2007
      • 11

      #3
      thx svlsr

      ok i will get them as a string first

      but i cant limit the character

      Code:
      string string1;
      cin.getline(string1,5);
      it saying

      no matching function for cin.getline

      i hav also tried this

      Code:
      string string1;
      int z=5;
      cin.getline(string1,z);
      i cant able to use any delimiter too as i am giving input

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by sheriff
        thx svlsr

        ok i will get them as a string first

        but i cant limit the character

        Code:
        string string1;
        cin.getline(string1,5);
        it saying

        no matching function for cin.getline

        i hav also tried this

        Code:
        string string1;
        int z=5;
        cin.getline(string1,z);
        i cant able to use any delimiter too as i am giving input
        But why using unformmated input when u can use formated input.

        e.g

        [CODE=cpp]string c;

        cin.width(6) //must also count '\0' character

        cin>>c;[/CODE]

        Savage

        Comment

        • sheriff
          New Member
          • May 2007
          • 11

          #5
          Originally posted by Savage
          But why using unformmated input when u can use formated input.

          e.g

          [CODE=cpp]string c;

          cin.width(6) //must also count '\0' character

          cin>>c;[/CODE]

          Savage
          thx savage

          but while giving input(with width fuction) it accepts more than 6 characters

          i want to limit the characters

          that i shud not able to give input more than 6 characters

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by sheriff
            thx savage

            but while giving input(with width fuction) it accepts more than 6 characters

            i want to limit the characters

            that i shud not able to give input more than 6 characters
            U can input more then six characters but only six of them are stored.

            And there is also a peek member of the cin that returns next input,so u can test it

            Savage

            Comment

            • sheriff
              New Member
              • May 2007
              • 11

              #7
              ya u r right !! savage

              it is taking only 6 characters

              thx a lot

              till my next question bye!!

              Happy weekend

              Comment

              • svlsr2000
                Recognized Expert New Member
                • Feb 2007
                • 181

                #8
                Originally posted by sheriff
                thx svlsr

                ok i will get them as a string first

                but i cant limit the character

                Code:
                string string1;
                cin.getline(string1,5);
                it saying

                no matching function for cin.getline

                i hav also tried this

                Code:
                string string1;
                int z=5;
                cin.getline(string1,z);
                i cant able to use any delimiter too as i am giving input
                getline doesnot take string as arguement
                try some like this
                char temp[6]
                cin.getline(tem p,6);
                getline expects char * as first arguement.
                If u want to use string you can use
                string s;
                cin.getline(s.c _str(),s.size() );

                Comment

                • Savage
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1759

                  #9
                  Originally posted by sheriff
                  ya u r right !! savage

                  it is taking only 6 characters

                  thx a lot

                  till my next question bye!!

                  Happy weekend
                  I'm more than happy to help u.

                  Savage

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    This is not correct:

                    [/quote=svlsr2000]
                    getline expects char * as first arguement.
                    If u want to use string you can use
                    string s;
                    cin.getline(s.c _str(),s.size() );
                    [/quote]

                    This will not compile. cin.getline() expects a char* argument. the c_str() method of string returns a representation of the string as a non-modifiable C-string, that is, a const char*.

                    You have to remember that a string does not contain a C-string.

                    Comment

                    Working...