Can someone help me create a function to print all items with a Prod_Stockcount less than 5? Its missing some code but all you need is to add and then create a function to print items with stockcount less than 5

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <fstream>
#include <stdlib.h>
# include <cstring>


using namespace std;

// Declare Global Variable

struct node {
        int Prod_Id ;
        char Prod_Name [20];
        char Prod_Category [10];
        char Prod_Supplier [20];
        float Prod_WholeSalePrice  ;   
        float Prod_RetailSalePrice ;
        float Prod_CostPrice;
        int Prod_stockcount;
        int order;
        node *next;// Pointer to next node
} product;



node *start_ptr = NULL;
node *temp1, *temp2;
node *current;

int rec_num ;


//###########################################
void addproduct () {
     
     string tempValue;
     bool ok;
     ofstream PRODUCT ("PRODUCT.txt", ios::app|ios::out);
         
     

                                  //data to be added to file
                                  //record number
         temp1 = new node;
         cout <<"\nAdd Product \n";
         rec_num++; //add one to customer count
         cout << "\n##### New Product #####";                      
         cout << "\nRecord Number: " << rec_num;
         //validation to check field length
         	          
         ok=false;
         do{
           cout << "\nEnter Product ID: ";
           cin >> tempValue;
           temp1->Prod_Id = atoi(tempValue.c_str());
           if (temp1->Prod_Id = atoi(tempValue.c_str())){
              ok=true;}
           else{
                ok=false;
                cout << "Invalid Numeric Value!";
           }
         }while(ok!=true);
         PRODUCT << temp1->Prod_Id << endl;
    
//************** 
         //validation to check field length
 	            ok = false;
                do{
                                    
                       cout <<"\nEnter Product Name: ";
		               cin >> tempValue;
         //input
		 //checks to see if length of first name was exceeded
		 
		 
         if(tempValue.length()>20){
			 ok=false;
			 cout <<"\nProduct Name Too Long!";
		 }else{
			 ok=true;
		 }
	   }while(ok!=true);
	   strcpy(temp1->Prod_Name, tempValue.c_str());
	   PRODUCT << temp1->Prod_Name << endl;
//************** 

         //validation to check field length
 	           ok = false;
               do{
                                    
                       cout <<"\nEnter Product Category: ";
		               cin >> tempValue;
         //input
		 //checks to see if length of first name was exceeded
		 
		 
         if(tempValue.length()>10){
			 ok=false;
			 cout <<"\nProduct Category Name Too Long!";
		 }else{
			 ok=true;
		 }
	   }while(ok!=true);
	   strcpy(temp1->Prod_Category, tempValue.c_str());
	   PRODUCT << temp1->Prod_Category<< endl;
//************** 
         //validation to check field length
 	           ok = false;
               do{
                                     
                       cout <<"\nEnter Supplier Name: ";
		               cin >> tempValue;
         
		 
		 
         if(tempValue.length()>20){
			 ok=false;
			 cout <<"\nProduct Supplier Name is  Too Long!";
		 }else{
			 ok=true;
		 }
	   }while(ok!=true);
	   strcpy(temp1->Prod_Supplier, tempValue.c_str());
	  PRODUCT << temp1->Prod_Supplier<< endl;

    
//************** 

//validation for numeric fields
         ok=false;
         do{
           cout << "\nEnter Whole Sale Price: ";
           cin >> tempValue;
           temp1->Prod_WholeSalePrice = atof(tempValue.c_str());
           if (temp1->Prod_WholeSalePrice = atof(tempValue.c_str())){
              ok=true;}
           else{
                ok=false;
                cout << "Invalid Numeric Value!";
           }
         }while(ok!=true);
         PRODUCT << temp1->Prod_WholeSalePrice<< endl;
//************** 

//validation for numeric fields
         ok=false;
         do{
           cout << "\nEnter Retail Sale Price: ";
           cin >> tempValue;
           temp1->Prod_RetailSalePrice = atof(tempValue.c_str());
           if (temp1->Prod_RetailSalePrice = atof(tempValue.c_str())){
              ok=true;}
           else{
                ok=false;
                cout << "Invalid Numeric Value!";
           }
         }while(ok!=true);
         PRODUCT << temp1->Prod_RetailSalePrice<< endl;
         
//************** 

//validation for numeric fields
         ok=false;
         do{
           cout << "\nEnter Number of products in stock: ";
           cin >> tempValue;
           temp1->Prod_stockcount = atoi(tempValue.c_str());
           if (temp1->Prod_stockcount = atoi(tempValue.c_str())){
              ok=true;}
           else{
                ok=false;
                cout << "Invalid Numeric Value!";
           }
         }while(ok!=true);
         PRODUCT << temp1->Prod_stockcount<< endl;         
         
//************** 
//validation for numeric fields
         ok=false;
         do{
           cout << "\nEnter OutStanding Order: ";
           cin >> tempValue;
           temp1->order = atoi(tempValue.c_str());
           if (temp1->order = atoi(tempValue.c_str())){
              ok=true;}
           else{
                ok=false;
                cout << "Invalid Numeric Value!";
           }
         }while(ok!=true);
         PRODUCT << temp1->order<< endl;         
         
//************** 
//validation for numeric fields
         ok=false;
         do{
           cout << "\nEnter Cost Price: ";
           cin>>tempValue;
           temp1->Prod_CostPrice = atof(tempValue.c_str());
           if (temp1->Prod_CostPrice = atof(tempValue.c_str())){
              ok=true;}
           else{
                ok=false;
                cout << "Invalid Numeric Value!";
           }
         }while(ok!=true);
         PRODUCT << temp1->Prod_CostPrice<< endl;         
//**************   



     temp1->next = NULL;

   //set up link to this node
   if (start_ptr == NULL)
      start_ptr = temp1;
   else { 
        temp2 = start_ptr;
         //we know this is not NULL - list not empty!
         while (temp2->next != NULL){  
               temp2 = temp2->next;//move to next link in chain    
         }
         temp2->next = temp1;
   }
    


}//end addpRODUCT
//*************************************************************
void read (){
     
}//end

//*************************************************
//**************  
void findProduct() {
     
node *current;
int target;
bool found;

     cout << "\n**********************Find Product******************** "; 
     cout << "\nEnter Product Id: ";
     cin >> target;
     found = false;
     current = start_ptr;
     if (current == NULL)
          cout << "The list is empty!" << endl;
     else { 
          while (current != NULL && found==false) {  
                if (target == current->Prod_Id){ //display customer
                   found = true;
                   cout << "\nProduct ID: " << current->Prod_Id;
                   cout << "\nProduct Name: " << current->Prod_Name;
                   cout << "\nProduct Category: " << current->Prod_Category;
                   cout << "\nProduct's Supplier: " << current->Prod_Supplier;
                   cout << "\nWhole Sale Price: " << current->Prod_WholeSalePrice;
                   cout << "\nRetail Price: " << current->Prod_RetailSalePrice;
                   cout << "\nCost Price: " << current->Prod_CostPrice;
                   cout << "\nNumber In Stock: " << current->Prod_stockcount;
                   cout << "\nOutstanding Order: " << current->order;
                   cout << endl;
                }
	            current = current->next;
          }
	      if (found==false) cout << "\nNo Matching Customer Found!" << endl;
     }

}//end of findCustomer

//******************
void viewAll() {
  
     
     cout << "\n##### View ALL #####"; 
     
      
          temp1 = start_ptr;
          while (temp1 != NULL) {  // Display details for what temp points to
                   cout << "\nProduct ID: " << temp1->Prod_Id;
                   cout << "\nProduct Name: " << temp1->Prod_Name;
                   cout << "\nProduct Category: " << temp1->Prod_Category;
                   cout << "\nProduct's Supplier: " << temp1->Prod_Supplier;
                   cout << "\nWhole Sale Price: " << temp1->Prod_WholeSalePrice;
                   cout << "\nRetail Price: " << temp1->Prod_RetailSalePrice;
                   cout << "\nCost Price: " << temp1->Prod_CostPrice;
                   cout << "\nNumber In Stock: " << temp1->Prod_stockcount;
                   cout << "\nOutstanding Order: " << temp1->order;
                   cout << endl;
                   
                   
             temp1 = temp1 -> next;
             
          
	      
     }
cout << "\nEnd of list!" << endl;
cout << "\nIF LIST IS EMPTY, PLEASE INITIALIZE FILE!" << endl;
}//end of viewAll

//############################################
void deleteProduct() {
     

    
}//end of deleteProduct
 
//############################################
int main (void){
    //Declare local variables
    
    char choice;
   
   
    do {
      cout << "******MAIN MENU******\n";
      cout << "Choose One of the Following:\n";
      cout << "(1) Add Product\n";
      cout << "(2) Find Product\n";
      cout << "(3) View All Records\n";
      cout << "(4) Delete Product\n";
      cout << "(5) Initialize Files\n";
      cout << "(6) Exit\n";
      
      cin >> (choice);
      switch (choice) {
                        
                 case '1':
                          addproduct ();
                          break;
                 case '2':
                          findProduct ();
                          break;
                 case '3':
                          viewAll();
                          break;
                 case '4':
                          deleteProduct();
                          break;
                 case '5':
                          read ();
                          break;
                 case '6':
                          exit (0);
                          break;
      }//end switch
    } while(choice != '6');
   return 0;
}//end of main

Edited 6 Years Ago by cyberguy007: n/a

It'll be very similar to your viewAll() method. Just test for Prod_stockcount before displaying.

Just a while loop with an if statement inside

while(not equal to null)
{
           if  (temp->Prod_stockcount < 2)
             {
                   //output, etc.

              }
}

Edited 6 Years Ago by jonsca: n/a

I try this already that why i started this thread because i think is the pointers messing up

void shortage() {
          
          temp1 = start_ptr;
          while (temp1 != NULL)   {  // Display details for what temp points to
          
          if ( temp1->Prod_stockcount < 5 ) {
     
          cout << "\n##### List #####"; 
                   
                     
                   cout << "\nProduct ID: " << temp1->Prod_Id;
                   cout << "\nProduct Name: " << temp1->Prod_Name;
                   cout << "\nProduct Category: " << temp1->Prod_Category;
                   cout << "\nProduct's Supplier: " << temp1->Prod_Supplier;
                   cout << "\nWhole Sale Price: " << temp1->Prod_WholeSalePrice;
                   cout << "\nRetail Price: " << temp1->Prod_RetailSalePrice;
                   cout << "\nCost Price: " << temp1->Prod_CostPrice;
                   cout << "\nNumber In Stock: " << temp1->Prod_stockcount;
                   cout << "\nOutstanding Order: " << temp1->order;
                   cout << endl;
                   
                   
                   temp1 = temp1 -> next;
             
          
	      
     }}
cout << "\nEnd of list!" << endl;
}//end of shortage

I put shortage into the code (right before main()) and added a 7th menu item to call that function and it seems to be working as expected. How many entries did you put into the system?

my program just seem to crashes,when shortage is initialize , i did all you did....

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <fstream>
#include <stdlib.h>
# include <cstring>


using namespace std;

// Declare Global Variable

struct node {
        int Prod_Id ;
        char Prod_Name [20];
        char Prod_Category [10];
        char Prod_Supplier [20];
        float Prod_WholeSalePrice  ;   
        float Prod_RetailSalePrice ;
        float Prod_CostPrice;
        int Prod_stockcount;
        int order;
        node *next;// Pointer to next node
} product;



node *start_ptr = NULL;
node *temp1, *temp2;
node *current;

int rec_num ;


//###########################################
void addproduct () {
     
     string tempValue;
     bool ok;
     ofstream PRODUCT ("PRODUCT.txt", ios::app|ios::out);
         
     

                                  //data to be added to file
                                  //record number
         temp1 = new node;
         cout <<"\nAdd Product \n";
         rec_num++; //add one to customer count
         cout << "\n##### New Product #####";                      
         cout << "\nRecord Number: " << rec_num;
         //validation to check field length
         	          
         ok=false;
         do{
           cout << "\nEnter Product ID: ";
           cin >> tempValue;
           temp1->Prod_Id = atoi(tempValue.c_str());
           if (temp1->Prod_Id = atoi(tempValue.c_str())){
              ok=true;}
           else{
                ok=false;
                cout << "Invalid Numeric Value!";
           }
         }while(ok!=true);
         PRODUCT << temp1->Prod_Id << endl;
    
//************** 
         //validation to check field length
 	            ok = false;
                do{
                                    
                       cout <<"\nEnter Product Name: ";
		               cin >> tempValue;
         //input
		 //checks to see if length of first name was exceeded
		 
		 
         if(tempValue.length()>20){
			 ok=false;
			 cout <<"\nProduct Name Too Long!";
		 }else{
			 ok=true;
		 }
	   }while(ok!=true);
	   strcpy(temp1->Prod_Name, tempValue.c_str());
	   PRODUCT << temp1->Prod_Name << endl;
//************** 

         //validation to check field length
 	           ok = false;
               do{
                                    
                       cout <<"\nEnter Product Category: ";
		               cin >> tempValue;
         //input
		 //checks to see if length of first name was exceeded
		 
		 
         if(tempValue.length()>10){
			 ok=false;
			 cout <<"\nProduct Category Name Too Long!";
		 }else{
			 ok=true;
		 }
	   }while(ok!=true);
	   strcpy(temp1->Prod_Category, tempValue.c_str());
	   PRODUCT << temp1->Prod_Category<< endl;
//************** 
         //validation to check field length
 	           ok = false;
               do{
                                     
                       cout <<"\nEnter Supplier Name: ";
		               cin >> tempValue;
         
		 
		 
         if(tempValue.length()>20){
			 ok=false;
			 cout <<"\nProduct Supplier Name is  Too Long!";
		 }else{
			 ok=true;
		 }
	   }while(ok!=true);
	   strcpy(temp1->Prod_Supplier, tempValue.c_str());
	  PRODUCT << temp1->Prod_Supplier<< endl;

    
//************** 

//validation for numeric fields
         ok=false;
         do{
           cout << "\nEnter Whole Sale Price: ";
           cin >> tempValue;
           temp1->Prod_WholeSalePrice = atof(tempValue.c_str());
           if (temp1->Prod_WholeSalePrice = atof(tempValue.c_str())){
              ok=true;}
           else{
                ok=false;
                cout << "Invalid Numeric Value!";
           }
         }while(ok!=true);
         PRODUCT << temp1->Prod_WholeSalePrice<< endl;
//************** 

//validation for numeric fields
         ok=false;
         do{
           cout << "\nEnter Retail Sale Price: ";
           cin >> tempValue;
           temp1->Prod_RetailSalePrice = atof(tempValue.c_str());
           if (temp1->Prod_RetailSalePrice = atof(tempValue.c_str())){
              ok=true;}
           else{
                ok=false;
                cout << "Invalid Numeric Value!";
           }
         }while(ok!=true);
         PRODUCT << temp1->Prod_RetailSalePrice<< endl;
         
//************** 

//validation for numeric fields
         ok=false;
         do{
           cout << "\nEnter Number of products in stock: ";
           cin >> tempValue;
           temp1->Prod_stockcount = atoi(tempValue.c_str());
           if (temp1->Prod_stockcount = atoi(tempValue.c_str())){
              ok=true;}
           else{
                ok=false;
                cout << "Invalid Numeric Value!";
           }
         }while(ok!=true);
         PRODUCT << temp1->Prod_stockcount<< endl;         
         
//************** 
//validation for numeric fields
         ok=false;
         do{
           cout << "\nEnter OutStanding Order: ";
           cin >> tempValue;
           temp1->order = atoi(tempValue.c_str());
           if (temp1->order = atoi(tempValue.c_str())){
              ok=true;}
           else{
                ok=false;
                cout << "Invalid Numeric Value!";
           }
         }while(ok!=true);
         PRODUCT << temp1->order<< endl;         
         
//************** 
//validation for numeric fields
         ok=false;
         do{
           cout << "\nEnter Cost Price: ";
           cin>>tempValue;
           temp1->Prod_CostPrice = atof(tempValue.c_str());
           if (temp1->Prod_CostPrice = atof(tempValue.c_str())){
              ok=true;}
           else{
                ok=false;
                cout << "Invalid Numeric Value!";
           }
         }while(ok!=true);
         PRODUCT << temp1->Prod_CostPrice<< endl;         
//**************   



     temp1->next = NULL;

   //set up link to this node
   if (start_ptr == NULL)
      start_ptr = temp1;
   else { 
        temp2 = start_ptr;
         //we know this is not NULL - list not empty!
         while (temp2->next != NULL){  
               temp2 = temp2->next;//move to next link in chain    
         }
         temp2->next = temp1;
   }
    


}//end addpRODUCT
//*************************************************************
void read (){
     
     ifstream PRODUCT ("PRODUCT.txt");
     if (PRODUCT.is_open()){
                            

    PRODUCT.seekg(0,ios::end);
    size_t size = PRODUCT.tellg();
    if( size == 0)
    {
         cout << "File is empty\n";
         
    }
    

                          else {
           
                          PRODUCT.seekg(0,ios::beg);
                          cout <<"\nLoading Files.....\n";
                          
                          do{
                          rec_num++; //add one to customer count     
                          temp1 = new node;
                          PRODUCT >> temp1->Prod_Id;
                          PRODUCT >> temp1->Prod_Name;
                          PRODUCT >> temp1->Prod_Category;
                          PRODUCT >> temp1->Prod_Supplier;
                          PRODUCT >> temp1->Prod_WholeSalePrice;
                          PRODUCT >> temp1->Prod_RetailSalePrice;
                          PRODUCT >> temp1->Prod_stockcount;
                          PRODUCT >> temp1->order;
                          PRODUCT >> temp1->Prod_CostPrice;
                          
                          temp1->next = NULL;
              
          
                          //set up link to this node
                          if (start_ptr == NULL)
                          start_ptr = temp1;
                          else { 
                          temp2 = start_ptr;
                          //we know this is not NULL - list not empty!
                          while (temp2->next != NULL){  
                          temp2 = temp2->next;//move to next link in chain    
                          }
                          
             temp2->next = temp1;
             }
             
}while (!PRODUCT.eof() );
}
} 
 else cout << "\nUnable to open file.\n";
 PRODUCT.close();
{ 
      
     if (start_ptr == NULL)
          cout << "\nThe list is empty!\n" << endl;
     else
        { temp1 = start_ptr;
          while (temp1->next != NULL)
             { temp2 = temp1;
               temp1 = temp1->next;
             }
          delete temp1;
          temp2->next = NULL;
        rec_num--;}
   }
cout << "\n*****DON'T INTIALIZE FILE MORE THAN ONCE******\n";
}//end

//*************************************************
//**************  
void findProduct() {
     
node *current;
int target;
bool found;

     cout << "\n**********************Find Product******************** "; 
     cout << "\nEnter Product Id: ";
     cin >> target;
     found = false;
     current = start_ptr;
     if (current == NULL)
          cout << "The list is empty!" << endl;
     else { 
          while (current != NULL && found==false) {  
                if (target == current->Prod_Id){ //display customer
                   found = true;
                   cout << "\nProduct ID: " << current->Prod_Id;
                   cout << "\nProduct Name: " << current->Prod_Name;
                   cout << "\nProduct Category: " << current->Prod_Category;
                   cout << "\nProduct's Supplier: " << current->Prod_Supplier;
                   cout << "\nWhole Sale Price: " << current->Prod_WholeSalePrice;
                   cout << "\nRetail Price: " << current->Prod_RetailSalePrice;
                   cout << "\nCost Price: " << current->Prod_CostPrice;
                   cout << "\nNumber In Stock: " << current->Prod_stockcount;
                   cout << "\nOutstanding Order: " << current->order;
                   cout << endl;
                }
	            current = current->next;
          }
	      if (found==false) cout << "\nNo Matching Customer Found!" << endl;
     }

}//end of findCustomer

//******************
void viewAll() {
  
     
     cout << "\n##### View ALL #####"; 
     
      
          temp1 = start_ptr;
          while (temp1 != NULL) {  // Display details for what temp points to
                   cout << "\nProduct ID: " << temp1->Prod_Id;
                   cout << "\nProduct Name: " << temp1->Prod_Name;
                   cout << "\nProduct Category: " << temp1->Prod_Category;
                   cout << "\nProduct's Supplier: " << temp1->Prod_Supplier;
                   cout << "\nWhole Sale Price: " << temp1->Prod_WholeSalePrice;
                   cout << "\nRetail Price: " << temp1->Prod_RetailSalePrice;
                   cout << "\nCost Price: " << temp1->Prod_CostPrice;
                   cout << "\nNumber In Stock: " << temp1->Prod_stockcount;
                   cout << "\nOutstanding Order: " << temp1->order;
                   cout << endl;
                   
                   
             temp1 = temp1 -> next;
             
          
	      
     }
cout << "\nEnd of list!" << endl;
cout << "\nIF LIST IS EMPTY, PLEASE INITIALIZE FILE!" << endl;
}//end of viewAll

//******************
void shortage() {
          
          temp1 = start_ptr;
          while (temp1 != NULL)   {  // Display details for what temp points to
                             
          if ( (temp1->Prod_stockcount) < 5 ) {
     
          cout << "\n##### List #####"; 
                   
                     
                   cout << "\nProduct ID: " << temp1->Prod_Id;
                   cout << "\nProduct Name: " << temp1->Prod_Name;
                   cout << "\nProduct Category: " << temp1->Prod_Category;
                   cout << "\nProduct's Supplier: " << temp1->Prod_Supplier;
                   cout << "\nWhole Sale Price: " << temp1->Prod_WholeSalePrice;
                   cout << "\nRetail Price: " << temp1->Prod_RetailSalePrice;
                   cout << "\nCost Price: " << temp1->Prod_CostPrice;
                   cout << "\nNumber In Stock: " << temp1->Prod_stockcount;
                   cout << "\nOutstanding Order: " << temp1->order;
                   cout << endl;
                   
                   
                   temp1 = temp1 -> next;
             
          
	      
     }}
cout << "\nEnd of list!" << endl;
}//end of shortage
//############################################
void deleteProduct() {
     
node *currentP;
node *prevP;

int target;
bool found;

     cout << "\n##### Delete Customer #####"; 
     cout << "\nEnter Product ID: ";
     cin >> target;
     
     found = false;
     prevP = NULL;          //there is no previous pointer for the first node
     currentP = start_ptr;  //point to first item in list

     //visit each node, maintaining a pointer to
     //the previous node we just visited.
     while (currentP != NULL && found==false) {                        //for (currP = link; currP != NULL; prevP = currP, currP = currP->getNext()) {
           if (target == currentP->Prod_Id){  
             found=true;                         // if (currP->getData() == item) {  /* Found it. */
             if (prevP == NULL) { //item is first item in list
                       start_ptr = currentP->next;                   //link = currP->getNext();/* Fix beginning pointer. */
             } else {
	                   prevP->next = currentP->next;
             }
             delete currentP;  /* Deallocate the node. */
             cout << "\nEntry deleted!" << endl;
             rec_num--;
           }
           prevP = currentP;
           currentP = currentP->next;
           
     }//end while
   
   ofstream PRODUCT ("PRODUCT.txt", ios::out|ios ::trunc);
   temp1 = start_ptr;
   while (temp1 != NULL) {
   
   PRODUCT << temp1->Prod_Id << endl;
   PRODUCT << temp1->Prod_Name << endl;
   PRODUCT << temp1->Prod_Category<< endl;
   PRODUCT << temp1->Prod_Supplier<< endl;
   PRODUCT << temp1->Prod_WholeSalePrice<< endl;
   PRODUCT << temp1->Prod_RetailSalePrice<< endl;
   PRODUCT << temp1->Prod_stockcount<< endl; 
   PRODUCT << temp1->order<< endl; 
   PRODUCT << temp1->Prod_CostPrice<< endl;         
   temp1 = temp1 -> next;
             
          
	      
     }
   
   if (found==false) cout << "\nNo Matching Customer Found!" << endl;
    
}//end of deleteProduct
 
//############################################
int main (void){
    //Declare local variables
    
    char choice;
   
   
    do {
      cout << "******MAIN MENU******\n";
      cout << "Choose One of the Following:\n";
      cout << "(1) Add Product\n";
      cout << "(2) Find Product\n";
      cout << "(3) View All Records\n";
      cout << "(4) Stock Shortage\n";
      cout << "(5) Delete Product\n";
      cout << "(6) Initialize Files\n";
      cout << "(7) Exit\n";
      
      cin >> (choice);
      switch (choice) {
                        
                 case '1':
                          addproduct ();
                          break;
                 case '2':
                          findProduct ();
                          break;
                 case '3':
                          viewAll();
                          break;
                 case '4':
                          shortage();
                          break;               
                 case '5':
                          deleteProduct();
                          break;
                 case '6':
                          read ();
                          break;
                 case '7':
                          exit (0);
                          break;
      }//end switch
    } while(choice != '7');
   return 0;
}//end of main

Output when I ran shortages (I just added the items and went right to shortages

******MAIN MENU******
Choose One of the Following:
(1) Add Product
(2) Find Product
(3) View All Records
(4) Delete Product
(5) Initialize Files
(6) Exit
(7) Shortages
7

##### List #####
Product ID: 124
Product Name: Lettuce
Product Category: Produce
Product's Supplier: Farm
Whole Sale Price: 0.25
Retail Price: 1
Cost Price: 2
Number In Stock: 1
Outstanding Order: 1

##### List #####
Product ID: 125
Product Name: Tomatoes
Product Category: Produce
Product's Supplier: Farm
Whole Sale Price: 0.2
Retail Price: 1
Cost Price: 2
Number In Stock: 1
Outstanding Order: 1

End of list!

what was the exact input that you used? Did you go to one of the other menuitems first?

I initialized the file or read from file to load saved data....run my code. When files are initialize/read and then shortage chosen it freezes...

Output when I ran shortages (I just added the items and went right to shortages

******MAIN MENU******
Choose One of the Following:
(1) Add Product
(2) Find Product
(3) View All Records
(4) Delete Product
(5) Initialize Files
(6) Exit
(7) Shortages
7

##### List #####
Product ID: 124
Product Name: Lettuce
Product Category: Produce
Product's Supplier: Farm
Whole Sale Price: 0.25
Retail Price: 1
Cost Price: 2
Number In Stock: 1
Outstanding Order: 1

##### List #####
Product ID: 125
Product Name: Tomatoes
Product Category: Produce
Product's Supplier: Farm
Whole Sale Price: 0.2
Retail Price: 1
Cost Price: 2
Number In Stock: 1
Outstanding Order: 1

End of list!

what was the exact input that you used? Did you go to one of the other menuitems first?

PRODUCT >> temp1->Prod_Id;
//This works
PRODUCT >> temp1->Prod_Name;
//This will not
PRODUCT >> temp1->Prod_Category;
//This will not
PRODUCT >> temp1->Prod_Supplier;
//etc.

I hadn't noticed it because you changed read() and I still had the old version. So there's an "easy"* solution to the above problem and that's to make your chat [] variables strings.
(* but would require other changes in your other methods, and you wouldn't have to use strcpy)
However, you probably chose char [] for a reason so you must use PRODUCT.getline(chararrayname,capacityofchararray); but mixing up the getline and the PRODUCT >> calls can create problems (probably would need a cin.ignore() after each getline() to ditch the newline left in the buffer).
You also don't want to use !=EOF in your loop. It may read an extra line in goofing up your input. Instead use a while (PRODUCT >> temp1->Prod_Id) with your other PRODUCT >> calls inside the loop to follow along at each iteration. When the loop ran out of ProdIds in the file it would collect the remaining variables and stop. This EOF change would apply regardless of whether or not your do the getline or >> methods.
Phew, so that's a lot. Honestly, I think you should use all PRODUCT >> or all cin.getline()s (with the getline you would need to parse the integers and floats from the strings you read in), and probably change your char[] to strings.

Edited 6 Years Ago by jonsca: n/a

I honestly don't understand what you're saying.

The program is meant to be one which helps manage a supermarket and store info about stock.

The problem is i added data then exit program. I initialized files(read from file) then view all. All is well till i select the function shortage which is suppose to display all items with stock count less than 5.The program just freezes from that point.

Edited 6 Years Ago by cyberguy007: n/a

I honestly don't understand what you're saying.

Just as you cannot say char s[80]; cin >> s; you cannot declare ifstream inf("mytxt.txt"); inf >> s; either. Best case it'll force something into the first element of the array and throw the rest of the characters back into the stream and worst case you'll overrun the size of the array and clobber the next n bytes of the memory.

The problem is i added data then exit program. I initialized files(read from file) then view all. All is well till i select the function shortage which is suppose to display all items with stock count less than 5.The program just freezes from that point.

I understand this, thank you. If the above case with trying to >> into a char array happens and you've corrupted the memory holding items in your linked list then it's certainly not going to be able to step through the list properly! We've both shown that if you create the list by adding items it works just fine, the list is not corrupted because you use the strcpy to get the results properly into the char array.

Again, I recommend investing some time and dumping the char arrays in favor of strings. It will make life easier, you won't have to worry about your file input (as much) and it will work. Howeve, I have also given you a method (using the getline() method of your ifstream object (see here for an excellent reference) and using the cin.ignore() (see here).

Edited 6 Years Ago by jonsca: n/a

This article has been dead for over six months. Start a new discussion instead.