• 0

ugh...why isn't this working?


Question

The program isn't running as it should, it tells me there are errors and there should be a ";" behind structure "else (quarters == 0)----here----{" and anywhere else that this structure shows up in the program.

any ideas why

#include <iostream.h>
#include <string.h>

main()
{
	int cents,quarters,dimes,nickels,pennys;
	const int array_size = 5;
	int centsarray[array_size];
	cout << "Input the number of cents to be calculated" << '\n';
	cin >> cents;

////////////////////////////////////////////////////////////////

	quarters = cents / 25;
	if (quarters >= 1){
  centsarray[1] = quarters;
	} else (quarters == 0) {
  centsarray[1] = 0;
	}

////////////////////////////////////////////////////////////////

	dimes = ((cents % 25) / 10);
	if (dimes >= 1){
  centsarray[2] = dimes;
	} else (dimes == 0) {
  centsarray[2] = 0;
	}

////////////////////////////////////////////////////////////////

	nickels = (((cents % 25) % 10) / 5);
	if (nickels >= 1){
  centsarray[3] = nickels;
	} else (nickels == 0) { 
  centsarray[3] = 0;
	}

////////////////////////////////////////////////////////////////

	pennys = ((((cents % 25) % 10) % 5) / 1);
	if (pennys > 0){
  centsarray[4] = pennys;
	} else (pennys == 0) {
  centsarray[4] = 0;
	}

////////////////////////////////////////////////////////////////

	cout << "Your change is: " 
   << centsarray[1] << " quarters " 
   << centsarray[2] << " dimes " 
   << centsarray[3] << " nickels" 
   << centsarray[4] << " pennys" << '\n';

	return 0;

}

err....this should work right???????

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

hmmm.... just tried that, and i see where you're coming from, but it had no effect on the code....

here is the compiler's output:

--------------------Configuration: Cpp1 - Win32 Debug--------------------

Compiling...

Cpp1.cpp

C:\Documents and Settings\Student 1\My Documents\C++\Project 8-9\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'

C:\Documents and Settings\Student 1\My Documents\C++\Project 8-9\Cpp1.cpp(26) : error C2143: syntax error : missing ';' before '{'

C:\Documents and Settings\Student 1\My Documents\C++\Project 8-9\Cpp1.cpp(35) : error C2143: syntax error : missing ';' before '{'

C:\Documents and Settings\Student 1\My Documents\C++\Project 8-9\Cpp1.cpp(44) : error C2143: syntax error : missing ';' before '{'

Error executing cl.exe.

Cpp1.obj - 4 error(s), 0 warning(s)

Link to comment
Share on other sites

  • 0

"else (quarters == 0)" should be "else if (quarters == 0)" if you are going to use the condition "(quarters == 0)"

and so on for the others.

if(condition)
 ?{

 ?}//end of if
else if(condition)
 ?{

 ?}//end of else if
else //no condition here
 ?{

 ?}//end of else

STV

Link to comment
Share on other sites

  • 0

well, the program at least runs now...however....I get this output

Input the number of cents to be calculated
85
Your change is: 0 quarters 1 dime 4202944 nickels1245044 pennys
Press any key to continue

Link to comment
Share on other sites

  • 0
well, the program at least runs now...however....I get this output

Input the number of cents to be calculated
85
Your change is: 0 quarters 1 dime 4202944 nickels1245044 pennys
Press any key to continue

584826922[/snapback]

You should initialize your variables to 0.

int cents=0,quarters=0,dimes=0,nickels=0,pennys=0;

Although I'm not sure if that's the problem, but when you get ridiculously high numbers, it's usually uninitialized variables. Hmm, just though of something, you're using 1-4, when you should be using 0 - 3.

Edited by kjordan2001
Link to comment
Share on other sites

  • 0
actually, it works fine on my end.  but that may be his problem now, kjordan2001.

STV

584826956[/snapback]

Huh, how's it working when he's using the wrong subscripts?

Edit: Hmm, actually he's made it 1 bigger than it needs to be, so I guess that's not the case.

Edit2: Works for me too. I was actually finding it odd it was printing out 3 quarters for him.

Edited by kjordan2001
Link to comment
Share on other sites

  • 0

FYI too, if your if statements (else, etc) are only one liners you don't need the braces. Just so you don't have to type as much

Cause I << lazy

I hate when simple things like that keep your program from runnin, happens to me all the time :pinch:

Yea get in the habit of declaring your values to a default (0) value, that way they're not full of garbage when you start :yes:

Link to comment
Share on other sites

  • 0

i think it is always good to have the braces there simply because you dont know when you might have to add another line to the body of that if statement and because it makes it easier to read.

STV

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.