Need help with struct and strings

This is the backend storeage for a game

Page 1 of 1

5 Replies - 908 Views - Last Post: 27 June 2007 - 09:46 PM Rate Topic: -----

#1 Creative  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-June 07

Need help with struct and strings

Posted 26 June 2007 - 10:23 PM

Ok i been going thought the Tutorials and I working with the file save one but I ran into big problems with strings and been trying to work it out for hours now so I posted asking for help.

Here is my code:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;
#define Hardwarestructid 2

struct HARDWARE{
	  int ID;
	  // Tryed char[30]
	  string HARDWARE_NAME;<<<<<<<<<<<<<<<<<<<<ERROR will display it but when saved then will not?
	  float PRICE;
	  int INSTOCK;
	  int YOUHAVE;
	  float RATEING;	  
} hard;

/////////////////<FUNCTIONS>
void printhardware (HARDWARE hard);
void savetofile (HARDWARE hard);
void loadfromfile ();
////////////////</FUNCTIONS>
//main program
int main(int argc, char *argv[])
{
  
/////////////////<LOAD>
loadfromfile();
printhardware(hard);

////////////////</LOAD>

////////////////<VARS>
string mystringprice;
string myquantity;
string myyouhave;
string myrateing;
string myinstock;
int n;
///////////////</VARS> 

  for (n=0; n<Hardwarestructid; n++)
  {	
	hard.ID =n +1;
	cout << " (" << hard.ID << ")\n";

	cout << "\nEnter Hardware Name: ";
	getline (cin,hard.HARDWARE_NAME);
	
	cout << "Enter Price of Hardware: ";
	getline (cin,mystringprice);
	stringstream(mystringprice) >> hard.PRICE;		
 
	cout << "Enter INSTOCK: ";	  
	getline (cin,myinstock);	
	stringstream(myinstock) >> hard.INSTOCK;  
		 
	cout << "Enter How many you have: ";	  
	getline (cin,myyouhave);	
	stringstream(myyouhave) >> hard.YOUHAVE;		 
	  
	cout << "Enter Rateing: ";	  
	getline (cin,myrateing);	
	stringstream(myrateing) >> hard.RATEING;		
  }

  cout << "\nYou have entered these Hardware:\n";
  ////////////////<PRINT STRUCT>
  for (n=0; n<Hardwarestructid; n++){
	  printhardware (hard);
	  savetofile (hard);
	  }
  ////////////////</PRINT STRUCT>
  
  
  system("pause");
  return 0;
  
}

//functions
void printhardware (HARDWARE hard)
{
  cout << "ID:	(" << hard.ID << ")\n";
  cout << "Hardware Name:	(" << hard.HARDWARE_NAME << ")\n";
  cout << "Price:	(" << hard.PRICE << ")\n";
  cout << "InStock:	(" << hard.INSTOCK << ")\n";
  cout << "You Have:	(" << hard.YOUHAVE << ")\n";
  cout << "Rateing:	(" << hard.RATEING << ")\n\n";  
}


void savetofile (HARDWARE hard)
{
 fstream file;  
 file.open("file.dat",ios::out);
 file.write((char *)(&hard), sizeof(hard));
 file << flush; 
 file.close();
}

void loadfromfile ()
{
 ifstream fin("file.dat", ios::binary);
 fin.read((char *)(&hard), sizeof(hard));
}


I want to hold tons of data and load it on start up as well as many more struct will be needed I have looked about and i think struct is the best way but if I am wrong please let me know.

Thanks Creative

This post has been edited by jayman9: 27 June 2007 - 03:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with struct and strings

#2 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Need help with struct and strings

Posted 27 June 2007 - 05:59 AM

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <conio.h>

using namespace std;
#define Hardwarestructid 2

struct HARDWARE{
int ID;
string HARDWARE_NAME;
float PRICE;
int INSTOCK;
int YOUHAVE;
float RATEING; 
} hard;

/////////////////<FUNCTIONS>
void printhardware (HARDWARE hard);
void savetofile (HARDWARE hard);
void loadfromfile (HARDWARE hard);
////////////////</FUNCTIONS>
//main program
int main(int argc, char *argv[])
{
	///////////////<VARS>
	string mystringprice;
	string myquantity;
	string myyouhave;
	string myrateing;
	string myinstock;
	int n;
	///////////////</VARS> 

	/////////////////<LOAD>
	loadfromfile(hard);

	for (n=0; n<Hardwarestructid; n++)
	{ 
		hard.ID =n +1;

		cout << "Enter Hardware Name: ";
		getline (cin,hard.HARDWARE_NAME);

		cout << "Enter Price of Hardware: ";
		getline (cin,mystringprice);
		stringstream(mystringprice) >> hard.PRICE; 

		cout << "Enter INSTOCK: "; 
		getline (cin,myinstock); 
		stringstream(myinstock) >> hard.INSTOCK; 

		cout << "Enter How many you have: "; 
		getline (cin,myyouhave); 
		stringstream(myyouhave) >> hard.YOUHAVE; 

		cout << "Enter Rateing: "; 
		getline (cin,myrateing); 
		stringstream(myrateing) >> hard.RATEING; 

		system("cls"); //Clear Screen

		cout << "\nYou have entered these Hardware:\n";
		printhardware(hard); //display hardware after each time through
		savetofile(hard); //save hardware after each tiem through
		getch(); //wait for user input
		system("cls"); //clear the screen
	}
	
	return 0;

}//end main

//functions
void printhardware (HARDWARE hard)
{
	cout << "ID: ("			<< hard.ID			<< ")\n";
	cout << "Hardware Name: (" << hard.HARDWARE_NAME << ")\n";
	cout << "Price: ("		 << hard.PRICE		 << ")\n";
	cout << "InStock: ("	   << hard.INSTOCK	   << ")\n";
	cout << "You Have: ("	  << hard.YOUHAVE	   << ")\n";
	cout << "Rateing: ("	   << hard.RATEING	   << ")\n\n"; 
}


void savetofile (HARDWARE hard)
{
	fstream file; 
	file.open("file.dat",ios::out | ios::binary | ios::app);
	file.write((char *)(&hard), sizeof(hard));
	file.close();
}

void loadfromfile (HARDWARE hard)
{
	ifstream fin;
	fin.open("file.dat",ios::binary);
	if(fin.fail()) //if file does not exist yet
	{
		cout << "No records currently available";
		getch();
	}
	else
	{
		cout << "Current Records:\n\n";
		for(int nIndex = 0; nIndex < Hardwarestructid; nIndex++)
		{
			fin.read((char *)(&hard), sizeof(hard));
			printhardware(hard);
		}
	}
	fin.close();
	getch();	   //wait for user input
	system("cls"); //clear screen
}



None of the code you had was "wrong" but there was just some logic error.. i fixed your functions and they depend on the #define hardwarestruc id value which is what it looked like you were doing in the first place... if not feel free to change it :) It will read/write the binary records and i fixed up the display a little but you can do that yourself.. it works as far as I can tell.

This post has been edited by Topher84: 27 June 2007 - 06:59 AM

Was This Post Helpful? 0
  • +
  • -

#3 Creative  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-June 07

Re: Need help with struct and strings

Posted 27 June 2007 - 04:17 PM

Topher84 Thank you for taking the time in helping me but when I ran that code and it still does not output the string only the others.

This post has been edited by Creative: 27 June 2007 - 04:19 PM

Was This Post Helpful? 0
  • +
  • -

#4 Creative  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-June 07

Re: Need help with struct and strings

Posted 27 June 2007 - 06:15 PM

Ok i got it saveing and loading of the string now as a char but..
It always the same string no matter what struct it is in?

like if i set the 1st one as Test1111 for the hardware name and then the 2nd as test2222
on save and load both the names for ID1 and ID2 are test22222 any ideas?

here is it working
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <conio.h>

using namespace std;
#define Hardwarestructid 2

struct HARDWARE{
int ID;
char  HARDWARENAME[30];
//string HARDWARE_NAME;
float PRICE;
int INSTOCK;
int YOUHAVE;
float RATEING; 
} hard;

/////////////////<FUNCTIONS>
void printhardware (HARDWARE hard);
void savetofile (HARDWARE hard);
void loadfromfile (HARDWARE hard);
////////////////</FUNCTIONS>
//main program
int main(int argc, char *argv[])
{
	///////////////<VARS>
	string mystringprice;
	string myquantity;
	string myyouhave;
	string myrateing;
	string myinstock;
	int n;
	///////////////</VARS> 

	/////////////////<LOAD>
	loadfromfile(hard);

	for (n=0; n<Hardwarestructid; n++)
	{ 
		hard.ID =n +1;

		cout << "Enter Hardware Name: ";
	   
		cin.getline(hard.HARDWARENAME,30);

		cout << "Enter Price of Hardware: ";
		getline (cin,mystringprice);
		stringstream(mystringprice) >> hard.PRICE; 

		cout << "Enter INSTOCK: "; 
		getline (cin,myinstock); 
		stringstream(myinstock) >> hard.INSTOCK; 

		cout << "Enter How many you have: "; 
		getline (cin,myyouhave); 
		stringstream(myyouhave) >> hard.YOUHAVE; 

		cout << "Enter Rateing: "; 
		getline (cin,myrateing); 
		stringstream(myrateing) >> hard.RATEING; 

		system("cls"); //Clear Screen

		cout << "\nYou have entered these Hardware:\n";
		printhardware(hard); //display hardware after each time through
		savetofile(hard); //save hardware after each tiem through
		getch(); //wait for user input
		system("cls"); //clear the screen
	}
	
	return 0;

}//end main

//functions
void printhardware (HARDWARE hard)
{
	cout << "ID: ("			<< hard.ID			<< ")\n";
	cout << "Hardware Name: (" << hard.HARDWARENAME << ")\n";
	cout << "Price: ("		 << hard.PRICE		 << ")\n";
	cout << "InStock: ("	   << hard.INSTOCK	   << ")\n";
	cout << "You Have: ("	  << hard.YOUHAVE	   << ")\n";
	cout << "Rateing: ("	   << hard.RATEING	   << ")\n\n"; 
}


void savetofile (HARDWARE hard)
{
	fstream file; 
	file.open("file.dat",ios::out | ios::binary /*| ios::app*/);
	file.write((char *)(&hard), sizeof(hard));
	file.close();
}

void loadfromfile (HARDWARE hard)
{
	ifstream fin;
	fin.open("file.dat",ios::binary);
	if(fin.fail()) //if file does not exist yet
	{
		cout << "No records currently available";
		getch();
	}
	else
	{
		cout << "Current Records:\n\n";
		for(int nIndex = 0; nIndex < Hardwarestructid; nIndex++)
		{
			fin.read((char *)(&hard), sizeof(hard));
			printhardware(hard);
		}
	}
	fin.close();
	getch();	   //wait for user input
	system("cls"); //clear screen
}

This post has been edited by born2c0de: 28 June 2007 - 12:01 AM

Was This Post Helpful? 0
  • +
  • -

#5 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Need help with struct and strings

Posted 27 June 2007 - 08:37 PM

The code i gave you works fine for me

I enter:

test,1,1,1,1 and then test2,2,2,2,2

and it shows me the values after each input.. then i close the program and re-open and it loads them as ID1 test and ID2 test2 on the main screen when you first start the program.. try doing a clean solution and complete rebuild on your code

Load after inputting the previous data:

ID: (1)
Hardware Name: test
Price: (1)
Instock: (1)
You Have: (1)
Rating: (1)

ID: (2)
Hardware Name: test2
Price: (2)
Instock: (2)
You Have: (2)
Rating: (2)

I don't see the problem
Was This Post Helpful? 0
  • +
  • -

#6 Creative  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-June 07

Re: Need help with struct and strings

Posted 27 June 2007 - 09:46 PM

Thanks Topher84

This post has been edited by Creative: 28 June 2007 - 03:11 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1