CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 35
  1. #1
    Join Date
    Jun 2013
    Posts
    16

    Visual studio C++ 2008 MFC quiz app

    I've been trying to make a little quiz application that takes questions from a file and them presents question by question on a static pannel, a few buttons that have the possible answers on them written as the questions go, and on the bottom a little place where we can see whats our score

    when we finish the quiz, the score gets writen in a data.dat file and thats that

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Visual studio C++ 2008 MFC quiz app

    Have fun.

  3. #3
    Join Date
    Jun 2013
    Posts
    16

    Re: Visual studio C++ 2008 MFC quiz app

    Quote Originally Posted by GCDEF View Post
    Have fun.

    thanks

  4. #4
    Join Date
    Jun 2013
    Posts
    16

    Re: Visual studio C++ 2008 MFC quiz app

    anyway, can someone post me something that might help?

    google didnt, irc networks didnt, other forums didnt

    codeguru is someone i consider that might help at least in pointing me on how to do it properly

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Visual studio C++ 2008 MFC quiz app

    The best way to get help is to ask a specific question about a problem you're having.

  6. #6
    Join Date
    Jun 2013
    Posts
    16

    Re: Visual studio C++ 2008 MFC quiz app

    Quote Originally Posted by GCDEF View Post
    The best way to get help is to ask a specific question about a problem you're having.
    i have a problem with opening files and copying the whole text into a string

    i get something like this: http://i.imgur.com/LpiYXHl.png Im thinking this has something to do with unicode file misconceptions

    altho i've tried to do this without the file, and getting the text on the static was imposible, and getting some text on the button

    i think i know how to make the score system work but im not sure about that ether

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Visual studio C++ 2008 MFC quiz app

    The best thing to do is to post your code here so that we can have a look at it and provide comment and guidance. Without seeing the code that is causing the problem we can't really offer you much help. When you post code, please use code tags (Go Advanced, select code and click '#').
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Jun 2013
    Posts
    16

    Post Re: Visual studio C++ 2008 MFC quiz app

    Quote Originally Posted by 2kaud View Post
    The best thing to do is to post your code here so that we can have a look at it and provide comment and guidance. Without seeing the code that is causing the problem we can't really offer you much help. When you post code, please use code tags (Go Advanced, select code and click '#').
    Code:
    BOOL CQuizDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	// Set the icon for this dialog.  The framework does this automatically
    	//  when the application's main window is not a dialog
    	SetIcon(m_hIcon, TRUE);			// Set big icon
    	SetIcon(m_hIcon, FALSE);		// Set small icon
    
    	// TODO: Add extra initialization here
    	char* pszFileName = "questions.txt";
    	CFile myFile;
    	CFileException fileException;
    	if ( !myFile.Open( "questions.dat", CFile::modeCreate |   
              CFile::modeReadWrite, &fileException ) )
    	{
    	    TRACE( "Can't open file %s, error = %u\n",   
    		pszFileName, fileException.m_cause );
    		MessageBox("Doesn't Work", "Doesn't Work", MB_OK); //This is just there so i can see if the file is not open
    	}
    	char szBuffer[5000];
    	UINT nActual = 0;
    
    	myFile.Seek( 0, CFile::begin );
    	nActual = myFile.Read( szBuffer, sizeof( szBuffer ) ); 
    	MessageBox(szBuffer, "foo", MB_OK); // This is here so i can see if the text is transfered here correctly
    	return TRUE;  // return TRUE  unless you set the focus to a control
    http://i.imgur.com/oSQo9Hr.png

    i got this monstrocity of an error, its been driving me crazy, and yes i've tried changing the encoding of the questions.txt, even tried different extensions

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Visual studio C++ 2008 MFC quiz app

    You're reading the file without ever writing to it, so you're just displaying an uninitialized buffer.

    Why are you setting pszFileName to "questions.txt", then hard coding "questions.dat" in the open call?
    Last edited by GCDEF; June 11th, 2013 at 08:36 AM.

  10. #10
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Visual studio C++ 2008 MFC quiz app

    1) CFile::modeCreate empty's the file, so as GCDEF mentioned, there is nothing read into the buffer.

    2) you need to NULL terminate the buffer

  11. #11
    Join Date
    Jun 2013
    Posts
    16

    Re: Visual studio C++ 2008 MFC quiz app

    Quote Originally Posted by GCDEF View Post
    You're reading the file without ever writing to it, so you're just displaying an uninitialized buffer.

    Why are you setting pszFileName to "questions.txt", then hard coding "questions.dat" in the open call?
    because there was previously an error when i did it with the string, the error is gone for some reason


    1) CFile::modeCreate empty's the file, so as GCDEF mentioned, there is nothing read into the buffer.

    2) you need to NULL terminate the buffer
    explane 2) please

  12. #12
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Visual studio C++ 2008 MFC quiz app

    Many routines that process character strings require a NULL character to indicate the end of the string.
    Otherwise, the routine would not know when to stop processing.

    In your case, the simplest way:

    Code:
    char szBuffer[5000] = {0};
    However, if the actual number of characters read in is 5000, you need
    to set szBuffer[4999] to NULL.

  13. #13
    Join Date
    Jun 2013
    Posts
    16

    Re: Visual studio C++ 2008 MFC quiz app

    Philip Nicoletti

    now there is no text at all, i think this is a good sign

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Visual studio C++ 2008 MFC quiz app

    Quote Originally Posted by Joe412 View Post
    Philip Nicoletti

    now there is no text at all, i think this is a good sign
    No it's not. You're still trying to read an empty file (I assume). The NULL just stops the garbage being displayed.

  15. #15
    Join Date
    Jun 2013
    Posts
    16

    Re: Visual studio C++ 2008 MFC quiz app

    YES!

    the file becomes empty as soon as i open the program

    Code:
    BOOL CQuizDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	// Set the icon for this dialog.  The framework does this automatically
    	//  when the application's main window is not a dialog
    	SetIcon(m_hIcon, TRUE);			// Set big icon
    	SetIcon(m_hIcon, FALSE);		// Set small icon
    
    	// TODO: Add extra initialization here
    	char* pszFileName = "crap.txt";
    	CFile myFile;
    	CFileException fileException;
    	if ( !myFile.Open( pszFileName,CFile::modeRead, &fileException ) )
    	{
    	    TRACE( "Can't open file %s, error = %u\n",   
    		pszFileName, fileException.m_cause );
    		MessageBox("Doesn't Work", "Doesn't Work", MB_OK); //This is just there so i can see if the file is not open
    	}
    	char szBuffer[5000] = {0};
    	UINT nActual = 0;
    
    	//myFile.Seek( 0, CFile::begin ); // this over here made crap.txt an empty file
    	myFile.Read( szBuffer, sizeof( szBuffer ) ); 
    	MessageBox(szBuffer, "foo", MB_OK); // This is here so i can see if the text is transfered here correctly
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured