Thread: Smarterchild

  1. #31
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    okay, I reformatted your code and got rid of the syntax errors for you. and if you ever mix spaces and tabs again I'll have to hire an assassin.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string name;
    	string question;
    	string subject;
    	string file;
    	string response;
    	bool keepgoing=false;
    	
    	do
    	{
    		cout<<'\n'<<name<<" says: ";
    		getline (cin, question);
    		if ( question == "do you know?" )
    		{
    			cout<<"What do you want to know?\n\n"<<name<<" says: ";
    			getline (cin, subject);
    			file = subject + ".txt";
    			ifstream c_file(file.c_str());
    			if (!c_file.is_open())
    			{
    				cout<<endl<<"Sorry I do not know what "
    					<<subject<<" is.\n\n"
    					<<"Please enter a definition of "
    					<<subject<<".\n\n"
    					<<name<<" says: ";
    				getline (cin, response);
    				ofstream d_file ( file.c_str() );
    				d_file<<response<<endl;
    				d_file.close();
    				cout<<"\nThank you! I now know what "<<
    			       		subject<<" is.\n\nHow about another "
    					<<"question!"<<endl;
    				keepgoing = 1;
    	               }
    			else
    			{
    				c_file>> response;
    				cout<<response
    					<<"\n\nThere you go! Thats all I know "
    					<<"about "<<subject<<"!\n\nSo let's "
    					<<"get on to the next question..."
    					<<endl;
    				keepgoing = true;
    			}
    		}
    		else if ( question == "who are you?" )
    		{
    	        	cout<<"I am Smarterthanchild, the world's only "
    				<<"completely\nuseless talking robot and "
    				<<"hopefully there are many\nin development\n\n"
    				<<"So that's my life story, ask a question now."
    				<<endl;
    		        keepgoing = true;
    	        }
            	else
    		{
    			cout<<"My grasp of English is so bad that I can't\n"
    				<<"understand that!\n\nAsk me another question "
    				<<"and we'll see if I can understand!"<<endl;
    		        keepgoing = true;
    	        }
    	} while ( keepgoing );
    
    	return 0;
    }
    edit: some of the changes you should notice right away (I don't even remember all the changes I made)
    • better formatting (all tabs)
    • slightly more efficient code (no need to call endl for every newline)
    • changed keepgoing to a boolean variable


    edit2: it's dinner time, so I can't work on it right now, but the first thing that catches my attention is that you're opening the file for writing without any mode flags. Probably not your main problem, but definately not a good idea.
    Last edited by major_small; 03-18-2006 at 06:00 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  2. #32
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Are the flag things that you mean things like ios::app?

  3. #33
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Any help?

  4. #34
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're not supposed to bump threads.
    Are the flag things that you mean things like ios::app?
    Yes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #35
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I was thinking about how to solve my problems at school today...is it possible to read into a character array to find out how long it is? Because I thought of character array [x]...get the input....find the length save x and save the array?

    Would that wor and how can I implement that in what I have

  6. #36
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Daved asked for example code...I have none...but what I want is for the user to input a phrase...it is then saved as a text file and can then be recalled when the subject is requested...I have it workign using strings apart from it will only read the first word of teh saved string

  7. #37
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    just read in until you get a newline character

    Code:
    file.getline(array,maxsizeofarray,'\n');

  8. #38
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by bumfluff
    Daved asked for example code...I have none...
    Makes it difficult to help you without code.

    Quote Originally Posted by bumfluff
    I have it workign using strings apart from it will only read the first word of teh saved string
    Makes it difficult to be working without code. How'd you accomplish this feat?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #39
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Here is the part with problems:

    Code:
    do {
          cout<<endl;
          cout<<name<<" says: ";
          getline (cin, question);
          cout<<endl;
    
             if ( question == "do you know?" ) {
             cout<<"What do you want to know?"<<endl;
             cout<<endl;
             cout<<name<<" says: ";
             getline (cin, subject);
             endl;
             file = subject + ".txt";
             ifstream c_file ( file.c_str() );
                   if (!c_file.is_open()) {
                   cout<<endl;
                   cout<<"Sorry I do not know what "<<subject<<" is."<<endl;
                   cout<<endl;
                   cout<<"Please enter a definition of "<<subject<<"."<<endl;
                   cout<<endl;
                   cout<<name<<" says: ";
                   getline (cin, response);
                   endl;
                   ofstream d_file ( file.c_str() );
                   d_file<<response<<endl;
                   d_file.close();
                   cout<<endl;
                   cout<<"Thank you! I now know what "<<subject<<" is."<<endl;
                   cout<<endl;
                   cout<<"How about another question!"<<endl;
                   keepgoing = 1;
                   }
                   else {
                   cout<<endl;
                   c_file>> response;
                   cout<<response<<endl;
                   cout<<endl;
                   cout<<"There you go! Thats all I know about "<<subject<<"!"<<endl;
                   cout<<endl;
                   cout<<"So let's get on to the next question..."<<endl;
                   keepgoing = 1;
                   }
                   }
             else if ( question == "who are you?" ) {
             cout<<"I am Smarterthanchild, the world's only completely"<<endl;
             cout<<"useless talking robot and hopefully there are many"<<endl;
             cout<<"in development"<<endl;
             cout<<endl;
             cout<<"So that's my life story, ask a question now."<<endl;
             keepgoing = 1;
             }
             else {
             cout<<"My grasp of English is so bad that I can't understand that!"<<endl;
             cout<<endl;
             cout<<"Ask me another question and we'll see if I can understand!"<<endl;
             keepgoing = 1;
             }
    } while ( keepgoing == 1 );

  10. #40
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I ave a problem where it will only read the first word from the file.
    Code:
    #include <iostream> 
    #include <fstream> //ifstream, ofstream
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
    	//write to file:
    	string data = "a loose fitting t-shirt";
    	ofstream outFile("C:\\TestData\\data.txt");
    	outFile<<data;
    
    	outFile.close();
    
    	//read from file:
    	ifstream inFile("C:\\TestData\\data.txt");
    	if(!inFile)
    	{
    		cout<<"couldn't open file"<<endl;
    		return 1;
    	}
    	
    	string input;
    	getline(inFile, input);
    
    	cout<<input<<endl;
    
    	return 0;
    }

  11. #41
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    So rather than using (example) x_file>>string I would use getline.

  12. #42
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The >> operator is programmed to skip leading whitespace(spaces, tabs, newlines) and stop reading when it encounters whitespace.

    getline() stops reading when it encounters a '\n' character. If you manually enter data into a file, when you hit return either an invisible \r\n(windows), \r(mac), or \n(unix) is entered at the end of the line. When you read from a file, C++ automatically converts the various line termination characters to a '\n', so getline() will stop when it reads the '\n'.

    On the other hand, if you programatically enter data into a file, you have to enter a '\n' with your program when you want the line to terminate, e.g.:

    outFile<<myString<<'\n';
    Last edited by 7stud; 03-21-2006 at 03:23 PM.

  13. #43
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Quote Originally Posted by bumfluff
    Here is the part with problems:

    Code:
    do {
          cout<<endl;
          cout<<name<<" says: ";
          getline (cin, question);
          cout<<endl;
    
             if ( question == "do you know?" ) {
             cout<<"What do you want to know?"<<endl;
             cout<<endl;
             cout<<name<<" says: ";
             getline (cin, subject);
             endl;
             file = subject + ".txt";
             ifstream c_file ( file.c_str() );
                   if (!c_file.is_open()) {
                   cout<<endl;
                   cout<<"Sorry I do not know what "<<subject<<" is."<<endl;
                   cout<<endl;
                   cout<<"Please enter a definition of "<<subject<<"."<<endl;
                   cout<<endl;
                   cout<<name<<" says: ";
                   getline (cin, response);
                   endl;
                   ofstream d_file ( file.c_str() );
                   d_file<<response<<endl;
                   d_file.close();
                   cout<<endl;
                   cout<<"Thank you! I now know what "<<subject<<" is."<<endl;
                   cout<<endl;
                   cout<<"How about another question!"<<endl;
                   keepgoing = 1;
                   }
                   else {
                   cout<<endl;
                   c_file>> response;
                   cout<<response<<endl;
                   cout<<endl;
                   cout<<"There you go! Thats all I know about "<<subject<<"!"<<endl;
                   cout<<endl;
                   cout<<"So let's get on to the next question..."<<endl;
                   keepgoing = 1;
                   }
                   }
             else if ( question == "who are you?" ) {
             cout<<"I am Smarterthanchild, the world's only completely"<<endl;
             cout<<"useless talking robot and hopefully there are many"<<endl;
             cout<<"in development"<<endl;
             cout<<endl;
             cout<<"So that's my life story, ask a question now."<<endl;
             keepgoing = 1;
             }
             else {
             cout<<"My grasp of English is so bad that I can't understand that!"<<endl;
             cout<<endl;
             cout<<"Ask me another question and we'll see if I can understand!"<<endl;
             keepgoing = 1;
             }
    } while ( keepgoing == 1 );
    This formatting..is horrible.. put a { on its own line every time and a tab on the next, put the } on the last line by itself without the tab

  14. #44
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I am sorry....I'm not exactly an expert yet anyway.

  15. #45
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I am sorry....I'm not exactly an expert yet anyway.
    What are you using to write your code? Most compilers have editors that automatically indent the next line after you type a '{'. So, you just type a '{' character and hit return, and your code is indented. Then when you type the closing '}', and hit return, the editor automatically slides it over to the left to match the first brace. So you end up with something like:
    Code:
    if(!inFile)
    {
    	cout<<"couldn't open file"<<endl;
    	return 1;
    }
    Last edited by 7stud; 03-21-2006 at 03:30 PM.

Popular pages Recent additions subscribe to a feed