something's wrong here

issues with += operator in processing module

Page 1 of 1

2 Replies - 303 Views - Last Post: 20 October 2009 - 08:10 PM Rate Topic: -----

#1 rahbtrek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-October 09

something's wrong here

Posted 20 October 2009 - 07:50 PM

my instructor said my pseudocode was sound, but visual c++ says different. the tdebugging error message says "the variable totval is not initialized." the error occurs at the lines "totval += totval + val<coin>". the assignment is to use the += operator in the processing module, but obviously the pseudocode and and c++ dont agree. i've rearranged this thing inside and out and i get the same results, which is an output of a 50 digit negative number. below is the code matching the pseudocode that my instructor said was sound. please help me. i'll mail you a beer

#include <iostream>
#include <iomanip>
using namespace std;

#define NAME "your name"
#define HALFDOLLAR	.5	/* half dollar accurate to two decimal digits */
#define QUARTER	.25	/* quarter accurate to two decimal digits */
#define DIME	.1	/* dime accurate to two decimal digits*/
#define NICKEL	.05	/* nickel accurate to two decimal digits  */
#define PENNY	.01 /* penny accurate to two decimal digits */


int main ()
{
  double	 amthlf, /* number of half dollars */
		  amtqua, /* number of quarters */
		  amtdim, /* number of dimes */
		  amtnic, /* number of nickels */
		  amtpen; /* number of pennies */
	valhlf, /* value of half dollars */
		  valqua, /* value of quarters */
		  valdim, /* value of dimes */
		  valnic, /* value of nickels */
		  valpen, /* value of pennies */
		  totval; /* value of all change */

	cout << "\nChange Counter\n";
	cout << "\nEnter the number of Half Dollars:   ";
	cin >> amthlf;
	cout << "\nEnter the number of Quarters:	   ";
	cin >> amtqua;
	cout << "\nEnter the number of Dimes:		  ";
	cin >> amtdim;
	cout << "\nEnter the number of Nickels		 ";
	cin >> amtnic;
	cout << "\nEnter the number of Pennies		 ";
	cin >> amtpen;

	valhlf = HALFDOLLAR * amthlf;
	totval += totval + valhlf;
	valqua = QUARTER * amtqua;
	totval += totval + valqua;
	valdim = DIME * amtdim;
	totval += totval + valdim;
	valnic = NICKEL * amtnic;
	totval += totval + valnic;
	valpen = PENNY * amtpen;
	totval += totval + valpen;
	
	cout << "\nThe total value of the half dollars is $:"<< fixed << setw(8)
	  << setprecision(2) << valhlf;
	cout << "\n\nThe total value of the quarters is $:  "<< setw(10) << valqua;
	cout << "\n\nThe total value of the dimes is $:	 "<< setw(10) << valdim;
	cout << "\n\nThe total value of the nickels is $:   "<< setw(10) << valnic;
	cout << "\n\nThe total value of the pennies is $:   "<< setw(10) << valpen;
	cout << "\n\nThe value of all the coins in "<<NAME<<"'s drawer $:" << setw(10) << totval;
	cout << "\n";

	return 0;
}



thx again for your time

Is This A Good Question/Topic? 0
  • +

Replies To: something's wrong here

#2 zhanfeng  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 75
  • Joined: 09-November 08

Re: something's wrong here

Posted 20 October 2009 - 08:01 PM

The variable totval is indeed not initialized. You are already using totval before initializing it.
totval += [b]totval[/b] + val<coin>

Was This Post Helpful? 0
  • +
  • -

#3 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: something's wrong here

Posted 20 October 2009 - 08:10 PM

all of this
  amthlf, /* number of half dollars */
		  amtqua, /* number of quarters */
		  amtdim, /* number of dimes */
		  amtnic, /* number of nickels */
		  amtpen; /* number of pennies */
	valhlf, /* value of half dollars */
		  valqua, /* value of quarters */
		  valdim, /* value of dimes */
		  valnic, /* value of nickels */
		  valpen, /* value of pennies */
		  totval; /* value of all change */


Initialize them with 0
totval=0;


Hope that helps :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1