This program should search for an invoice, display all its current information, and allow the user to change and store each of the values originally entered. To exit “edit mode” (and the program) the user can enter an Invoice Number of 0. My problem is I can't properly search through all the elements. I've tested it throughly and all my set and get functions are working properly, its my loop thats the problem. I've walked through this in my head, and verbally but I can't see whats wrong. I think my pride is getting in the way so I need help. Output given at the bottom.

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

#include "invoiceheader.h"

#define MAX_INVOICES 10000 
#define EXIT_CODE    0    
#define SKIP_CODE    1 
int main()
{
   Invoice *Invoices = new Invoice[MAX_INVOICES];
   int     invoiceNum;   
   int     qtyShipped;   
   int     partNum;      
   string  partDescrip; 
   float   unitPrice;   
   string  editChoice;   
   bool    done = true; 
   bool    found = true;

do
   {
      for(int i=0; i<MAX_INVOICES; i++)
      {
      cout << "\nType an Invoice number you would like to search for"
           << " or (" << EXIT_CODE << " to exit): ";
      cin  >> invoiceNum;

      // Check for the exit code
      if(invoiceNum == EXIT_CODE)
      {
         done = false;
         break;
      }
      else
      {
        
            if(invoiceNum == Invoices[i].getInvoiceNum())
            {
               cout << "\nDoes anything need editing? (y or n): ";
               cin  >> editChoice;
               if(editChoice == "y")
               {
                  cout << "\nCurrent contents of invoice " << invoiceNum
                       << ":";
                  Invoices[i].printInvoiceData();
                  cout << "\n\nEnter new quantity shipped (" << SKIP_CODE 
                       << " to skip): ";
                  cin  >> qtyShipped;
                  if(qtyShipped != SKIP_CODE)
                     Invoices[i].setQtyShipped(qtyShipped);
                  cout << "\nEnter new part number (" << SKIP_CODE 
                       << " to skip): ";
                  cin  >> partNum;
                  if(partNum != SKIP_CODE)
                     Invoices[i].setPartNum(partNum);
                  /*cout << "\nEnter new part description (" << SKIP_CODE 
                       << " to skip): ";
                  cin.ignore(1,'\n');
                  getline(cin, partDescrip);
                  if(partDescrip != SKIP_CODE)
                     Invoices[i].setPartDecription(partDescrip);*/// Stuck
                  cout << "\nEnter new unit price (" << SKIP_CODE 
                       << " to skip): ";
                  cin  >> unitPrice;
                  if(unitPrice != SKIP_CODE)
                     Invoices[i].setUnitPrice(unitPrice);
                  cout << "\nHere is the new invoice: ";
                     Invoices[i].printInvoiceData();
               }
               
            }
            else
            {
               cout << "\nSorry, no Invoices of " << invoiceNum 
                    << " found.";
               found = false;
               break;
            }
          
         }
      }

   } while(done != false);

Output without editing:

Enter an Invoice Number (or 0 to exit):12

Enter Quantity Shipped: 32

Enter Part Number: 45

Enter Part Description: This is for the engine.

Enter Unit Price: 23.76

Enter an Invoice Number (or 0 to exit):2

Enter Quantity Shipped: 45

Enter Part Number: 2

Enter Part Description: This is for the transmission.

Enter Unit Price: 45.34

Enter an Invoice Number (or 0 to exit):0

Type an Invoice number you would like to search for or (0 to exit): 2

Sorry, no Invoices of 2 found.
Type an Invoice number you would like to search for or (0 to exit): 1

Sorry, no Invoices of 1 found.
Type an Invoice number you would like to search for or (0 to exit): 12

Does anything need editing? (y or n): n

Type an Invoice number you would like to search for or (0 to exit): 12

Sorry, no Invoices of 12 found.
Type an Invoice number you would like to search for or (0 to exit): 0

Press any key and <enter> to exit

Output with editing:

Enter an Invoice Number (or 0 to exit):14

Enter Quantity Shipped: 12

Enter Part Number: 56

Enter Part Description: This is a test.

Enter Unit Price: 45.67

Enter an Invoice Number (or 0 to exit):0

Type an Invoice number you would like to search for or (0 to exit): 14

Does anything need editing? (y or n): y

Current contents of invoice 14:

Invoice Number: 14
Quantity Shipped: 12
Part Number: 56
Part Description: This is a test.
Unit Price: $45.67
Sales Tax Rate: 5%
Sales Tax Amount: 0.27
Shipping Cost: $5.00

Total Cost: $553.31

Enter new quantity shipped (1 to skip): 1

Enter new part number (1 to skip): 24

Enter new unit price (1 to skip): 4.35

Here is the new invoice:

Invoice Number: 14
Quantity Shipped: 12
Part Number: 24
Part Description: This is a test.
Unit Price: $4.35
Sales Tax Rate: 5.00%
Sales Tax Amount: 0.03
Shipping Cost: $5.00

Total Cost: $57.23
Type an Invoice number you would like to search for or (0 to exit): 14

Sorry, no Invoices of 14 found.
Type an Invoice number you would like to search for or (0 to exit): 0

Press any key and <enter> to exit

The only thing I can see that's could be messing it up is the for loop. Try putting it within the else statement so that it goes through the array each time instead of just looking at one spot in the array and then stopping.

I would fix it by doing this. Your code:

if(invoiceNum == Invoices[i].getInvoiceNum())
{ cout << "\nDoes anything need editing? (y or n): ";
..... }

Mine:

for (i=0; i<max; i++)
     { if(invoiceNum == Invoices[i].getInvoiceNum()) 
          { found = true;
             break;}
     }
if (found == true)
   { cout << "\nDoes anything need editing? (y or n): ";
     ..... }

You should be able to keep the rest as is and I believe that will fix your problem.


This might just be me, but it seems like you have a lot of functions that you don't need. Of course, I don't know what they do cause you didn't give us that part, but you could accomplish this with a struct and save yourself a lot of space.

Just as an example, I'm going to use invoice number, part number, and price. My struct would be this:

struct outline
{  int num;
    int partNum;
    float price;
};

Then, to fill it in, each would:

outline Invoice[max_length]
while (val != 0)
{ cout<< "Invoice num: ";
   cin >> val;
   if (val == 0) break;
  
   Invoice[i].num = val;
   cout << "Part num: ";
   cin >> Invoice[i].partNum;
   cout << "Price: ";
   cin >> Invoice[i].price; }

Then when you search for the invoice number, you just do a for loop.

// get invoice number and store in val
for (n...)
{ if (val == Invoice[n].num)
      { found = true; 
        break; }
   if (found == true)
      { // prints, then asks if you want to edit
         cout << "New part num: ";
         cin >> val;
         if (val != SkipCode)
            Invoice[n].partNum = val;
          ....  }
   else cout << "Sorry invoice not found";
}

In the end, I believe it would save you time and space by eliminating your functions and the number of variables you need. Of course, that's if you are allowed to use a struct to do this.

Hope this helped.

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.