8 Replies - 468 Views - Last Post: 06 February 2018 - 03:30 PM Rate Topic: -----

#1 lonewolf71   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-February 18

[Error] 'else' without a previous 'if'

Posted 05 February 2018 - 04:28 PM

i cant figure out why i get this error, [Error] 'else' without a previous 'if'
its written in c.
here is the code
#include <stdio.h>
#include <stdlib.h>

int number_of_adults_in_house, numeber_of_kids_in_the_house, numeber_ofpeople_in_the_house;
float cost_of_lunch, how_much_money_on_hand, how_much_money_on_hand2 ;


int main()
{
	
	 printf("Lets figure out how many people live in your house.\n ");
	 printf("Enter how many adults live in you house \n");
	 scanf("%d", &number_of_adults_in_house);
	
	 printf("Enter how many adults live in you house \n");
	 scanf("%d", &numeber_of_kids_in_the_house);
	 numeber_ofpeople_in_the_house= number_of_adults_in_house + numeber_of_kids_in_the_house ;
	
	 printf("%There are %   i  people in the house. \n \n\n", numeber_ofpeople_in_the_house);
	
	
	
	 printf("Now let's see how much money you will have after lunch. \n");
	 printf("You always get the  #1 hamburger meal for lunch. It coasts $4.50 \n");
	 cost_of_lunch= 4.50 ;
	 printf("\nNow tell me how much money you have \n");
	 scanf("%d", & how_much_money_on_hand);
	 how_much_money_on_hand2= how_much_money_on_hand -  cost_of_lunch ;
	
	if  (how_much_money_on_hand2 >= 0);
	{
		printf("%You now only have $%,"" left. ", how_much_money_on_hand2 );
	
	}
	else 
	{
	 printf ( "Sorry but you don't have enough money for lunch today.");
	}
		
	return 0;
}



Thanks for any help

This post has been edited by Martyr2: 05 February 2018 - 04:32 PM
Reason for edit:: Fixed code tags. It is [code]your code here[/code]


Is This A Good Question/Topic? 0
  • +

Replies To: [Error] 'else' without a previous 'if'

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5207
  • View blog
  • Posts: 13,955
  • Joined: 18-April 07

Re: [Error] 'else' without a previous 'if'

Posted 05 February 2018 - 04:30 PM

You have a semicolon after the line if (how_much_money_on_hand2 >= 0)

That is going to cause you some problems because the if statement will be seen as a single line if body containing a "null statement". Try removing it and see if that helps. :)
Was This Post Helpful? 1
  • +
  • -

#3 lonewolf71   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-February 18

Re: [Error] 'else' without a previous 'if'

Posted 05 February 2018 - 04:35 PM

thank you for the fast response but it didn't make a different :nottalkingtoyou:
Was This Post Helpful? 0
  • +
  • -

#4 lonewolf71   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-February 18

Re: [Error] 'else' without a previous 'if'

Posted 05 February 2018 - 04:42 PM

correction i misunderstood you ! it did clear up the if/esle problem thank you! but still having a problem with the math part in that same section.
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5207
  • View blog
  • Posts: 13,955
  • Joined: 18-April 07

Re: [Error] 'else' without a previous 'if'

Posted 05 February 2018 - 04:52 PM

For your money on hand, you want to be scanf using a floating specifier, not %d. That is for integers and will truncate your cents. Try using %f for all variables which are floats. :)
Was This Post Helpful? 1
  • +
  • -

#6 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1460
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: [Error] 'else' without a previous 'if'

Posted 05 February 2018 - 04:57 PM

same goes for line 32

printf("%You now only have $%,"" left. ", how_much_money_on_hand2 );



should be

 printf("You now only have $%f,"" left. ", how_much_money_on_hand2 );

Was This Post Helpful? 1
  • +
  • -

#7 lonewolf71   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-February 18

Re: [Error] 'else' without a previous 'if'

Posted 06 February 2018 - 11:36 AM

thank you both Martyr2 and snoopy11. that did fix 1/2 the problem! but it wont give the amount left over if an amount greater then 4.50 is entered. i dont understand why though. any insight what im doing wrong.
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5207
  • View blog
  • Posts: 13,955
  • Joined: 18-April 07

Re: [Error] 'else' without a previous 'if'

Posted 06 February 2018 - 03:16 PM

I am not sure why you keep using percent signs and such at the beginning of your strings in your printf. Remove those. Secondly, you haven't put in a specifier for printing your values. Here is your program fixed up...

#include <stdio.h>
#include <stdlib.h>

int number_of_adults_in_house, numeber_of_kids_in_the_house, numeber_ofpeople_in_the_house;
float cost_of_lunch, how_much_money_on_hand, how_much_money_on_hand2 ;


int main()
{
	
	 printf("Lets figure out how many people live in your house.\n ");
	 printf("Enter how many adults live in you house \n");
	 scanf("%d", &number_of_adults_in_house);
	
	 printf("Enter how many adults live in you house \n");
	 scanf("%d", &numeber_of_kids_in_the_house);
	 numeber_ofpeople_in_the_house= number_of_adults_in_house + numeber_of_kids_in_the_house ;
	
	 printf("There are %i  people in the house. \n \n\n", numeber_ofpeople_in_the_house);
	
	
	
	 printf("Now let's see how much money you will have after lunch. \n");
	 printf("You always get the  #1 hamburger meal for lunch. It coasts $4.50 \n");
	 cost_of_lunch= 4.50 ;
	 printf("\nNow tell me how much money you have \n");
	 scanf("%f", & how_much_money_on_hand);
	 how_much_money_on_hand2= how_much_money_on_hand -  cost_of_lunch ;
	
	if  (how_much_money_on_hand2 >= 0)
	{
		printf("You now only have $%.2f left. ", how_much_money_on_hand2 );
	
	}
	else 
	{
	 printf ( "Sorry but you don't have enough money for lunch today.");
	}
		
	return 0;
}



Notice the print statement where I put in %.2f. This is saying "Put the value of how_much_money_on_hand2 at that location and specify it to 2 decimal places". Also notice that I removed the percent sign before your "There are %i people in your house" statement. Run this.

I entered 1 adult, 1 kid, got 2 people in the house, entered 10.00 and got a result of 5.50 in change. Which appears to be working fine after this initial test. :)

This post has been edited by Martyr2: 06 February 2018 - 03:17 PM

Was This Post Helpful? 0
  • +
  • -

#9 lonewolf71   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-February 18

Re: [Error] 'else' without a previous 'if'

Posted 06 February 2018 - 03:30 PM

Thank you!
I used the % where i did because the text book im using did it that way! I have very limited experience so...
but thank you especially for explaining why what i did dint work and what you did did work.

thanks to all who offered their help! it works fine now!

This post has been edited by Skydiver: 06 February 2018 - 05:06 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1