Like Tree4Likes

AI

This is a discussion on AI within the C++ Programming forums, part of the General Programming Boards category; I need help with appending and retrieving information from the un() part of this program. I am trying to think ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    8

    AI

    I need help with appending and retrieving information from the un() part of this program. I am trying to think of different ways to get information to and from a text file. If you could help I would be greatly appreciative. Thanks, kiknwing.

    Code:
    #include "stdafx.h"
    
    class Index
    {
    	ofstream myFile;
    	HANDLE np;
    	unsigned threadID;
    
    
    	char selection;
    	string user;
    
    
    public : 
    	
    	Index(void);
    	~Index(void);
    	int m(void);		//menu
    	void r(void);		//register
    	void l(void);		//login			
    	void un (void);		//username
    	void pw (void);		//password
    	void notepad(void);
    	
    	static unsigned int __stdcall notepadThread(void* arg);
    	void AI (void);
    
    
    	void c(void);	//clearscreen
    	void p(void);	//pause
    
    
    } index;
    
    
    
    
    int Index::m(void)
    {
    	c();
    	
    	cout<<"Register = 1\nLogin = 2"<<endl;
    	cin>>selection;
    	
    	switch (selection)
    	
    	{
    	
    	case '1' : {	AI();
    	break;
    			   }
    	
    	case '2' : {	AI();
    	break;
    			   }
    
    
    	default : { cout<<"nope";
    	break;
    			  }
    	}
    	 
    	return 1;
    }
    
    
    void Index::AI(void)
    {
    	if (selection == 1 || 2)
    	{ 
    		c();
    		cout<<"Please Enter Your Username."<<endl<<"Username: ";
    		cin>>user;
    		un();
    	}
    }
    
    
    Index::~Index (void)
    {
    
    
    }
    
    
    void Index::un(void)
    {
    	
    }
    
    
    void Index::pw(void)
    {
    
    
    }
    
    
    Index::Index (void) {
    np = 0;
    }
    
    
    unsigned int __stdcall Index::notepadThread(void* arg) {
    system("notepad.exe record.txt");
    _endthreadex(0);
    arg = 0;
    return 0;
    }
    
    
    void Index::notepad(void) {
    np = (HANDLE)_beginthreadex( NULL, 0, &Index::notepadThread, np, 0, &threadID );
    }
    
    
    void Index::c(void)
    {
    	system("CLS");
    }
    
    
    void Index::p(void)
    {
    	system("pause");
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    Index index;
    while(index.m());
    return 0;
    }

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    What do you need help with?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    8

    update and seekg()

    Quote Originally Posted by CodeMonkey View Post
    What do you need help with?
    Since my last post I have updated the AI program. I need help specifically with seekg(). I need seekg() to retrieve data from the txt file back into the program. This stuff I posted before is fixed.

    The program:
    Code:
    #include "stdafx.h"
    
    class Index
    {
    	fstream myFile;
    	HANDLE np;
    	unsigned threadID;
    
    
    	char selection;
    	string user;
    
    
    public : 
    	
    	Index(void);
    	~Index(void);
    	int m(void);		//menu
    	void r(void);		//register
    	void l(void);		//login			
    	void un (void);		//username
    	void pw (void);		//password
    	void notepad(void);
    	
    	static unsigned int __stdcall notepadThread(void* arg);
    	void AI (void);
    
    
    	void c(void);	//clearscreen
    	void p(void);	//pause
    
    
    } index;
    
    
    
    
    int Index::m(void)
    {
    	c();
    	
    	cout<<"Register = 1\nLogin = 2"<<endl;
    	cin>>selection;
    	
    	switch (selection)
    	
    	{
    	
    	case '1' : {	AI();
    	break;
    			   }
    	
    	case '2' : {	AI();
    	break;
    			   }
    
    
    	default :  { cout<<"nope";
    	break;
    			   }
    	}
    	 
    	return 1;
    }
    
    
    void Index::AI(void)
    {
    	if (selection == 1 || 2)
    	{ 
    		c();
    		cout<<"Please Enter Your Username."<<endl<<"Username: ";
    		cin>>user;
    		un();
    	}
    }
    
    
    Index::~Index (void)
    {
    
    
    }
    
    
    void Index::un(void)
    {
    	
    	while (myFile>>user);
    	{
    		myFile.open("record.txt", ios_base::app);
    		myFile<<"\n----------\n";
    		myFile<<&user;
    		myFile.close();
    		ifstream openfile("random.txt", ios::in);
    		if (openfile.is_open())
    		{
    			
    			openfile.seekg(0);
    			openfile>>user;
    			cout<<user<<endl;
    		}
    		p();
    		if (! myFile)
            {
                    cout<<"Error";
            }
    		pw();
    	}
    }
    
    
    void Index::pw(void)
    {
    	notepad();
    }
    
    
    Index::Index (void) {
    np = 0;
    }
    
    
    unsigned int __stdcall Index::notepadThread(void* arg) {
    system("notepad.exe record.txt");
    _endthreadex(0);
    arg = 0;
    return 0;
    }
    
    
    void Index::notepad(void) {
    np = (HANDLE)_beginthreadex( NULL, 0, &Index::notepadThread, np, 0, &threadID );
    }
    
    
    void Index::c(void)
    {
    	system("CLS");
    }
    
    
    void Index::p(void)
    {
    	system("pause");
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    Index index;
    while(index.m());
    return 0;
    }
    Here is what I currently need help with. The program isn't getting the addresses of the allocated strings I need.
    Code:
    void Index::un(void){
    	
    	while (myFile>>user);
    	{
    		myFile.open("record.txt", ios_base::app);
    		myFile<<"\n----------\n";
    		myFile<<&user;
    		myFile.close();
    		ifstream openfile("random.txt", ios::in);
    		if (openfile.is_open())
    		{
    			
    			openfile.seekg(0);
    			openfile>>user;
    			cout<<user<<endl;
    		}
    		p();
    		if (! myFile)
            {
                    cout<<"Error";
            }
    		pw();
    	}
    }

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    your function names are very terse, fair enough you comment them but c() and p() etc are just ridicolous, spend a few more keystrokes on them.

    Why are you particularly interested in calling notepad? what is the point of that?

    Why do you open a file for input and then want to seek to the start of the file?

    Why do you check for an error with myFile after closing it and then doing other things - surely if it is at fault you want to know about that at once? - and if you are going to output an error let it be meaningful! We have a page in work that gets added to with the most useless error messages encountered - !500 for example, infuriating but you have to laugh
    Last edited by rogster001; 04-19-2013 at 01:03 PM.
    Salem likes this.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    8
    Quote Originally Posted by rogster001 View Post
    your function names are very terse, fair enough you comment them but c() and p() are just ridicolous, spend a few more keystrokes on them.

    Why are you particularly interested in calling notepad? what is the point of that?

    Why do you open a file for input and then want to seek to the start of the file?

    Why do you check for an error with myFile after closing it and then doing other things - surely if it is at fault you want to know about that at once?
    p() is for system pause I do not know how to run my program without it so..yeah.And as you can see I do not know of any other way to get information from a txt file back to the program. I also do not know how to use any other file to post my data into. I really would like to know how to get inputs posted back to the program. So I need think seekg() might help. Perhaps you could point me in the right direction as well.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    p() is for system pause
    We know that because you commented it in your code and can read the function definition - It is a very bad way of naming functions though - if you had called your function PauseProgram() then nobody would need to see the comment or even the definition - good code should speak for itself. You need to read more quality code snippets.

    file io in C++ is relatively easy to learn about and get a handle on - pun intended.

    You just need some simple practice stuff - look at the 'how do i' section and also c++ reference
    Last edited by rogster001; 04-19-2013 at 01:23 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    8
    Quote Originally Posted by rogster001 View Post
    We know that because you commented it in your code and can read the function definition - It is a very bad way of naming functions though - if you had called your function PauseProgram() then nobody would need to see the comment or even the definition - good code should speak for itself. You need to read more quality code snippets.

    file io in C++ is relatively easy to learn about and get a handle on - pun intended.

    You just need some simple practice stuff - look at the 'how do i' section and also c++ reference
    I've looked at that before, but I still don't see how it could make the program better? I need to get entire strings from the file I am writing to, so how else do I use that to get the data from the file? Also how could different file types help and how do I use them? Thanks, kiknwing.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    Here is what I currently need help with. The program isn't getting the addresses of the allocated strings I need.
    You would be better off saving the strings to a file and then reading them back in.

    Saving the address of a variable is almost certainly meaningless because:
    * You will lose read/write access to that particular location
    * You would be counting on that location containing your data for no real reason

    Stuff that your current program depends on, like persistent memory, is not how RAM really works anymore.

    I've looked at that before, but I still don't see how it could make the program better?
    Since you have trouble with files, I would think it is obvious how an article on the subject helps you make the program better.

    We won't point you to a bunch of locations on the web where you can just steal exactly what you are looking for, if that is what you want. You were supposed to learn from what you were linked.

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    8
    Quote Originally Posted by whiteflags View Post
    You would be better off saving the strings to a file and then reading them back in.

    Saving the address of a variable is almost certainly meaningless because:
    * You will lose read/write access to that particular location
    * You would be counting on that location containing your data for no real reason

    Stuff that your current program depends on, like persistent memory, is not how RAM really works anymore.


    Since you have trouble with files, I would think it is obvious how an article on the subject helps you make the program better.

    We won't point you to a bunch of locations on the web where you can just steal exactly what you are looking for, if that is what you want. You were supposed to learn from what you were linked.
    I don't want to steal anything. I just need help with seekg().

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    I've looked at that before, but I still don't see how it could make the program better?
    Then you probably are not going to get any better as a programmer.

    I need to get entire strings from the file I am writing to
    Getting strings from files is an absolute doddle - If you browsed the C++ reference pages or 'how do i' links i gave you - read the examples and maybe, shock horror even tried them - then you would very soon see that.

    So you want to get a string from a file that already has content and then write back to that same file?

    Or you mean you want to just read from a data file and then write back to another file?

    Either way you will see it is not hard - if you take time to learn.

    Also how could different file types help and how do I use them?
    Believe me anything other than a simple text file is not going to help you at the point you are now - If you can't get that how can you expect to work with files that are written to, data content created as a result of complex algorithms or something like that? that you need to have code to reinterpret back out in order to view? there is no 'filetype' that gives some kind of magic i/o fix.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    I just need help with seekg().
    So go look it up! !

    Or Say what the particular problem you are having with it is.. you know.. explain the problem you are having, in a more informative way - If you just want to know the basic usage of seekg() then that is exactly what documentation - like c++ reference is for! Then you can come back and say things like -

    "what is wrong with my arg to seekg? "

    "why do i get this warning from the compiler? the code compiles but the function call shown seems to produce the warning"

    " why is this not compiling, i get the error shown but according to the docs this should be valid, am i missing something? "

    I get a crash with the following error when i run the program, in the debugger it is on the seekg call , see code below, where am i going wrong?"

    things like that will get you better replies
    stahta01 likes this.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  12. #12
    Registered User
    Join Date
    Apr 2013
    Posts
    8
    Quote Originally Posted by rogster001 View Post
    So go look it up! !

    Or Say what the particular problem you are having with it is.. you know.. explain the problem you are having, in a more informative way - If you just want to know the basic usage of seekg() then that is exactly what documentation - like c++ reference is for! Then you can come back and say things like -

    "what is wrong with my arg to seekg? "

    "why do i get this warning from the compiler? the code compiles but the function call shown seems to produce the warning"

    " why is this not compiling, i get the error shown but according to the docs this should be valid, am i missing something? "

    I get a crash with the following error when i run the program, in the debugger it is on the seekg call , see code below, where am i going wrong?"

    things like that will get you better replies
    post#3 line 89 - 103. Why does the program not output the string of user with seekg()?
    Last edited by kiknwing; 04-19-2013 at 03:10 PM.
    rogster001 likes this.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    Quote Originally Posted by kiknwing View Post
    post#3 line 89 - 103. Why will does the program not output the string of user with seekg()?
    As I said, you are using saving a memory address to the file. I already explained why that is a wrong approach.

    However, there is nothing wrong with saving strings to files and then retrieving them, e.g.
    Code:
    #include <fstream>
    #include <iostream>
    #include <stdexcept>
    #include <string>
    
    class Member
    {
       private:
       std::fstream file;
       std::string user;
    
       public:
       void setUser();
       std::string getUser() const;
       bool saveUser();
       bool loadUser();
    };
    
    void Member::setUser()
    {
       std::cout << "Enter user name: ";
       std::cin >> user;
       if (!std::cin)
       {
          throw std::runtime_error("Set user failed!!\n");
       }
    }
    
    std::string Member::getUser() const
    {
       return user;
    }
    
    bool Member::saveUser()
    {
       bool result = false;
       file.open("record.txt", std::fstream::app | std::fstream::out);
    
       if ( !file.is_open()) {
          throw std::runtime_error("Couldn't open file\n");
       }
    
       file << user << std::endl;
    
       if (file) {
          result = true;
       }
    
       file.close();
       return result;
    }
    
    bool Member::loadUser()
    {
       bool result = false;
       file.open("record.txt", std::fstream::in);
    
       if ( !file.is_open())
       {
          throw std::runtime_error("Couldn't open file\n");
       }
    
       file >> user;
    
       if (file)
       {
          result = true;
       }
       file.close();
       return result;
    }
    
    int main()
    {
       Member member;
       member.setUser();
       if ( !member.saveUser())
       {
          std::cerr << "Failed to save the user (broken file) !!\n";
          return 0;
       }
    
       if ( !member.loadUser())
       {
          std::cerr << "Failed to load the user (record.txt: no content) !!\n";
          return 0;
       }
    
       std::cout << "User retrieved from file is " << member.getUser() << std::endl;
       return 0;
    }
    You can see that this at least retrieves something from a file.
    kiknwing likes this.

  14. #14
    Registered User
    Join Date
    Apr 2013
    Posts
    8
    Quote Originally Posted by whiteflags View Post
    As I said, you are using saving a memory address to the file. I already explained why that is a wrong approach.

    However, there is nothing wrong with saving strings to files and then retrieving them, e.g.
    Code:
    #include <fstream>
    #include <iostream>
    #include <stdexcept>
    #include <string>
    
    class Member
    {
       private:
       std::fstream file;
       std::string user;
    
       public:
       void setUser();
       std::string getUser() const;
       bool saveUser();
       bool loadUser();
    };
    
    void Member::setUser()
    {
       std::cout << "Enter user name: ";
       std::cin >> user;
       if (!std::cin)
       {
          throw std::runtime_error("Set user failed!!\n");
       }
    }
    
    std::string Member::getUser() const
    {
       return user;
    }
    
    bool Member::saveUser()
    {
       bool result = false;
       file.open("record.txt", std::fstream::app | std::fstream::out);
    
       if ( !file.is_open()) {
          throw std::runtime_error("Couldn't open file\n");
       }
    
       file << user << std::endl;
    
       if (file) {
          result = true;
       }
    
       file.close();
       return result;
    }
    
    bool Member::loadUser()
    {
       bool result = false;
       file.open("record.txt", std::fstream::in);
    
       if ( !file.is_open())
       {
          throw std::runtime_error("Couldn't open file\n");
       }
    
       file >> user;
    
       if (file)
       {
          result = true;
       }
       file.close();
       return result;
    }
    
    int main()
    {
       Member member;
       member.setUser();
       if ( !member.saveUser())
       {
          std::cerr << "Failed to save the user (broken file) !!\n";
          return 0;
       }
    
       if ( !member.loadUser())
       {
          std::cerr << "Failed to load the user (record.txt: no content) !!\n";
          return 0;
       }
    
       std::cout << "User retrieved from file is " << member.getUser() << std::endl;
       return 0;
    }
    You can see that this at least retrieves something from a file.
    Question: Is there any way I can use seekg() to retrieve those strings or addresses? I can't include the code you wrote because it conflicts with the Index class I made. Can you also tell me how I can include your class with mine? Thanks, kiknwing.

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    Question: Is there any way I can use seekg() to retrieve those strings or addresses?
    No. In your code you opened the file for appending, which deals with output. seekg() seeks in an input file. Any reference would tell you these things.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21