LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2012, 11:43 PM   #1
melanie92
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Rep: Reputation: Disabled
Help with C Program


Hi there! I keep getting these reoccurring errors while running this program on Quincy 2005

#include <stdio.h>
#include <math.h>

int
main (void)
{
double g, d, t;

printf ("Enter time in seconds : \n");
scanf ("%lf", &t);

if (t > 0);
{
scanf ("%lf%lf", &d,&g);

g = 9.807;
d = sqrt(2*t/g);

printf ("Object has fallen %lf meters after %lf seconds of freefall on Earth", d, t);
}

else (t < 0);
{

printf ("Answer is not valid");
}

return (0);
}


The errors are:
c:/users/owners/documents/text3.c In function `main':
c:/users/owners/documents/text.c22: parse error before "else"
Unsuccessful build

Any help would be appreciated.
Thank you!
Note: Very little knowledge in the C Program
 
Old 10-17-2012, 11:55 PM   #2
crosstalk
Member
 
Registered: Jul 2010
Distribution: Gentoo, Debian, Mint, Xubuntu
Posts: 150

Rep: Reputation: 35
The issue is you have a semicolon immediately after the if. That separates the if from the braces after it.

When it runs into the else, it expects that the last set of braces was "connected" with an if statement -- but due to that semicolon, they aren't.

I suggest you remove that semicolon, as well as the one after your else clause (which does the same thing for the else -- which will not report an error but will make your program behave incorrectly).

Good luck!
 
Old 10-18-2012, 12:12 AM   #3
melanie92
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
I have removed the semi-colons and the same In function `main': error occurs as well as a new error:
c23: parse error before '{' token

Thank you for you time though!
 
Old 10-18-2012, 12:27 AM   #4
crosstalk
Member
 
Registered: Jul 2010
Distribution: Gentoo, Debian, Mint, Xubuntu
Posts: 150

Rep: Reputation: 35
Could you post your new code? By the way, if you put it between CODE tags, the formatting will be retained.
 
Old 10-18-2012, 12:36 AM   #5
melanie92
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Code:
#include <stdio.h>
#include <math.h>

int 
main (void)
{
	double g, d, t;
	
	printf ("Enter time in seconds : \n");
	scanf ("%lf", &t);
	
if (t > 0) 
{
	scanf ("%lf%lf", &d,&g); 
	
	g = 9.807;
	d = sqrt(2*t/g);
	
	printf ("Object has fallen %lf meters after %lf seconds of freefall on Earth", d, t);
}

else (t < 0)
{

printf ("Answer is not valid");
}

return (0);
}
 
Old 10-18-2012, 05:09 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Code:
else (t < 0)
{
    ...
}
does not make sense. It should be either

Code:
else if (t < 0)
{
    ...
}
or just

Code:
else
{
    ...
}
 
2 members found this post helpful.
Old 10-19-2012, 04:47 PM   #7
DutchGeek
Member
 
Registered: Sep 2006
Distribution: SuSE, Slackware
Posts: 55

Rep: Reputation: 10
Why do you have this:

Code:
scanf ("%lf%lf", &d,&g);
You're not reading distance and acceleration from the input, acc. is initialized to 9.807, and distance is calculated from it. So you can remove that line.


Cheers
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Could someone program a SH / Bash program with Zenity to play radio based on a site? frenchn00b Programming 2 02-25-2011 04:16 PM
LXer: Can't Program, won't Program? Then Mash the Web with Mozilla's Ubiquity LXer Syndicated Linux News 0 05-01-2010 12:30 PM
open source dvd/cd copy program? burning on the fly program? vieya Linux - Software 3 12-14-2009 09:16 PM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 12:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration