4 Replies - 607 Views - Last Post: 02 October 2012 - 08:48 AM Rate Topic: -----

#1 mored  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-August 12

Program calculates correctly BUT crashes when entering last FOR loop

Posted 02 October 2012 - 07:33 AM

This program calculates and displays price per egg for certain number of eggs, but crashes when it attempts to display a list of egg prices, using a FOR loop ... Any help would be most appreciated ...

(using Visual Studio 6.0)

int main()
{ 
 cout << "Hello! Welcome to Ernesto's Eggs! \n Please enter the number of eggs you wish to buy." << endl; //asks for total number of eggs
 //Displays list
 double eggnum;
 cin >> eggnum; //User inputs number of eggs 
 double dozennum = eggnum/12; //Divides by 12 in order to get number of dozens
 double dozprice; 
 if (dozennum>0 && dozennum<4)
 {
  dozprice = 0.50;
 
 }
 else if ( dozennum >=4 && dozennum<6)
 {
  dozprice= 0.45;
 }
 else if (dozennum >=6 && dozennum<11)
 {
  dozprice = 0.40;
 }
 else if (dozennum>= 11)
 {
  dozprice = 0.35;
 
 }
 if (eggnum == 123456789)
 { 
  cout << "     O     " << endl;
  cout << "   \\ | /   " << endl;
  cout << "     |     " << endl;
  cout << "    / \\   " << endl;
  cout << "Congratulations! Easter Egg found. \n -LV" << endl;
 }

 //This section assigns prices per dozen according to number of eggs bought
 double unitprice = dozprice/12; //Calculates price per egg
 double totalprice = dozennum * dozprice; //Calculates total price= price per dozen times number of dozens
 cout.setf(ios::fixed);
 //Outputs prices
 cout << "Your price per dozen is $";
 cout.precision(2);cout << dozprice;
 cout << "." <<endl;
 cout << "The price per egg is $";
 cout.precision(2);cout << unitprice; 
 cout <<"." << endl;
 cout << "Your total price is $";
 cout.precision(2); cout << totalprice << ". " << endl;
 cout <<"Thanks for buying from Ernesto's Eggs!" << endl;
 //Displays list
 for (double egg=1;120 >= egg;egg= egg+1) 
 {
 double dozpr;
 double dozen = egg/12; //Divides by 12 in order to get number of dozens
 //This section assigns prices per dozen according to number of eggs bought
 double unitpr = dozpr/12; //Calculates price per egg
 double totalpr = dozen * dozpr;//Calculates total price= price per dozen times number of dozens
 if (dozen>0 && dozen<4)
 {
  dozpr = 0.50;
 
 }
 else 
 {
  if ( dozen >=4 && dozen<6)
  {
   dozpr= 0.45;
  }
  else
  {
   if (dozen >=6 && dozen<11)
   {
    dozpr = 0.40;
   }
   else
   {
    dozpr = 0.35;
 
   }
  }
  
 }
  cout << egg;
  cout << unitpr;
 }
 
 
 
 
 return 0;
}



UPDATE: I corrected the FOR statement
for (double egg=1;120 >= egg;egg= egg+1)
and changed it to
for (double egg=1;egg<=120;egg++)
BUT I still got same error (it crashes when trying to display list)

Is This A Good Question/Topic? 0
  • +

Replies To: Program calculates correctly BUT crashes when entering last FOR loop

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1703
  • View blog
  • Posts: 3,262
  • Joined: 30-May 10

Re: Program calculates correctly BUT crashes when entering last FOR loop

Posted 02 October 2012 - 07:42 AM

You need to post your actual crash information.

It seems to work here.
$ g++ -Wall foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:60:9: warning: unused variable ‘totalpr’ [-Wunused-variable]
$ ./a.out 
Hello! Welcome to Ernesto's Eggs! 
 Please enter the number of eggs you wish to buy.
33
Your price per dozen is $0.50.
The price per egg is $0.04.
Your total price is $1.38. 
Thanks for buying from Ernesto's Eggs!
1.00 0.00
2.00 0.04
3.00 0.04
<< snipped for brevity >>
118.00 0.03
119.00 0.03
120.00 0.03


I just fixed the final output to add some space and newline for readability.
Was This Post Helpful? 0
  • +
  • -

#3 mored  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-August 12

Re: Program calculates correctly BUT crashes when entering last FOR loop

Posted 02 October 2012 - 07:46 AM

"unhandled exception in NTDLL.dll: Access Violation"
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,753
  • Joined: 16-October 07

Re: Program calculates correctly BUT crashes when entering last FOR loop

Posted 02 October 2012 - 08:20 AM

Use functions! Also, you're faking yourself out:
double dozpr; // declared by not assigned
double unitpr = dozpr/12; // oops, still not assigned




It couldn't get it to break, but you do have some strange math and incrementing doubles can send you odd places. Everything is not a double! How can you buy 1.2346 eggs? What does a fraction of a dozen mean and how is it even useful here?

I'd go with something like:
#include <iostream>

using namespace std;

// you use this in two places, make a function
double getDozenPrice(int dozennum) {
	// using your logic.  However, 0 dozen still has a price
	if (dozennum<4) { return 0.50; }
	if (dozennum<6) { return 0.45; }
	if (dozennum<11) { return 0.40; }
	return 0.35;
}

void userPurchase() {
	// your code here
	// use getDozenPrice here too
}

void showList() {
	// 120 >= egg:  who the hell are you, Yoda?
	for (int egg=1; egg<=120; egg++) {
		int dozen = egg/12; // because you don't need or want the fraction
		double dozpr = getDozenPrice(dozen);
		double unitpr = dozpr/12;
		// this seems fundamtally wrong
		//double totalpr = dozen * dozpr;
		// I'd use unit price
		double totalpr = unitpr * egg;
		cout << egg 
			<< '\t' << dozen
			<< '\t' << unitpr 
			<< '\t' << totalpr 
			<< endl;
	}
}

int main() { 
	userPurchase();
	showList();

	return 0;
}



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#5 mored  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-August 12

Re: Program calculates correctly BUT crashes when entering last FOR loop

Posted 02 October 2012 - 08:48 AM

OK great --- thank you !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1