Jump to content

What am I Doing wrong?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
onat12agi@gmail.com

onat12agi@gmail.com

    Newbie

  • Members
  • Pip
  • 8 posts
#include <stdio.h>
#include <conio.h>
main()
{
int pr,cr,totalbill,consumption,reading;
clrscr();
	printf("\n Welcome to the maynilad Water Consumption Bill Calculator");
	printf("\n Enter Current Reading: ");scanf ("%d", &cr);
	printf("\n Enter Previous Reading: ");scanf ("%d", &pr);
	consumption=cr-pr;

	if ((reading>1) && (reading<=50))
totalbill=consumption*3;

	else if ((reading>50) && (reading<=100))
totalbill=150+(consumption-50)*4;

	else if (reading>100)
totalbill=350+(consumption-100)*5;


	elseif(reading<1)
[COLOR="Red"]printf("Invalid Value");[/COLOR]


[COLOR="Blue"]printf("The total Bill is %d", totalbill)[/COLOR]


getch();
}


The red text has an "statement missing ;" although I have an ;.
and the blue text, how could I seperate this printf from the else if statement.

I'm Practicing my C Programming, so I'm a little rusty now, having priorities on other program laguages.

Edited by WingedPanther, 24 July 2009 - 10:16 AM.
add code tags (the # button)


#2
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
1) Use code tags (the # button) when posting code...
2) Else if should be in two words, before your red printf
3) Your blue printf() is missing a ;
4) The blue printf() should already be separated from the else if statement. It will always be executed.
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I would recommend reworking your code as follows:
#include <stdio.h>
#include <conio.h>
main()
{
  int pr,cr,totalbill,consumption,reading;
  clrscr();
  printf("\n Welcome to the maynilad Water Consumption Bill Calculator");
  printf("\n Enter Current Reading: ");scanf ("%d", &cr);
  printf("\n Enter Previous Reading: ");scanf ("%d", &pr);
  consumption=cr-pr;

  if ((reading>1) && (reading<=50))
  {
    totalbill=consumption*3;
  }
  else if ((reading>50) && (reading<=100))
  {
    totalbill=150+(consumption-50)*4;
  }
  else if (reading>100)
  {
    totalbill=350+(consumption-100)*5;
  }
  else if(reading<1)
  {
    printf("Invalid Value");
  }
  printf("The total Bill is %d", totalbill);
  getch();
}
Notice that the indentation is fixed, making it easier to read. Also, use {} for every if statement, even a one-liner. It makes your intentions clearer.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
onat12agi@gmail.com

onat12agi@gmail.com

    Newbie

  • Members
  • Pip
  • 8 posts

Quote

Notice that the indentation is fixed, making it easier to read. Also, use {} for every if statement, even a one-liner. It makes your intentions clearer.

#include <stdio.h>
#include <conio.h>
main()
{
int pr,cr,totalbill,consumption,reading;
clrscr();
	printf("\n Welcome to the maynilad Water Consumption Bill Calculator");
	printf("\n Enter Current Reading: ");scanf ("%d", &cr);
	printf("\n Enter Previous Reading: ");scanf ("%d", &pr);
	consumption=cr-pr;


if ((consumption>1) && (consumption<=50))
	{
	totalbill=consumption*3;
	}
else if ((consumption>50) && (consumption<=100))
	{
	totalbill=150+(consumption-50)*4;
	}
[COLOR="Red"]else if (consumption>100)
	}[/COLOR]
	totalbill=350+(consumption-100)*5;
	{

[COLOR="SandyBrown"]else if (consumption<1)
	[/COLOR]{
	printf("Invalid Value");
	}
}
printf("\n The total Bill is %d Pesos   ", totalbill);
printf("\n The total Consumption is %d M3", consumption);
[COLOR="YellowGreen"]{[/COLOR]


getch();
}

When I followed your advice, the following errors occured:
Statement Missing ;
Misplaced Else

And a Compound Statement Missing}

I use Borland Turbo C++ IDE Dos Compiler Version 3.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Your braces after the red statement are backwards.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog