Thread: what's the best way to achieve this

  1. #31
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    I'm not at a computer right now but i see a couple of things.

    1. I personally would break each stage of the input up to make it easier, and consequently, easier to track down your bugs.

    2. Cin.get() is unecessary if the user input is wrong, and is likely having a negative affect on the conditions of your while loop
    goto( comeFrom() );

  2. #32
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Strlen is for c style char arrays, you want .length()
    goto( comeFrom() );

  3. #33
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    the problem still exist.
    When I do this
    Code:
      if (length(home_team)==0 && length(away_team)==0 && length(score_away_team)!=0 && length(score_home_team)==0) {
    I get this a message :
    C:\Users\wobben\Desktop\toernooi\toernooi\main.cpp |20|error: 'length' was not declared in this scope|

    And when I do this :
    Code:
    if (home_team.length()==0
    The same error appears.

    Roelof

  4. #34
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you included
    Code:
     #include <string>

  5. #35
    Registered User
    Join Date
    May 2010
    Posts
    230
    Yep,

    I included string and cstring.

    Roelof

  6. #36
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Ok then
    Code:
     home_team.length()
    should be correct.

    But if it's not working repost the code.

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

    I see the problem now.
    If I do if (home_team.length=0) It's oke
    But when I do if(home_team.length=0 && away_team.length=0) I get this message:
    C:\Users\wobben\Desktop\toernooi\toernooi\main.cpp |20|error: invalid use of member (did you forget the '&' ?)|

    So i think and rewriet it so every input get seperate check.

    Roelof

  8. #38
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you are using if(home_team.length()=0) it should be if(home_team.length()==0)

    Jim

  9. #39
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    Does anyone know how I can check if a input only contains numbers ?

    Roelof

    Edit 1 :

    The only thing I can think is to make a loop and check everything in the string seperate.
    with converting yoiu have the problem that if the string contains 0 converting give the same answer if string contains a.
    Last edited by roelof; 06-02-2010 at 08:53 AM.

  10. #40
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Why bother converting from a string to an int? Strings are obviously more flexible than ints for user input. With strings a user can enter "1", "one", "One", or "ONE" which all mean the same thing. If the string matches any of those, then assign the value 1 to an integer.
    goto( comeFrom() );

  11. #41
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    There is no problem.
    The problem is the input of score_home_team and score_away_team.
    That must be numbers because I use them for calculating of the total points a team made and the total points other teams made against this team.
    Also I have to decide which team has won the game.

    Roelof

  12. #42
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Code:
    ^[\d]+$
    goto( comeFrom() );

  13. #43
    Registered User
    Join Date
    May 2010
    Posts
    230
    Sorry,

    Im a noob in C++.
    What do you mean by ^[/d]+$

    Roelof

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

    I have googled but this seems to be a print solution.
    I was looking for input validation that the string only contains numbers.

    Roelof

    Edit :

    can this be a good solution:
    Code:
    int myNumber = 0;
    
     while (true) {
       cout << "Please enter a valid number: ";
       getline(cin, input);
    
       // This code converts from string to number safely.
       stringstream myStream(input);
       if (myStream >> myNumber)
         break;
       cout << "Invalid number, please try again" << endl;
     }
     cout << "You entered: " << myNumber << endl << endl;
    can someone tell me what mystream >> mynumber means ?
    Last edited by roelof; 06-02-2010 at 11:21 AM.

  15. #45
    Registered User
    Join Date
    May 2010
    Posts
    230
    Nobody.

    Is there a way i can use a loop and take all the characters one for one for a check if it's a number ?

    Roelof

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