2 Replies - 803 Views - Last Post: 23 October 2008 - 10:26 PM Rate Topic: -----

#1 Se7Olutionyg   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 21-October 08

Functions and loop next

Posted 23 October 2008 - 04:07 PM

#include <iostream>
using namespace std;
//Function declare
void displayMenu(void);
void getSelection (int &userChoice);
void processChoice (int userChoice);

float monitorCoke( float );
float monitorpep ( float ); 
float monitorcana ( float);
float monitorhire ( float );
 // variable declare and const
 
float option, cocaout, pepsiout ,canaout,pepleft,canaleft,cokeleft,hireout ,
	pepfinal, cokefinal,
	canafinal, hire,
	 hirefinal;
const float coke = 1000;
const float pep = 1000;
const float cana = 1000;
const float hires = 1000;
float get_option;

//Main
int main (void)
{
   int userChoice = 0;
	displayMenu (void);
	getSelection(userChoice);
	processChoice (userChoice);
	monitorCoke( cocaout);
	monitorpep (  pepsiout);
	monitorcana (  canaout);
	monitorhire (  hireout);
	return 0;
}
//end main

//start function
void displaymenu(void)
{
	 cout << " Have a nice week. Each product will have 1000 pieces a week" <<endl;
	 cout << " 1. Coca-Cola : 1000 pieces" << endl;
	 cout << " 2. Pepsi : 1000 pieces" << endl;
	 cout << " 3. Canada Dry" << endl;
	 cout << " 4. Hires " << endl;
	 }
void getSelection (int &userChoice)
{
	 cout <<  "Please enter your choice:" << endl;
	 cin>> userChoice;
	 }
void processChoice (int userChoice)
{
if (option == 1.0)
{
		  
		   cout<< "How many Coca-cola  would you like: " << endl;
		   cin>>  cocaout; 
		   }
		   else if (option == 2.0 ) 
		   {

			  
					  cout<< "How many Pepsi would you like: " << endl;
					  cin >> pepsiout;
					 
				}
				
		   else if ( option ==3.0 )
		   {
				   cout<< "How many Canada Dry would you like: " << endl;
				   cin >> canaout;
				 
			 
				   }
		   else if ( option == 4.0) 
		   {
						 cout<< "How many Hires would you like: " << endl;
						 cin>> hireout;
						 }// end if

}

float monitorCoke( float cokeleft)
		{
			cocaout = 0;
		   cokeleft = coke;
		   while ( cocaout < cokeleft)
		   {
		   cokeleft = cokeleft - cocaout; 
		   cout << " There are " << cokeleft << " Coca-cola left in the machine " << endl;
		  
		   }
		   return cokeleft;
		}
		   
		   
 float monitorpep ( float pepleft)						
		   {
		   pepsiout = 0;
		   pepleft = pep;
		   while (pepsiout < pepleft)	
		   {
		   pepleft = pepleft - pepsiout; 
		   cout << " There are " << pepleft << "Pepsi left in the machine " << endl;
		   }
		   return pepleft;
		   }//end function monitorpep
 float monitorcana ( float canaleft)						
 {
		   canaout = 0;
		   canaleft = canaleft - canaout;
		   while (canaout < canaleft)
		   
		   {
					canaleft = canaleft - canaout; 
		   cout << " There are " << canaleft << "Canada Dry left in the machine " << endl;
		   
		   }
		   return canaleft;
		   }// end monitorcana
  float monitorhire ( float hireleft)						
  {
		   hireout = 0;
		   hireleft = hire;
		   while (canaout < canaleft)
		   
		   {
					hireleft = hireleft - hireout; 
		   cout << " There are " << hireleft << "Hires left  in the machine " << endl;
		   
		   }
		   return hireleft;
		   }// end monitorhire



This time, I tried to fix all the error but then I have the error with displayvoid function, i don't know why though trying many solutions

Is This A Good Question/Topic? 0
  • +

Replies To: Functions and loop next

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Functions and loop next

Posted 23 October 2008 - 09:46 PM

well you declare a function void displayMenu(void);
but implement it as: void displaymenu(void);

One letter. also when you use it, the line should be:
displayMenu();

of course... once you fix that you still have some problems... looks like an infinite loop. I'll take more of a look at it in a bit (going to come back to it).

This post has been edited by NickDMax: 23 October 2008 - 09:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Functions and loop next

Posted 23 October 2008 - 10:26 PM

Well I have looked over your program and... well there are a lot of things going on here that we should take a look at.

First off lets answer the first question: Why is there an infinite loop. Well look at the function:

Quote

float monitorCoke( float cokeleft)
{
	cocaout = 0; // initialize to 0
	cokeleft = coke; // initialize to 1000
	while ( cocaout < cokeleft)  // will this ever happen? Can cocaout ever be < cokeleft?
	{
		cokeleft = cokeleft - cocaout; //1000 - 0 = 1000
		cout << " There are " << cokeleft << " Coca-cola left in the machine " << endl;
	}
	return cokeleft;
}


The problem is that cokeleft = cokeleft - cocaout; never changes the value of cokeleft because cocaout = 0. Thus the infinite loop.

Ok... that problem aside there are a lot of things going on in your program that are going to cause you troubles:

#1 Try to limit the use of global variables to constants only. Your program has all kinds of global variable and it probably does not need any.

In your processChoice() function you take in an argument (good) but then you try to use a global variable to determine the option. Try this instead:
	if (userChoice == 1) {
		cout<< "How many Coca-cola  would you like: " << endl;
		cin>>  cocaout;
	} else if (userChoice == 2 ) {
		cout<< "How many Pepsi would you like: " << endl;
		cin >> pepsiout;
	} else if ( userChoice ==3 ) {
		cout<< "How many Canada Dry would you like: " << endl;
		cin >> canaout;
	} else if ( userChoice == 4) {
		cout<< "How many Hires would you like: " << endl;
		cin>> hireout;
	}// end if
}


now if we remove the cocaout = 0 line from the monitorCoke() function it is no longer an infinite loop. -- of course the problem is that we are still using global variables... since I don't really know what the purpose of the program is I can't really help you eliminate them but I can tell you that in the end you really should not need any.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1