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
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
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.Originally Posted by roelof
Not quite. I had a function with this prototype in mind:Originally Posted by roelof
Code:bool is_integral(const std::string& str);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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.Originally Posted by roelof
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Oke,
Now I have this :
Two questionsCode:#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; }
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
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
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.
Or just
Works the other way around tooCode:boost::lexical_cast<std::string>(myint);
Yay for boost.Code:boost::lexical_cast<int>(mystr);
...But, you don't really need that. All you need is:
Yay for iostream.Code:ofstream a_file ( "test.txt", ios::app ); a_file << home_team << "," << away_team;
(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.
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
First, inconsistent spacing. Use a fixed amount of indentation (spaces or tabs) for every block level.
Second, this:Putting two ending braces at the last row hurts readability. Place them on separate rows.Code:if (lengte==0){ cout<< "input is empty. There must be a input";}}
Third,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.Code:{cout<<"1) Input game.: "<<endl;
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
Hello Elysia,
Is this better.
RoelofCode:#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; }
You're definitely on the right track! It's much better, but not perfect.
Here are some things I found:
(else is misplaced.)Code:if (ss >> home_score && ss.eof()) // controle of er alleen cijfers zijn ingevoerd { break; } else { cout << "Invalid input: enter a number only.\n"; }
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
Oke,
We go for perfect.
RoelofCode:#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; }
Just two minor misses:
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.Code:a_file.close() ; return 0; }
And the last:
Last brace goes down a line.Code:if( input == "2" ) { /* do whatever */ }