Ok I have a simple question I have a header file that has the following code

void   setPartDecription(string partDescrip);
string getPartDecription()  {return partDescription;}

Of course I have other functions but these are the problem functions and they are public. In my main function I have this:

cout << "\nEnter Part Description: ";
cin  >> partDescrip;
Invoices[i].setPartDecription(partDescrip);
cout << Invoices[i].getPartDecription();

After I type in a part description like "This part is for the engine" it goes into an infinite loop. Is there a certain way I need to pass and return the string data type? I've tried

getline(cin, partDescrip);

but its just skips over it and doesn't even let me type a part description, help would be really appreciated.

Ok I have a simple question I have a header file that has the following code

void   setPartDecription(string partDescrip);
string getPartDecription()  {return partDescription;}

Of course I have other functions but these are the problem functions and they are public. In my main function I have this:

cout << "\nEnter Part Description: ";
cin  >> partDescrip;
Invoices[i].setPartDecription(partDescrip);
cout << Invoices[i].getPartDecription();

After I type in a part description like "This part is for the engine" it goes into an infinite loop. Is there a certain way I need to pass and return the string data type? I've tried

getline(cin, partDescrip);

but its just skips over it and doesn't even let me type a part description, help would be really appreciated.

I can't see precisely the problem because I don't have enough code, really...but you should have a member in the class:

string mPartDescription;

and this member string should be the one that is set, and the one that is returned. I would guess that the body of "setPartDescription()" is the problem. Bear in mind that the compiler will make a copy and use the copy to set the variable...to avoid this you should probably declare it as:

void setPartDescription( const string& in )

I usually overload the assignment operator as well as the copy constructor, and make them private and leave them undefined to catch accidental copying and assignment; if you do want to use them move them into public and define them yourself so you can be sure of what is going on. If you don't do this, the compiler will define both for you which may have unwanted side effects...like infinite recursion.

Thanx...
Sean

>>getline(cin, partDescrip);
That should work ok assuming partDescrip is std::string. Why the program skips it is due to other parts of your program. If you have input for integers then you need to clean out the input keyboard buffer of the '\n' that is left. Right after the input integer add cin.ignore('\n'); , or for a more detailed explaination see Narue's thread here.

So if I use getline it still skips over it. Also I only included the setPartDescription function definition to shorten the code. I hope this is enough information. I used the cin.ignore as well and if I type something like "This part is for the engine." the output in the program is "or the engine."

Header File:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Invoice
{
public:

          Invoice();

   void   setInvoiceNum(int invceNum);
   void   setQtyShipped(int qtyShip);
   void   setPartNum(int partNum);
   void   setPartDecription(string partDescrip);
   void   setUnitPrice(float untPrice);
   void   setSalesTaxRate(float salesTaxRte);
   void   setShippingCost(float shipCost);

   int    getInvoiceNum()     const {return invoiceNumber;}
   int    getQtyShipped()     const {return qtyShipped;}
   int    getPartNum()        const {return partNumber;}
   string getPartDecription()       {return partDescription;}
   float  getUnitPrice()      const {return unitPrice;}
   float  getSalesTaxRate()   const {return salesTaxRate;}
   float  getShippingCost()   const {return shippingCost;}

   float  calcSalesTaxAmnt();
   float  calcTotalCost();
   void   printInvoiceData();

private:
   int    invoiceNumber;
   int    qtyShipped;
   int    partNumber;
   string partDescription;
   float  unitPrice;
   float  salesTaxRate;
   float  salesTaxAmnt;
   float  shippingCost;
   float  totalCost;

};

void Invoice::setPartDecription(string partDescrip)
{
   partDescription = partDescrip;
}

Main Fuction:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

#include "invoiceheader.h"

#define MAX_INVOICES 10000
#define EXIT_CODE    0

int main()
{
   Invoice *Invoices = new Invoice[MAX_INVOICES];
   int     invoiceNum;
   int     qtyShipped;
   int     partNum;
   string  partDescrip;
   float   unitPrice;
   string  editChoice;
   bool    done = true;


   do
   {
      for(int i=0; i<MAX_INVOICES; i++)
      {
         cout << "\nEnter an Invoice Number (or " << EXIT_CODE << " to exit):";
         cin  >> invoiceNum;
         if(invoiceNum == EXIT_CODE)
         {
            done = false;
            break;
         }
         else
         {
            Invoices[i].setInvoiceNum(invoiceNum);
            cout << Invoices[i].getInvoiceNum();  // debug
            cout << "\nEnter Quantity Shipped: ";
            cin  >> qtyShipped;
            Invoices[i].setQtyShipped(qtyShipped);
            cout << Invoices[i].getQtyShipped();  // debug
            cout << "\nEnter Part Number: ";
            cin  >> partNum;
            Invoices[i].setPartNum(partNum);
            cout << Invoices[i].getPartNum();  // debug
            cout << "\nEnter Part Description: ";
            cin.ignore('\n');
            getline(cin, partDescrip);
            Invoices[i].setPartDecription(partDescrip);
            cout << Invoices[i].getPartDecription(); // debug, problem area
            cout << "\nEnter Unit Price: ";
            cin  >> unitPrice;
            Invoices[i].setUnitPrice(unitPrice);
         }
      }

   } while (done != false);

Try to see if the problem is input or your class.
Remove all user inputs from main and put something yourself in it. If it works, problem is only in input.

Fortunately, it's so simple: ;)

cin.ignore('\n'); // skip 10 chars: '\n' == 10

The 1st ignore() member function argument: The number of elements to skip from the current read position. That's why you have "or the engine" wreck only.
Change to

cin.ignore(1,'\n');

or study the Narue's Novel About cin Flushing (see Ancient Dragon's post)...

Briefly, here's one way to solve it from Narue's post

#include <ios>
#include <istream>
#include <limits>

void ignore_line ( std::istream& in )
{
  in.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
}

With that, all you have to do is call the function ignore_line(cin);

Fortunately, it's so simple: ;)

cin.ignore('\n'); // skip 10 chars: '\n' == 10

The 1st ignore() member function argument: The number of elements to skip from the current read position. That's why you have "or the engine" wreck only.
Change to

cin.ignore(1,'\n');

or study the Narue's Novel About cin Flushing (see Ancient Dragon's post)...

Thats exactly what I needed, thank you so much. So simple, all I needed was "1" lol. Thanks for all the help guys!

So if I use getline it still skips over it. Also I only included the setPartDescription function definition to shorten the code. I hope this is enough information. I used the cin.ignore as well and if I type something like "This part is for the engine." the output in the program is "or the engine."

Header File:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Invoice
{
public:

          Invoice();

   void   setInvoiceNum(int invceNum);
   void   setQtyShipped(int qtyShip);
   void   setPartNum(int partNum);
   void   setPartDecription(string partDescrip);
   void   setUnitPrice(float untPrice);
   void   setSalesTaxRate(float salesTaxRte);
   void   setShippingCost(float shipCost);

   int    getInvoiceNum()     const {return invoiceNumber;}
   int    getQtyShipped()     const {return qtyShipped;}
   int    getPartNum()        const {return partNumber;}
   string getPartDecription()       {return partDescription;}
   float  getUnitPrice()      const {return unitPrice;}
   float  getSalesTaxRate()   const {return salesTaxRate;}
   float  getShippingCost()   const {return shippingCost;}

   float  calcSalesTaxAmnt();
   float  calcTotalCost();
   void   printInvoiceData();

private:
   int    invoiceNumber;
   int    qtyShipped;
   int    partNumber;
   string partDescription;
   float  unitPrice;
   float  salesTaxRate;
   float  salesTaxAmnt;
   float  shippingCost;
   float  totalCost;

};

void Invoice::setPartDecription(string partDescrip)
{
   partDescription = partDescrip;
}

Main Fuction:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

#include "invoiceheader.h"

#define MAX_INVOICES 10000
#define EXIT_CODE    0

int main()
{
   Invoice *Invoices = new Invoice[MAX_INVOICES];
   int     invoiceNum;
   int     qtyShipped;
   int     partNum;
   string  partDescrip;
   float   unitPrice;
   string  editChoice;
   bool    done = true;


   do
   {
      for(int i=0; i<MAX_INVOICES; i++)
      {
         cout << "\nEnter an Invoice Number (or " << EXIT_CODE << " to exit):";
         cin  >> invoiceNum;
         if(invoiceNum == EXIT_CODE)
         {
            done = false;
            break;
         }
         else
         {
            Invoices[i].setInvoiceNum(invoiceNum);
            cout << Invoices[i].getInvoiceNum();  // debug
            cout << "\nEnter Quantity Shipped: ";
            cin  >> qtyShipped;
            Invoices[i].setQtyShipped(qtyShipped);
            cout << Invoices[i].getQtyShipped();  // debug
            cout << "\nEnter Part Number: ";
            cin  >> partNum;
            Invoices[i].setPartNum(partNum);
            cout << Invoices[i].getPartNum();  // debug
            cout << "\nEnter Part Description: ";
            cin.ignore('\n');
            getline(cin, partDescrip);
            Invoices[i].setPartDecription(partDescrip);
            cout << Invoices[i].getPartDecription(); // debug, problem area
            cout << "\nEnter Unit Price: ";
            cin  >> unitPrice;
            Invoices[i].setUnitPrice(unitPrice);
         }
      }

   } while (done != false);

I thing that is confusing about the code is that you are using 2 different variables with the same name;

in the class you have a member variable "string partDescrop"
you also have a local variable "string partDescrip"
use a different name for the member and the local so that it is unambiguous which one is being used where.

Thanx...
Sean

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.