Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, need help on my project, cant get OR operator to work, minor bug,

Status
Not open for further replies.

amino83

Programmer
Nov 10, 2003
8
US

Here's my program. It compiles, runs but there's a bug in it.
It has to do with the function get_action; the problem is in the OR statements :

Brand_number!=('1'))||(Brand_number!=('2'))||(Brand_number!=('3'))||(Brand_number!=('4')))

It just doesn't work. The brand number has to equal 1,2,3, or 4, but whenever i try the above operator it only displays the error messages, even when there is a valid number.

Code:
/*This program prompts the user for inventory values for four different sodas then it  prompts the user for an action,a soda brand name and  an amount.It then carries out the action requested.Main is  divided into three functions namely instr_display,initialize and process_orders*/ 

#include <iostream> 
#include <string> 
using namespace std; 
void initialize(int&,int&,int&,int&); 
void process_orders(int& quantity_c,int& quantity_p,int& quantity_d,int& quantity_h); 
void sell_purchase(int,int&,char); 
void inven_display(int quantity_c,int quantity_p,int quantity_d,int quantity_h); 
void instr_display(); 
void clear_line(); 
int get_amount(); 
char get_action(char& Brand_number,int& amount); 
bool valid_read(string Brand_name,int& quantity); 
const int NEGATIVE_VALUE = -1; 
const char INVALID= 'X';  


     



int main() 
{ 
  int coke=0;             //  quantity of Coke entered by user for inventory                
  int pepsi=0;             //   quantity of Pepsi entered by user for inventory      
  int canada_dry=0;             //   quantity of Canada Dry entered by user for inventory 
  int hires=0;              //   quantity of hires entered by user for inventory 
  
  
    instr_display(); 
    initialize(coke,pepsi,canada_dry,hires); 
   process_orders(coke,pepsi,canada_dry,hires); 
    clear_line(); 
   return 0; 
} 


   /*displays the instruction at the begining of the program and whenever user enter 'I'*/ 

 void  instr_display( ) 
{ 
    cout <<&quot;You will first be prompted for the inventories for each of the four brands.&quot;<<endl;  
   cout <<&quot;You should then enter the initial number of cases available for each brand.&quot;<<endl; 
   cout <<&quot;All other commands should follow one of these formats:&quot;<< endl; 
   cout << &quot; &quot;<<&quot;(P)urchase + <brand number> + <amount>.&quot;<< endl; 
   cout << &quot; &quot;<<&quot;(S)ell     + <brand number> + <amount>.&quot;<< endl; 
   cout << &quot; &quot;<<&quot;(D)isplay present inventory for the four brands.&quot;<< endl; 
   cout << &quot; &quot;<<&quot;Show(I)structions for user input.&quot;<< endl; 
   cout << &quot; &quot;<<&quot;(Q)uit.&quot;<< endl; 
   cout <<&quot;Brand Number codes:&quot;<< endl; 
    cout <<&quot; &quot;<<&quot;1--Coke  2--Pepsi 3--Canada Dry  4-- Hires &quot;<< endl; 
    
} 

  
 /*this function initializes the inventory with different quantities of soda*/ 

  void initialize(int& quantity_c,int& quantity_p,int& quantity_d ,int& quantity_h) 
{        
         
  
    
    while (!(valid_read(&quot;Coke&quot;,quantity_c))) 
   { 
      cin.clear(); 
      cin.ignore (80,'\n'); 
      cout<<&quot; &quot;<<&quot;Invalid input ...... please re-enter. &quot;<<endl; 
   } 
    
    while (!(valid_read(&quot;Pepsi&quot;,quantity_p))) 
   { 
      cin.clear(); 
      cin.ignore (80,'\n'); 
      cout<<&quot; &quot;<<&quot;Invalid input ...... please re-enter. &quot;<<endl; 
   } 
    
    while (!(valid_read(&quot;Canada Dry &quot;,quantity_d))) 
   { 
      cin.clear(); 
      cin.ignore (80,'\n'); 
      cout<<&quot; &quot;<<&quot;Invalid input ...... please re-enter. &quot;<<endl; 
   } 
    
    while (!(valid_read(&quot;hires&quot;,quantity_h))) 
   { 
      cin.clear(); 
      cin.ignore (80,'\n'); 
      cout<<&quot; &quot;<<&quot;Invalid input ...... please re-enter. &quot;<<endl; 
   } 
    

} 
    

/* This function uses the action type entered by the user to choose if the user wants to purchase,sell display inventory ,instructions or quit*/ 

void process_orders(                                            
             int& quantity_c,                                // refernce for c 
               int& quantity_p,                                 //refernce for p 
               int& quantity_d,                                 //refernce for d 
               int& quantity_h                                  //refernce for h 
                           ) 
{ 
  char ch;                          //holds the action enterd by user 
  char Brand_number;                //holds the company code entered by the user    
  int amount;                       //holds the amount of soda entered 
//  int quantity;                     //holds quantity of soda in inventory 

  while((ch=get_action(Brand_number,amount))!='Q') 
  { 
    
       switch (ch) 
      { 
          case 'S':             
            case 'P':switch (Brand_number) 
                      { 
                           case '1': 
                                sell_purchase(amount,quantity_c,ch); 
                               break; 
                           case '2': 
                                     sell_purchase(amount,quantity_p,ch); 
                               break; 
                              case '3': 
                                     sell_purchase(amount,quantity_d,ch); 
                               break; 
                            case '4': 
                                     sell_purchase(amount,quantity_h,ch); 
                               break; 
                              default: 
                              cout<<&quot; &quot;<<&quot;The action you requested is invalid..nothing has been doneD&quot;<<endl; 
                      } 
           break; 
          case 'D': 
              cout<<&quot;Present Inventory:&quot;<<endl; 
              inven_display(quantity_c,quantity_p,quantity_d,quantity_h); 
           break; 
            case 'I': 
              instr_display(); 
           break; 
          default: 
              cout<<&quot; &quot;<<&quot;The action you requested is invalid..nothing has been doneC&quot;<<endl; 
      } 
   } 
    cout<<&quot;Closing Inventory:&quot;<<endl; 
    inven_display(quantity_c,quantity_p,quantity_d,quantity_h); 
} 

/*This function displays the  inventory*/ 

void inven_display(int quantity_c,            
                int quantity_p, 
                int quantity_d, 
                int quantity_h 
               ) 
{    
   cout<<&quot; &quot;<<&quot;Coke       -- &quot;<<quantity_c<<&quot; &quot;<<&quot;cases&quot;<<endl; 
    cout<<&quot; &quot;<<&quot;Pepsi      -- &quot;<<quantity_p<<&quot; &quot;<<&quot;cases&quot;<<endl; 
   cout<<&quot; &quot;<<&quot;Canada Dry -- &quot;<<quantity_d<<&quot; &quot;<<&quot;cases&quot;<<endl; 
    cout<<&quot; &quot;<<&quot;Hires      -- &quot;<<quantity_h<<&quot; &quot;<<&quot;cases&quot;<<endl; 
      } 


/* function computes the sell and purchase order*/ 

void sell_purchase( int amount,int& quantity,char ch)  
{ 
    
    
   if(ch=='S') 
   { 
      if(amount>quantity){ 
         cout<<&quot; &quot;<<&quot;Insufficient inventory to fill sell order,nothing changed&quot;<<endl; 
      } 
       else 
       { 
         quantity=quantity - amount; 
          cout<<&quot; &quot;<<&quot;Inventory updated..&quot;<<endl; 
      } 
   } 
    else 
       
    { 
      quantity= amount + quantity; 
       cout<<&quot; &quot;<<&quot;Inventory updated..&quot;<<endl; 
    } 



} 


 /* function extracts the action,company code and amount from keyboard*/ 

 char get_action(char& Brand_number,int& amount)        
 { 
    char ch; 
    
   cout<<&quot;Enter action, and, if needed, company code and amount: &quot;; 
    cin>>ch; 
   cout<<ch<<endl; 
   if(ch=='P' ||ch =='S') 
    
   { 
       
       if(!(cin>>Brand_number))||(Brand_number!=('1'))||(Brand_number!=('2'))||(Brand_number!=('3'))||(Brand_number!=('4'))) 
      { 
         cin.clear(); 
         cin.ignore(); 
            cout<<&quot; &quot;<<&quot;The action you requested is invalid..nothing has been doneA.&quot;<<endl; 
            //cout<<&quot;Enter action, and, if needed, company code and amount: &quot;; 
         return INVALID; 
         //cin>>ch; 
          }          
         else 
            if(!((amount=get_amount())>NEGATIVE_VALUE)) 
         { 
          cin.clear(); 
         cin.ignore(); 
            cout<<&quot; &quot;<<&quot;The action you requested is invalid..nothing has been doneB.&quot;<<endl; 
      //   cout<<&quot;Enter action, and, if needed, company code and amount: &quot;; 
            return INVALID; 
            } 
        

   }else 
    
      clear_line(); 
    
    
   return ch; 
} 

/*error trap for amount*/ 

  int get_amount()                      
   { 
      int amount; 

      if (!(cin>>amount)){ 
         cin.clear(); 
         cin.ignore(); 
            return NEGATIVE_VALUE; 
      } 
        return amount; 
      
      } 


  void clear_line() 
  { 
     cin.ignore(80,'\n'); 
  } 

  
bool  valid_read(string Brand_name,int& quantity) 
   { 
    
       cout<<&quot;Enter number of cases of&quot;<<&quot; &quot;<<Brand_name<<&quot; &quot;<<&quot;on hand: &quot;; 
      quantity =get_amount(); 

      return quantity> NEGATIVE_VALUE; 
   }



Thanks in Advance
 
I think in your situadion is more logically to use operator && instead of ||, or to use operator == instead of !=:
1.
Brand_number ==('1')) || (Brand_number ==('2'))||(Brand_number==('3'))||(Brand_number==('4')))
or
2.
Brand_number!=('1'))&&(Brand_number!=('2'))&&(Brand_number!=('3'))&&(Brand_number!=('4')))

Ion Filipski
1c.bmp
 
On line 206,

if(!(cin>>Brand_number))||(Brand_number!=('1'))||(Brand_number!=('2'))||(Brand_number!=('3'))||(Brand_number!=('4')))

u have one extra ')'.

That could be causing the problem.


In the sweat of thy brow shall you eat your bread.
-Bible

 
I only want it to enter the loop if its not 1,2,3,and 4. If I use == then wouldn't it enter the loop if the input is 1,2,3, or 4?

And one more question; I can't seem to get the paranthesis right! do i need to add more or take some away?

Thanks for the speedy reply!
 
too many operations in a single line:
if(!(cin>>Brand_number))||(Brand_number!=('1'))||(Brand_number!=('2'))||(Brand_number!=('3'))||(Brand_number!=('4')))

put this piece of code out of if(..., and your code will be more easy to understand and/or to debug.
(cin>>Brand_number)

2. I think if you would check if Brand_number is none of '1', '2', '3', '4' you should use && instead of ||

Ion Filipski
1c.bmp
 
Ok so it runs but, it gives me an invalid error when I type in 1,2,3, or 4.Why is that? its like its not even entering the if loop int get_action.

here's a copy of the sample run:

You will first be prompted for the inventories for each of the four brands.
You should then enter the initial number of cases available for each brand.
All other commands should follow one of these formats:
(P)urchase + <brand number> + <amount>.
(S)ell + <brand number> + <amount>.
(D)isplay present inventory for the four brands.
Show(I)structions for user input.
(Q)uit.
Brand Number codes:
1--Coke 2--Pepsi 3--Canada Dry 4-- Hires
Enter number of cases of Coke on hand: 10
Enter number of cases of Pepsi on hand: 10
Enter number of cases of Canada Dry on hand: 10
Enter number of cases of hires on hand: 10
Enter action, and, if needed, company code and amount: P 1 3
P
The action you requested is invalid..nothing has been done.
The action you requested is invalid..nothing has been done
Enter action, and, if needed, company code and amount: 3
The action you requested is invalid..nothing has been done
Enter action, and, if needed, company code and amount: P 100 3
P
The action you requested is invalid..nothing has been done.
The action you requested is invalid..nothing has been done
Enter action, and, if needed, company code and amount: 0
The action you requested is invalid..nothing has been done
Enter action, and, if needed, company code and amount:

basically it should say &quot;inventory updated&quot; when you type in &quot;P 1 3&quot; and &quot;The action you requested is invalid..nothing has been done.&quot; If you type in somthing like &quot;P 300 1&quot;.

Is there somthing wrong with my logic?

 
Now with && I get this:
You will first be prompted for the inventories for each of the four brands.
You should then enter the initial number of cases available for each brand.
All other commands should follow one of these formats:
(P)urchase + <brand number> + <amount>.
(S)ell + <brand number> + <amount>.
(D)isplay present inventory for the four brands.
Show(I)structions for user input.
(Q)uit.
Brand Number codes:
1--Coke 2--Pepsi 3--Canada Dry 4-- Hires
Enter number of cases of Coke on hand: 100
Enter number of cases of Pepsi on hand: 100
Enter number of cases of Canada Dry on hand: 100
Enter number of cases of hires on hand: 100
Enter action, and, if needed, company code and amount: P 1 300
P
Inventory updated..
Enter action, and, if needed, company code and amount: S 100 1
S
Inventory updated..
Enter action, and, if needed, company code and amount: 1
The action you requested is invalid..nothing has been done
Enter action, and, if needed, company code and amount:

it says Inventory updated , then invalid action...
thats the same result i get without using :
Brand_number!=('1'))&&(Brand_number!=('2'))&&(Brand_number!=('3'))&&(Brand_number!=('4')))
 
And the weird thing is that it doesnt change the amount thats in the inventory eventhough it says &quot;inventory updated&quot; for the &quot;S 100 1&quot;. The sell_purchase function is responsible for updating the inventory, but i dont know why it would display &quot;inventory updated&quot; when the company number entered is invaild.
 
This is elementary logic, now will write the program for you. Better will be you to walk the code with debugger.

The following techincs are very often used in different programs:

if(...&&....&&.....)//<- the Brand_number is none of these
{
..do something there
do something
and/or if(other check there..
and/or switch(...)case...
do something
}else if(other check there..
{//the Brand_number is one of above ^
do something
and/or if(other check there..
and/or switch(...)case...
do something
}

Ion Filipski
1c.bmp
 
Dear Amino,

In my humble opinion, please spend 30 minutes learning how to use debugger to step through your code.

Alternately, at various points in your program, dump the impt variables to the screen and chk if they are behaving as they should be.

Even experienced programmers make elementary logic mistakes. So, plz learn to use debugger.

good luck!

In the sweat of thy brow shall you eat your bread.
-Bible

 
Sorry for a typo, instead of &quot;now will write&quot; i would write &quot;no one will write&quot;

Ion Filipski
1c.bmp
 
Is there a tutorial somewhere on how to use the debuger in visual C++? I do turn it on but just runs the program.

thanks for the help
 
1. put breakpoints in your code and press f5.
or
2. press menu Debug->StepInto


or press f1 and read help, it really helps.

Ion Filipski
1c.bmp
 
hmm, I guess i'am lost. Most of this is my partners code and i'am just trying help him fix it. This is a programming 2 project but i haven't coded C++ in over a year so I barely remember anything. Thats why i'am having troubele figuring out whats wrong. I think I have to change somthing with the else if statement, that might be causing trouble when brand_number ==1,2,3,or 4. Thanks for help, i did try the debugger, the program wasn't breaking where it should.It breaks at the else if when its both == or != . Is that where the problem is? Again thanks for help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top