Thread: filing

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    27

    filing

    hey guys ! i have been given an assignment . the question is stated below :

    1. Create a struct Product with fields id (int), name (char*), price (double), and isAvailable (bool).
    2. Write a function which returns a pointer to a dynamically allocated Product filled with the provided data.
    Product * getNewProduct(int id, char * name, int nameLength, double price, bool isAvailable);

    3. Write a function to read data of products from a file and fill it in a dynamically allocated array of Products and return it. Here is the format of the file.

    3
    1234 Phone 4400.22 0
    1111 Laptop 34999.99 1
    9087 Burger 45.00 0








    The first line contains an integer the number of products, on each of the following lines there is data for each product in following format
    <id><name><price><isAvailable>
    The name will not contain any spaces.
    4. Write a function that takes a Product * (which points to the array of products) and an integer id. The function searches the product with that id in the array and returns a pointer to that Product if found. The function returns NULL otherwise.

    5. Write a function that takes a Product * and a char * name. The function searches the product with that name in the array and returns a pointer to that Product if found. The function returns NULL otherwise.

    so far i have done the first three parts of this question but i have some problem . my code is pasted below :

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std ;
    struct Product
    {
    	int id ;
    	char *name ;
    	double price ;
    	bool isAvailable ;
    };
    Product* getNewProduct(int id, char* name , double price , bool isAvailable );
    bool* ReadData (ifstream &inFile, Product P[] );
    int main ()
    {
    
    
    	system("pause");
    	return 0 ;
    }
    Product* getNewProduct(int id, char* name , double price , bool isAvailable )
    {
    	Product *P = new Product ;
    	(*P).id = id ;
    	(*P).isAvailable = isAvailable ;
    	(*P).name = name ;
    	(*P).price = price ;
    
    
    	return P ;
    }
    bool* ReadData (ifstream &inFile , Product P[] )
    {
    	ifstream inFile ;
    	int size ;
    	string line ;
    	bool isAvailable ;
    	
    	inFile.open("file.txt");
    	if (!inFile.is_open())
    	{
    		cout << " opening of file failed " << endl;
    		exit(1);
    	}
    
    
    	while (!inFile.eof())
    	{
    		
    		inFile >> size ;
    		Product *arr = new Product [size];
    		
    		for (int i = 0 ; i<size ; i++ )
    		{
    			getline (inFile,line,' ' );
    			P[i].id = atoi(line);       // gives error (underlines line stating that no suitable conversion from std::string to const char* exists )
    
    
    			P[i].name = getline(inFile,line,' ' ); // gives error underlining "getline"
    
    
    			getline (inFile,line,' ' );
    			P[i].price = atof(line); // gives error (underlines line stating that no suitable conversion from std::string to const char* exists )
    
    
    			P[i].isAvailable = inFile >> isAvailable ;
    
    
    		}
    	}
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    but i have some problem
    When you have "some problem" you wish to address with a forum, it's in your best interests to provide exact details of the "problem" to the forum. Don't expect people to download your code, compile it, and run it to try to discover for you what your "problem" is. Particularly when the program relies on data to which we have no access.

    How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    Quote Originally Posted by rags_to_riches View Post
    When you have "some problem" you wish to address with a forum, it's in your best interests to provide exact details of the "problem" to the forum. Don't expect people to download your code, compile it, and run it to try to discover for you what your "problem" is. Particularly when the program relies on data to which we have no access.

    How To Ask Questions The Smart Way
    bro i have mentioned the exact location of the problem and i know that nobody will download my code and compile it and try to sort out the problems.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    As a rule, don't just use "IntelliSense" to tell you errors. Compile the code and paste the errors in your post, rather than make us look through the code. Or at least tell us in your post that you've pointed out the errors in your code.

    OK, so line is an instance of the C++ string class. atoi() expects a const char *. Look at that page and see if you can't find a function that converts a C++ string to a const char *.

    You used getline() properly twice. For some reason you changed how you used getline() properly into something that isn't correct. Look at that page and notice what the return value of getline() is, and what you're trying to with that return value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. filing
    By waqas94 in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2013, 02:32 AM
  2. A little problem with filing in C
    By TheUmer in forum C Programming
    Replies: 19
    Last Post: 03-22-2010, 11:33 AM
  3. how to make a program using Filing in C++?
    By zedora in forum C++ Programming
    Replies: 1
    Last Post: 01-14-2010, 11:24 AM
  4. filing problems..urgent...
    By must in forum C++ Programming
    Replies: 0
    Last Post: 07-23-2002, 04:31 AM