Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Why is my function shortage not working?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cyberguy




PostPosted: Fri Jan 08, 2010 7:29 pm   Post subject: Why is my function shortage not working?

code:


# 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                   
Sponsor
Sponsor
Sponsor
sponsor
Barbarrosa




PostPosted: Sat Jan 09, 2010 2:15 am   Post subject: Re: Why is my function shortage not working?

It would be a lot more helpful if you posted your stacktrace, and/or the specific error you're getting. Even the specific line that's showing a problem would help.

You could also give people who intend to help you some context. If you do that, I'll be willing to help you (although I may be delayed a few days).

It's not good practice to post a lone, long piece of code without comments that tell specifically what the entire block is for.
cyberguy




PostPosted: Sat Jan 09, 2010 8:53 am   Post subject: Re: Why is my function shortage not working?

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.
Barbarrosa




PostPosted: Sun Jan 10, 2010 12:46 pm   Post subject: Re: Why is my function shortage not working?

The problem is that you go to the next node only if the supermarket has less than 5 of the current item in stock.
c++:

temp1 = temp1 -> next;

This needs to be outside the "if" statement.

Thanks for explaining a bit more. It can really help to know when and how the program goes wrong.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: