Member Avatar for format_c

error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream'
1> C:\Program Files\Microsoft Visual Studio 9.0\VC\include\string(527) : see declaration of 'std::getline'

void read()
{
 Inventory items[100];
 string line;
 int i=0;
 ifstream inventdata;
 inventdata.open("Inventory.dat", ios::in);
    if( !inventdata )
    {
      cout << "ERROR: Unable to open " << "Inventory.dat" << " ...exiting." << endl;
      exit(1);
    }
    while(!inventdata.eof())
      {
		  getline(inventdata,items[i].unitPrice);
		  getline(inventdata,items[i].itemNumber);
		  getline(inventdata,items[i].inStock);
	      getline(inventdata,items[i].itemName);
	  i++;
		  
	}
    
	  for(int j=0;j<100;j++)
	  {
	    cout<<items[j];
	  }
	  
	
  
  
    
    
    return ;

}

this is just the section where the code occurs after the while loop. i cant seem to find a fixx.

Recommended Answers

All 8 Replies

you should use while(inventdata.good()){...}

Member Avatar for format_c

just made that change but still the same
error is detected at line 15. seems to be the getline.

I believe the order of operations is messing you up. Try this instead, for all 3 getlines:

getline(inventdata,(items[i]).unitPrice);
Member Avatar for format_c

gave that a try.... still no avail. ill paste the whole code

header file with the error.

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;


struct Inventory
{
  char itemName[25];
  int itemNumber;
  int inStock;
  double unitPrice;
};


void read();

/*
*******************************************************************************
*******************************************************************************
*
* Function Name : read
* 
* Input         : Reference Address to Object data for Struct Inventory
* 
* Output        :
*
* Process       : 
*
*
*******************************************************************************
*******************************************************************************
*/
void read()
{
 Inventory items[100];
 string line;
 int i=0;
 ifstream inventdata;
 inventdata.open("Inventory.dat", ios::in);
    if( !inventdata )
    {
      cout << "ERROR: Unable to open " << "Inventory.dat" << " ...exiting." << endl;
      exit(1);
    }
    while(inventdata.good())
      {
		  getline(inventdata,(items[i]).unitPrice);
		  getline(inventdata,(items[i]).itemNumber);
		  getline(inventdata,(items[i]).inStock);
	      getline(inventdata,(items[i]).itemName);
	  i++;
		  
	}
    
	  for(int j=0;j<100;j++)
	  {
	    cout<<items[j];
	  }
	  
	
  
  
    
    
    return ;

}

The main

#include <iostream>
#include "Invent.h"
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;


//Inventory addItem();
//void write(Inventory &);

int main()
{
 
  
  ofstream inventdata;
  inventdata.open("Inventory.dat", ios::app);
    if( !inventdata )
    {
      cout << "ERROR: Unable to open " << "Inventory.dat" << " ...exiting." << endl;
      exit(1);
    } 
 
  int i=0;
  char last = 'n';
  
  while(last == 'n' && i < 100 )
  {
  Inventory productData;
   
  cout<<"Enter Item Name"<<endl;
  cin.ignore();
  cin.getline (productData.itemName,25);
  
  cout<<"Unit Price"<<endl;
  cin>>productData.unitPrice;
  
  cout<<"Product Code"<<endl;
  cin>>productData.itemNumber;
  
  cout<<"Total In Stock"<<endl;
  cin>>productData.inStock;
  
  cout<<endl;
  cout<<endl;
  cout<< setw (4);
  
  cout<< productData.unitPrice<<"    "<< productData.itemNumber <<"    "<< productData.inStock <<"    "<< productData.itemName<<endl; 
  inventdata<< productData.unitPrice<<"    "<< productData.itemNumber <<"    "<< productData.inStock <<"    "<< productData.itemName<<endl;
  
  
  i++; 
    
  cout<<"Last Item?? Y/N"<<endl;
  //char last = 'y';
  cin>>last;
  
  
  }
  
  read();
  
 return 0; 
}

OK, I see what's going on now.

Here's your problem:

struct Inventory
{
  char itemName[25];
  int itemNumber;
  int inStock;
  double unitPrice;
};

There are 2 versions of getline(). There are string::getline() and istream::getline(). Both versions are used for reading character data. Neither version is used for reading numeric data (i.e. ints and doubles). The version you are attempting to use is string::getline(). It can only read its data into a std::string, which you don't have.

The istream::getline() version reads data into a C-style string (char array). You should be using this version on Line 54 of your header to read into Inventory::itemName. To read into itemNumber, inStock, and unitPrice, you will need to use some other input operation/function.

The problem is that you are readin all line in single double value, second argument should be array or string...

Member Avatar for format_c

Thanx guyz.. that problem is now solved now.....im getting another error when i try to implement a bubble sort.

error is
error C2106: '=' : left operand must be l-value
error C2106: '=' : left operand must be l-value
error C2228: left of '.itemName' must have class/struct/union

if( list[q].itemNumber>list[q+1].itemNumber )
			{
				temp[q].unitPrice = list[q].unitPrice;
				temp[q].itemNumber = list[q].itemNumber;
				temp[q].inStock = list[q].inStock;
				temp[q].itemName = list[q].itemName; // <-- this line 
				list[q].unitPrice=list[q+1].unitPrice;
				list[q].itemNumber=list[q+1].itemNumber; 
				list[q].inStock=list[q+1].inStock;
				list[q].itemName=list[q+1].itemName; //<-- this line
				list[q+1].unitPrice = temp[q].unitPrice;
				list[q+1].itemNumber = temp[q].itemNumber;
				list[q+1].inStock = temp[q].inStock;
				list[q+1].itemName= temp[q].itemName; //<<-- this line
			}

thanx

If you haven't changed things, itemName is a C-style string. You can't use a regular assignment statement with a C-style string. You must use either strcpy() or strncpy() to copy one string into the other. Of the 2, strncpy() is a better choice because it's a "safer" option.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.