Thread: what's the best way to achieve this

  1. #46
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Yes, you can step through the string character-by-character and compare their numerical values against a range of numbers. Look for an ASCII chart to know what the numerical equivalent of the number characters are and compare each character to those numbers.

  2. #47
    Registered User
    Join Date
    May 2010
    Posts
    230
    oke,

    And with what command can I take the characters one by one.

    Roelof

  3. #48
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    If you are using the string class, you are able to access each character in the string with the [] operator, just like an array.

  4. #49
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by roelof View Post
    oke,

    And with what command can I take the characters one by one.

    Roelof
    Either use the [] operator like the poster above me suggested, or you can use the string::at() function, which provides bounds checking while the [] operator does not.

  5. #50
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I tried your suggestions with this code
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string home_score;
    
    
    int main()
    {
    
     do
     {
        cout<<"Enter home score :";
        cin.getline (cin, home_score);
        for (unsigned Nindex; home_score.length(), Nindex++) {
          if (!isdigit(home_score.at[Nindex]))
            cout << "Only numbers";
        };
            }
        while (home_score.fail)
        return 0;
    }
    But it fails to compile.

    Anyone who can learn me what went wrong here ?

    Roelof

  6. #51
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sigh, I think I should give you a working example:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
        using namespace std;
    
        int home_score;
        for (;;)
        {
            cout << "Enter the home score: ";
            string line;
            getline(cin, line);
            stringstream ss(line);
            if (ss >> home_score && ss.eof())
            {
                break;
            }
            else
            {
                cout << "Invalid input: enter a number only.\n";
            }
        }
    
        cout << "You entered: " << home_score << endl;
    
        return 0;
    }
    The idea is to read a line as string input from the user. You initialise a stringstream with this line, and then extract from the stream into your desired variable. If the extraction fails, then the input is invalid. Otherwise, you check with the eof() member function: it should return true if there is nothing left to read from the stringstream, otherwise you conclude that the input is invalid due to extra stuff at the end. If the input is valid, you break from the infinite loop, otherwise you print an error message and continue.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #52
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I see.
    After the break is there no stack left from the for look ?

    Roelof

  8. #53
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by roelof
    After the break is there no stack left from the for look ?
    What do you mean?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #54
    Registered User
    Join Date
    May 2010
    Posts
    230
    hello,

    What I mean is this.
    I thought that for the for loop there is memory used.
    Normal is that the loop ended at the end-argument.
    But we use a break now.

    Will the memory uses by the for loop be given back or are there pieces of the for loop left behind in the memory ?

    Roelof

  10. #55
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by roelof
    I thought that for the for loop there is memory used.
    Normal is that the loop ended at the end-argument.
    But we use a break now.

    Will the memory uses by the for loop be given back or are there pieces of the for loop left behind in the memory ?
    You do not have to worry about manual memory management here, and also the variables local to the for loop will be destroyed when control leaves the loop, as is the case for a break.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #56
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    Learned another thing about C++

    Roelof

  12. #57
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello laserlight.

    One problem with your example.
    Home_score is a string not a int.

    So in your script if a put aa as input it get past the test .

    Roelof

  13. #58
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by roelof
    Home_score is a string not a int.
    Why is home_score a string?

    If you really insist on home_score being a string, then yes, you should use what syzygy suggested. But what you should do is write a function that takes a string and returns true if the string is composed of numeric characters only.

    EDIT:
    Well, of course, you could rename line to home_score, and home_score to temp, though you would then need to pull out the variable to outside of the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #59
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    I tried but it won't compile
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        using namespace std;
    
        string home_score;
        int teller ;
        bool test, result;
        do
        {
            test=true;
            result=true;
            cout << "Enter the home score: ";
            getline(cin, home_score);
            for (teller, home_score.length, teller++) {
                if (home_score.at[teller] > "1" && home_score.at[teller] < "9")
                {
                  test=true ;
                else
                   test=false;
                }
                if test=false {
                   result=false ;
                    cout "You entered a wrong value ";
                } }
            }
        while (result=false) ;
        cout << "You entered: " << home_score << endl;
        return 0;
    }
    Roelof

  15. #60
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by roelof
    I tried but it won't compile
    Read the error messages.
    • Your for loop syntax is incorrect.
    • home_score.at[teller] is wrong. It should be home_score.at(teller) or home_score[teller]
    • Comparing with "1" is incorrect. You could compare with '1', but then what about 0s?
    • The brace placement of one of your if statements is wrong.
    • The condition of one of your if statements lack parentheses.

    Other problems:
    • a = b is not the same as a == b. This problem occurs in several places.
    • You made some effort to indent your code properly, and that is good, but you still can be a little more consistent. Avoid placing consecutive (closing) braces on the same line.
    • You can simplify your logic: notice that you do not need to assign true to test in the loop body. In fact, this is why I suggested that you write a function.

    As a matter of style, teller is not a conventional name for a variable used as an array/array-like container index. Furthermore, along with test and result, it should be declared in the do while loop, following the rule of thumb to declare variables near first use. In fact, it should be declared in the for loop's initial statement.

    Once again, why is home_score a string?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-31-2008, 01:26 PM
  2. Best way to achieve this
    By cloudy in forum Networking/Device Communication
    Replies: 1
    Last Post: 09-06-2006, 07:23 PM
  3. Replies: 3
    Last Post: 09-11-2005, 10:13 AM
  4. The more intelligent risks you take in life, the more you'll achieve
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-20-2003, 05:23 PM
  5. Regarding Careers - need advice
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-05-2002, 04:59 AM