Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

277 Neutral

About Enerjak

  • Rank

Personal Information

  • Interests
  1. I decided to write a small program that writes a vector to a binary file. However, I'm having some issues with the code. Here is what I have so far: #include <iostream> #include <map> #include <string> #include <vector> #include <cmath> #include <math.h> #include <fstream> using namespace std; class Book { protected: string m_pBookTitle; string m_pAuthor; string m_pGenre; int m_pNumberOfPages; public: Book() : m_pBookTitle(""), m_pAuthor(""), m_pGenre(""), m_pNumberOfPages(0){} Book(string title, string author, string genre, int numPages) : m_pBookTitle(title), m_pAuthor(author), m_pGenre(genre), m_pNumberOfPages(numPages) { } ~Book(){} string getBookTitle() const { return m_pBookTitle; } string getBookAuthor() const { return m_pAuthor; } string getBookGenre() const { return m_pGenre; } int getNumPages() const { return m_pNumberOfPages; } }; int main(int argc, char** argv) { vector<Book*> Books; Book* book1 = new Book("The man with a dog", "Robert White", "scifi", 300); Books.push_back(book1); Book* book2 = new Book("Just got here", "James Hancock", "Fantasy", 100); Books.push_back(book2); Book* book3 = new Book("The Girl with the Dragon Tattoo", "Eddinton Carlos","Fiction", 500); Books.push_back(book3); cout << "Number of books: " << Books.size() << endl; ofstream bookFile; bookFile.open("Books.book", ios::binary); if (bookFile.is_open()) { for (vector<Book*>::iterator i = Books.begin()+1; i != Books.end(); i++) { bookFile.write((char*)&(*i), sizeof((*i))); } } bookFile.close(); ifstream bookFileIn; bookFileIn.open("Books.book", ios::binary); vector<Book*> LoadedBooks; if (bookFileIn.is_open()) { Book* book = nullptr; while (!bookFileIn.eof()) { bookFileIn.read((char*)&book, sizeof(book)); LoadedBooks.push_back(book); } } bookFileIn.close(); for (vector<Book*>::iterator i = LoadedBooks.begin(); i != LoadedBooks.end(); i++) { Book* b = (*i); cout << "Book title: " << b->getBookTitle() << endl; cout << "Book Author: " << b->getBookAuthor() << endl; cout << "Genre: " << b->getBookGenre() << endl; cout << "Number of pages: " << b->getNumPages() << endl; } cout << "Number of books: " << LoadedBooks.size() << endl; system("PAUSE"); return EXIT_SUCCESS; } This is what happens with the +1: http://puu.sh/ChluR/3c42a42137.png And removing the +1: http://puu.sh/Chlwz/ed52d85cdb.png The problem is that one of the books gets added twice. Someone in the Discord server I'm in said that I can't do bookFile.write((char*)&(*i),sizeof((*i)) because the book has non trivial types such as strings. What should I do?
  2. protected void menuImage_ChildClick(Object sender, EventArgs e) { if(pbxPhoto.Image != null && sender is MenuItem) { MenuItem mi = (MenuItem)sender; pbxPhoto.SizeMode = modeMenuArray[mi.Index]; pbxPhoto.Invalidate(); } } I'm trying to make one click function to handle multiple menus. Using this code does not work when I click the menu buttons after the image is loaded in the picture box. Here is where I set the event handler of both menu items. // // menuStretch // this.menuStretch.Name = "menuStretch"; this.menuStretch.Size = new System.Drawing.Size(152, 22); this.menuStretch.Text = "S&tretch to Fit"; this.menuStretch.Click += new System.EventHandler(this.menuImage_ChildClick); // // menuActual // this.menuActual.Name = "menuActual"; this.menuActual.Size = new System.Drawing.Size(152, 22); this.menuActual.Text = "&Actual Size"; this.menuActual.Click += new System.EventHandler(this.menuImage_ChildClick); Menu Stretch and Menu Actual are ToolStripMenuItems, which don't have an index variable as I've noticed. How do I get this to work? Otherwise I have to make a separate event function per menu item, which, depending on the amount of image properties, could be a a lot.
  3. Whoops! Here is the right link: https://stackoverflow.com/questions/9763796/reading-from-a-text-file-and-splitting-each-individual-line-into-different-arra go all the way down.
  4. I found this on Google: https://stackoverflow.com/questions/24041113/error-1-inconsistent-accessibility-return-type-is-less-accessible-than-method It shows how to split the lines into an array using a delimiter - like you said I should use. I should have searched more than make a post on it. Thanks for your help.
  5. I got the code in Java to work. Here is the fixed code: package address_book; import java.io.*; import java.util.*; import javax.swing.*; public class application { public static void main(String[] args) { Scanner file = null; person per = null; try { file = new Scanner(new File("address_book.txt")); String firstName = ""; String lastName = ""; String address = ""; while(file.hasNext()) { firstName = file.next(); lastName = file.next(); address = file.nextLine(); per = new person(firstName, lastName, address); } file.close(); System.out.println(per.getFirstName() + " " + per.getLastName() + " " + per.getAddress()); } catch(FileNotFoundException e) { } } } This is what it outputs: And here is the address book file again just for reference: As you can see from the fixed code above, I have made three string variables, firstName, lastName, and address. In order to get the first name from the file, I used Scanner.next(), which reads the string "Robert." Scanner.next() only reads one string until there is a space. Next I read in the last name using another Scanner.next() to get the last name. The Scanner.nextline() gets the rest of the line in the file - which is the address.
  6. No, Name and address are not on separate lines. However, the code I wrote here: package Def; import java.io.*; import java.util.Scanner; import javax.swing.JOptionPane; public class Application { public void main(String[] ages) { try { Scanner scan = new Scanner(new File("Address_book.txt")); String Name = ""; String Address = ""; while(scan.hasNext()) { Name = scan.next(); Address = scan.next(); } JOptionPane.showMessageDialog(null, Name); scan.close(); } catch(FileNotFoundException e) { } } } This should load Address_book.txt - which for some reason it doesn't, but that's another problem all together.
  7. I've recently begun to learn C#. As you can see from this code snippet: Scanner file = new Scanner(new File("hashValues.txt")); String miningDay = ""; String currency = ""; double netPayOut = 0.0; String i = ""; String walletID = ""; Array hashValues; while(file.hasNext()) { miningDay = file.next(); currency = file.next(); netPayOut = file.nextDouble(); i = file.next(); walletID = file.next(); } Java has a class called Scanner which allows you to open a file through the file class. The Scanner class has functions to read individual strings, as well as int and double data types. Does C# have something similar to this? I tried to do something like this, but it doesn't really work. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Globalization; using System.IO; namespace Mod3_Lab1 { class Program { static void Main(string[] args) { StreamReader addressBook = null; String Name = ""; String Address = ""; try { addressBook = new StreamReader("address_book.txt"); while(!addressBook.EndOfStream) { Name = addressBook.ReadLine(); Address = addressBook.ReadLine(); } addressBook.Close(); Console.WriteLine("Name: " + Name); Console.WriteLine("Address: " + Address); } catch(FileNotFoundException) { } } } } Here is the output:
  8.   Should I do that for the outer loop as well?    Edit: Ignore this, I got what you meant. 
  9. I thought I'd try my hand at adding buttons pragmatically using arrays. I have this little function that I thought would work well enough:  private bool createButtons() { bool bButtonsCreated = false; int row = NumPad.RowCount; int col = NumPad.ColumnCount; int i = 1; int j = 1; int num = (i * j); for (i = 1; i < row; i++) { for(j = 1; i < col; j++) { // set the text for the buttons. CalcButtons = new Button[num]; CalcButtons[num] = new Button(); CalcButtons[num].Text = "7"; CalcButtons[num - 1].Text = "8"; CalcButtons[num - 2].Text = "9"; CalcButtons[num - 3].Text = "4"; CalcButtons[num - 4].Text = "5"; CalcButtons[num - 5].Text = "6"; CalcButtons[num - 6].Text = "1"; CalcButtons[num - 7].Text = "2"; CalcButtons[num - 8].Text = "3"; CalcButtons[num].Size = new Size(60, 60); } } if(CalcButtons[num] != null) { bButtonsCreated = true; NumPad.Container.Add(CalcButtons[num]); } else { MessageBox.Show("Buttons not created", "Error", MessageBoxButtons.OK); } return bButtonsCreated; } However, as you can see, it gives out an indexOutOfBounds exception:      What can I do to fix this? Is this the right way to use arrays to do this? 
  10. I'm trying to make an about book using C#. Now, I used a template for it. I edited the assembly description in the properties window. The text shows up in design mode, however, when the application is ran the description box is empty. What can I do to fix this?    Could using .NET 4.0 be the reason for this bug? Is it even a bug or an error on my part? I'm using visual studio 2013.   Design mode:     Application running.      please let me know if you need anything else. 
  11. I was able to use Direcr3D9's debug layer when I was using windows 7. I had to switch to windows 10 because it (windows 7) couldn't find the drivers automatically for my hardware, which included the network card to get online so I was screwed. I could just use Direct3D9 without a debug layer but that will make it harder to know when something messed up, such as not releasing  com objects, devices, surfaces. That's the thing. 
  12. Okay, do I have Graphics tools installed:      I'm trying to use Direct3D9's debugging layer. I've set it up in the control panel as you can see:     And I have Debugging info enabled in Visual Studio:     How do I fix this issue? Cause even though that is all set up, nothing gets written to the output window.  
  13. Okay, I downloaded the graphics tools and everything works now. Thanks to everyone for putting up with me.
  14. So in other words, to use the Debugging layer on windows 10, I would need to install visual studio 2015?
  15. After all that, it still doesn't work when using the debugging flag. Am I missing something? I was getting the error with the application verifier checked off and it installed the windows 8 SDK. I'm not sure what else to do to get this to work using the debug layer. I guess getting windows 10 as soon as it came out was a bad idea....(I heard the risks but I still downloaded it when they offered it for free.) Not really sure where to from here.    P.S. sorry for double posting.
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!