Thread: what's the best way to achieve this

  1. #61
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    Home_score is a string because on message 13 StainedBlue learned me that a string is the best way for user input.

    I try to adress your suggestions.

    You mean something like this :

    getline (cin_home_score);
    result= check_numbers() ;

    Roelof

  2. #62
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by roelof
    Home_score is a string because on message 13 StainedBlue learned me that a string is the best way for user input.
    It is true that reading input as a string tends to make it easier to process and validate the input. But this is precisely what my example in post #51 did: I read in a line as a string, and then from that line of input extracted it into an int. But what you did was to replace the int with a string... that does not make sense, unless you have some other reason for home_score to be a string.

    Quote Originally Posted by roelof
    I try to adress your suggestions.

    You mean something like this :

    getline (cin_home_score);
    result= check_numbers() ;
    Not quite. I had a function with this prototype in mind:
    Code:
    bool is_integral(const std::string& str);
    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

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

    Then I misunderstood your code.
    If I want to place home_score in a file then I can use home_score or do I need line ?

    C++ is very hard to understand.

    Roelof

  4. #64
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by roelof
    If I want to place home_score in a file then I can use home_score or do I need line ?
    If you want to write the value of home_score to a file, then you can use the overloaded operator<< just like how it is printed to standard output.
    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

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

    Now I have this :
    Code:
    #include <iostream>
    #include <cstring>
    #include <sstream>
    #include <fstream>
    
    using namespace std;
    
    int input_games()
    {
    
         string home_team , away_team, game ;
         int lengte ;
         do {
            cout<< "Home team :";
            getline(cin,home_team);
            lengte = home_team.length() ;
              if (lengte==0){
                 cout<< "input is empty. There must be a input";}}
         while (lengte==0);
         do {
            cout<< "Away team :";
            getline(cin,away_team);
            lengte = away_team.length() ;
              if (lengte==0){
                 cout<< "input is empty. There must be a input";}}
         while (lengte==0);
         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";
            }
        }
        int away_score;
        for (;;)
        {
            cout << "Enter the away score: ";
            string line;
            getline(cin, line);
            stringstream ss(line);
            if (ss >> away_score && ss.eof())
            {
                break;
            }
            else
            {
                cout << "Invalid input: enter a number only.\n";
            }
        }
        game = home_team + "," + away_team
        ofstream a_file ( "test.txt", ios::app );
        a_file << game ;
        a_file.close ;
    return 0;
     }
    
    
    
    string get_user_input()
    {
        string input;
    
        do
        {cout<<"1) Input game.: "<<endl;
         cout<<"2) Making the rank.: "<<endl;
         cout<<"3) Quit.: "<<endl;
         getline(cin, input);
         if ( input!="1" && input!="2" && input!="3" ) {
         cout<< "Input must be 1 or 2 or 3" <<endl;
          }
       }
       while ( input!="1" && input!="2" && input!="3"); // while not 1, 2, or 3
          return input;
    }
    
    int main(){
    
         string input ;
    
        do {
           input = get_user_input();
           if( input == "1" ){ input_games(); }
             else if( input == "2" ){ /* do whatever */ }}
    
       while (input!="3");
      return 0;
    }
    Two questions

    1) How can I take care that score_home and score_away will be in the "database" ?
    2) The compiler said that a_file is no declared in this scope. What type must a_file be ?
    The tutorial said nothing about that .

    Regards,

    Roelof

  6. #66
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello ,

    2 is solved by using a ; and () at the right place.

    Anyone any solution for problem 1 : I tried static_cast <string>(home_score) but then I get this message : C:\Users\wobben\Desktop\toernooi\toernooi\main.cpp |59|error: invalid conversion from 'int' to 'const char*'|

    Roelof

  7. #67
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    You can't cast an int to a string.
    It wont work, as you found out...

    What you need to do is insert "home_score" into a stringstream. Next, insert the stringstream into a string, and you'll now have the int in the string.

    Like this:
    Code:
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    int main() {
    int i = 15;
    stringstream ss; //create a stringstream object
    ss << i; //'i' (15) is now in stringstream
    string s; //create a string object
    ss >> s; //15 is now in string
    cout<< s; //output string
    cout<< "\n\nPress Enter to end the program";
    cin.get(); 
    return 0;
    }
    Last edited by Programmer_P; 06-04-2010 at 09:27 AM.

  8. #68
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or just
    Code:
    boost::lexical_cast<std::string>(myint);
    Works the other way around too
    Code:
    boost::lexical_cast<int>(mystr);
    Yay for boost.

    ...But, you don't really need that. All you need is:
    Code:
        ofstream a_file ( "test.txt", ios::app );
        a_file << home_team << "," << away_team;
    Yay for iostream.
    (You can remove the close, too. It's done automatically when the a_file object does out of scope.)
    Other than that, you really, really, REALLY need to fix your indentation.
    Last edited by Elysia; 06-04-2010 at 09:33 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #69
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello Elysia.

    Thank you, I now can make the 'database';

    What's not right about my indention.
    I try to use it as described in the tutorial ?

    Roelof

  10. #70
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First, inconsistent spacing. Use a fixed amount of indentation (spaces or tabs) for every block level.
    Second, this:
    Code:
              if (lengte==0){
                 cout<< "input is empty. There must be a input";}}
    Putting two ending braces at the last row hurts readability. Place them on separate rows.
    Third,
    Code:
        {cout<<"1) Input game.: "<<endl;
    Put the beginning brace of a separate line or as the last character on the previous line. But do NOT put it on the line where the code of the next block begins. This hurts readability.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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

    I try to solve it and put then the result here.
    If it's then allright with you and your colleges I try to write the last and difficult part of this programm.

    Roelof

  12. #72
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello Elysia,

    Is this better.
    Code:
    #include <iostream>
    #include <cstring>
    #include <sstream>
    #include <fstream>
    
    using namespace std;
    
    int input_games()
    {
    
        string home_team , away_team, game ;
        int lengte ;
        do 
        {
            cout<< "Home team :"; // inlezen van de team die thuis speelt
            getline(cin,home_team);
            lengte = home_team.length() ; // controle of de gebruiker iets ingevoerd heeft
            if (lengte==0)
            {
                cout<< "input is empty. There must be a input";
            }
        }
        while (lengte==0);
        do 
        {
            cout<< "Away team :"; // inlezen van het uitspelende team
            getline(cin,away_team);
            lengte = away_team.length() ; //controle of de gebruiker iets ingevoerd heeft 
            if (lengte==0)
            {
                cout<< "input is empty. There must be a input";
            }
        }
        while (lengte==0);
        int home_score;
        for (;;)
        {
            cout << "Enter the home score: "; // Inlezen van de soore van de thuisspelende team
            string line;
            getline(cin, line);
            stringstream ss(line);
            if (ss >> home_score && ss.eof()) // controle of er alleen cijfers zijn ingevoerd
            {
                break;
            }
                else
            {
                cout << "Invalid input: enter a number only.\n";
            }
        }
        int away_score;
        for (;;)
        {
            cout << "Enter the away score: "; // invoeren van de score van het uitspelende team
            string line;
            getline(cin, line);
            stringstream ss(line);
            if (ss >> away_score && ss.eof()) // controle of er alleen maar cijfers zijn ingevoerd
            {
                break;
            }
            else
            {
                cout << "Invalid input: enter a number only.\n";
            }
        }
        ofstream a_file ( "test.txt"); // open de tekstfile and zet de gegevens erin 
        a_file << home_team << ";" << away_team << ";" << home_score << ";" << away_score ;
        a_file.close() ;
    return 0;
     }
    
    string get_user_input() // invoeren van de keuze van het hoofdmenu 
    {
        string input;
    
        do
        {
            cout<<"1) Input game.: "<<endl;
            cout<<"2) Making the rank.: "<<endl;
            cout<<"3) Quit.: "<<endl;
            getline(cin, input);
            if ( input!="1" && input!="2" && input!="3" )
            {
                cout<< "Input must be 1 or 2 or 3" <<endl;
            }
        }
        while ( input!="1" && input!="2" && input!="3"); // while not 1, 2, or 3
        return input;
    }
    
    int main(){
    
        string input ;
    
        do 
        {
            input = get_user_input();
            if( input == "1" ){ input_games(); // als de gebruiker 1 kiest ga dan naar invoeren van de wedstrijdgevens
        }
        else 
            if( input == "2" ){ /* do whatever */ }} // als de gebruiker 2 kiest , ga dan naar het maken van de stand 
        while (input!="3"); // laat het menu zien zolang de gebruiker geen 3 kiest 
        return 0;
    }
    Roelof

  13. #73
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're definitely on the right track! It's much better, but not perfect.
    Here are some things I found:
    Code:
            if (ss >> home_score && ss.eof()) // controle of er alleen cijfers zijn ingevoerd
            {
                break;
            }
                else
            {
                cout << "Invalid input: enter a number only.\n";
            }
    (else is misplaced.)

    You missed this part:
    Code:
            if( input == "1" ){ input_games(); // als de gebruiker 1 kiest ga dan naar invoeren van de wedstrijdgevens
        }
        else 
            if( input == "2" ){ /* do whatever */ }} // als de gebruiker 2 kiest , ga dan naar het maken van de stand
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #74
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    We go for perfect.
    Code:
    #include <iostream>
    #include <cstring>
    #include <sstream>
    #include <fstream>
    
    using namespace std;
    
    int input_games()
    {
    
        string home_team , away_team, game ;
        int lengte ;
        do
        {
            cout<< "Home team :"; // inlezen van de team die thuis speelt
            getline(cin,home_team);
            lengte = home_team.length() ; // controle of de gebruiker iets ingevoerd heeft
            if (lengte==0)
            {
                cout<< "input is empty. There must be a input";
            }
        }
        while (lengte==0);
        do
        {
            cout<< "Away team :"; // inlezen van het uitspelende team
            getline(cin,away_team);
            lengte = away_team.length() ; //controle of de gebruiker iets ingevoerd heeft
            if (lengte==0)
            {
                cout<< "input is empty. There must be a input";
            }
        }
        while (lengte==0);
        int home_score;
        for (;;)
        {
            cout << "Enter the home score: "; // Inlezen van de soore van de thuisspelende team
            string line;
            getline(cin, line);
            stringstream ss(line);
            if (ss >> home_score && ss.eof()) // controle of er alleen cijfers zijn ingevoerd
            {
                break;
            }
            else
            {
                cout << "Invalid input: enter a number only.\n";
            }
        }
        int away_score;
        for (;;)
        {
            cout << "Enter the away score: "; // invoeren van de score van het uitspelende team
            string line;
            getline(cin, line);
            stringstream ss(line);
            if (ss >> away_score && ss.eof()) // controle of er alleen maar cijfers zijn ingevoerd
            {
                break;
            }
            else
            {
                cout << "Invalid input: enter a number only.\n";
            }
        }
        ofstream a_file ( "test.txt"); // open de tekstfile and zet de gegevens erin
        a_file << home_team << ";" << away_team << ";" << home_score << ";" << away_score ;
        a_file.close() ;
    return 0;
     }
    
    string get_user_input() // invoeren van de keuze van het hoofdmenu
    {
        string input;
    
        do
        {
            cout<<"1) Input game.: "<<endl;
            cout<<"2) Making the rank.: "<<endl;
            cout<<"3) Quit.: "<<endl;
            getline(cin, input);
            if ( input!="1" && input!="2" && input!="3" )
            {
                cout<< "Input must be 1 or 2 or 3" <<endl;
            }
        }
        while ( input!="1" && input!="2" && input!="3"); // while not 1, 2, or 3
        return input;
    }
    
    int main(){
    
        string input ;
    
        do
        {
            input = get_user_input();
            if( input == "1" )
            { 
                input_games(); // als de gebruiker 1 kiest ga dan naar invoeren van de wedstrijdgevens
            }
            else
            if( input == "2" )
            { 
            /* do whatever */ }
        } // als de gebruiker 2 kiest , ga dan naar het maken van de stand
        while (input!="3"); // laat het menu zien zolang de gebruiker geen 3 kiest
        return 0;
    }
    Roelof

  15. #75
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just two minor misses:
    Code:
        a_file.close() ;
    return 0;
     }
    return 0 should up a level; the brace shouldn't have a leading space. You can also remove the a_file.close(). It isn't needed.
    And the last:
    Code:
            if( input == "2" )
            { 
            /* do whatever */ }
    Last brace goes down a line.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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