C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-25-2010, 12:44 AM   #1
Registered User
 
Join Date: Apr 2010
Posts: 7
Angry Misplaced else in function main -- help?

Hello,

i'm a newbie with C
i started learning it like 2 weeks ago by getting a few ebooks that i've found online..

attached with the ebook a booklet full of exercises to test what i've learned for each chapter but without the solutions .. :S

i managed to get through most tests but im stuck at these two.. hope you could point out wht im doing wrong:

Quote:
Write a complete C program that asks the user to enter continuously a series of words and the
word “end” to stop.
For each word entered, you should output the number of characters of this word and then ask him
to enter the other word.
Finally, when he finished entering all the words, you should output the number of words entered
(excluding “end”)
Code:
#include <stdio.h>
int main(void)
{
char word[256];
char stop[3]="end";
int cnt=0;
printf("Enter a word to know it's number of characters. once finished enter \"end\" to stop\n");
scanf("%s",&word);
while (stop!=word)
printf("The number of characters of %s is %u\n",word,strlen(word));
cnt = cnt++;
else
printf("you've decided to stop. you entered %d words so far.\n",cnt);
return 0;	
}
and this is the error:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
tma.c:
Warning W8065 tma.c 10: Call to function 'strlen' with no prototype in function main
Error E2054 tma.c 11: Misplaced else in function main
*** 1 errors in Compile ***


Quote:
Write a complete C program that calculates the roots of any quadratic equation of the form ax2 +
bx + c = 0 where a, b and c are variables that should be entered by user.
First, if a = 0 then the root is x= -c/b
otherwise, you have to find Δ, Δ=b2 – 4ac
If Δ< 0 there are no roots
If Δ= 0 we have 2 double roots: x1 = x2 = -b/2a
If Δ>0, two roots x1 = (-b-√Δ)/2a and (–b +√Δ)/2a
Hint: you can include math library to use some math functions
Code:
#include "stdio.h"
#include "math.h"

void main()
{
int a,b,c,delta;
float x,x1;
printf("\n Enter values of a,b,c for finding roots of a quadratic eq:\n");
scanf("%d%d%d",&a,&b,&c);

if (a == 0) {
x = -c/b
printf("in this case the root is %f",x);
}
else
 delta=b^2-4*a*c

if ( delta<0)
{
printf("there are no roots")
}
if (delta == 0)
{
x= -b/(2*a);
printf("we have double roots %f",x);
}
if (delta>0)
{ 
x = (-b-sqrt(delta))/2*a)
x1 = (-b+sqrt(delta)/2*a
printf("we have two roots which are %f and %f",x,x1);
}
}
ERROR:

Quote:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
new.c:
Error E2379 new.c 13: Statement missing ; in function main
Error E2379 new.c 18: Statement missing ; in function main
Warning W8004 new.c 33: 'x' is assigned a value that is never used in function main
Warning W8004 new.c 33: 'delta' is assigned a value that is never used in function main
*** 2 errors in Compile ***

Last edited by rollyah; 04-25-2010 at 01:05 AM. Reason: added the 2nd error
rollyah is offline   Reply With Quote
Old 04-25-2010, 01:00 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 12,704
The else must have a matching if, but there isn't any. In fact, it looks like you don't need an else.

By the way, you should indent your code properly. If you do so, you may discover a logic error due to the lack of braces.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 04-25-2010, 01:07 AM   #3
Registered User
 
Join Date: Apr 2010
Posts: 7
Quote:
Originally Posted by laserlight View Post
The else must have a matching if, but there isn't any. In fact, it looks like you don't need an else.

By the way, you should indent your code properly. If you do so, you may discover a logic error due to the lack of braces.
thanks for your reply

but if i remove else wouldn't that lead to an infinite loop?

im struggling to learn the how Tos for C so thanks again for helping out
rollyah is offline   Reply With Quote
Old 04-25-2010, 01:18 AM   #4
The Beautiful C++ Utopia
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,455
Start by doing so... First, indent your code.
Second, check all your lines of code for missing ; (there are at least two).
Third, check all your lines for missing parenthesises (there is at least one).
And fourth, there is a problem in the algorithm. a^2 is not a squared (it's a XOR 2). Nor should there be any squared variables in the formula. Look at it again, closely.
And last, you are missing parenthesises again. When you have a*b/c*d, you'll have to add parenthesises, remember? This is like a calculator.
And change void main to int main.
__________________
WARNING: Any and all code samples I post are not tested unless explicitly mentioned otherwise. Use at your own risk.
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Why did the Java creators shoot themselves in the foot?
Elysia is offline   Reply With Quote
Old 04-25-2010, 03:17 AM   #5
Ultraviolence Connoisseur
 
Join Date: Mar 2004
Posts: 239
Also your stop[3] doesn't leave room for a trailing null so it is actually an array of characters, not a valid C string.

You should use char stop[] = "end"; this will include the trailing \0. This is important due to the next error (besides the ones mentioned above) that you are trying to compare a character array to a string using a simple ==.

If you make end[] a string as I said above, then you can use strcmp(end,word) == 0 to see if they match.

Of course you also need to include string.h for strcmp and strlen() which you are already using.
nonpuz is offline   Reply With Quote
Old 04-30-2010, 12:04 PM   #6
Registered User
 
Join Date: Apr 2010
Posts: 7
so i found my prob with the first excercise..
though i'm still facing issues with the 2nd..

Code:
#include <stdio.h>
#include <math.h>
int main (void)
{
int a,b,c;
float x,x1,x2,y,delta;
     printf("enter three numbers please\n");
	 scanf("%d%d%d",a,b,c);
	 delta=b*b-4*a*c;
	     if (a==0)
		     x=(-c/b);
			 printf("the root is %f\n",x);
		 else 
		     printf("Delta=b2-4ac is equal to %f\n",delta);
			 
		    if (delta<0)
			     printf("there are no roots\n");
			else
			    if (delta==0)
			        y=(-b/(2*a));
		            printf("we have double roots x1=x2=%d/2*%d=%f\n",a,b,y);
				else
			        if (delta>0)
			            x1=((-b+sqrt(delta))/(2*a));
				        x2=((-b+sqrt(delta))/(2*a));
				        printf("there are two roots which are %f and %f\n",x1,x2);
				 
return 0;	     	 
}
error:

Quote:
Error E2054 roland2.c 13: Misplaced else in function main
Error E2054 roland2.c 22: Misplaced else in function main
*** 2 errors in Compile ***
rollyah is offline   Reply With Quote
Old 04-30-2010, 12:07 PM   #7
The Beautiful C++ Utopia
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,455
Fix your indentation.
It's your tool for finding these kinds of things.
__________________
WARNING: Any and all code samples I post are not tested unless explicitly mentioned otherwise. Use at your own risk.
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Why did the Java creators shoot themselves in the foot?
Elysia is offline   Reply With Quote
Old 04-30-2010, 12:11 PM   #8
Registered User
 
Join Date: Apr 2010
Posts: 7
Quote:
Originally Posted by Elysia View Post
Fix your indentation.
It's your tool for finding these kinds of things.
thank you for your advice.. but im struggling here..
i tried fxing my indentation over and over again..
i'm not sure i'm even doing it right..
any advice?
rollyah is offline   Reply With Quote
Old 04-30-2010, 12:13 PM   #9
The Beautiful C++ Utopia
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,455
For example, Visual Studio has an indentation tool. There are others.
But it isn't difficult to do it yourself. Consider this indented code of yours. You should be able to spot the problem:
Code:
#include <stdio.h>
#include <math.h>
int main (void)
{
	int a,b,c;
	float x,x1,x2,y,delta;
	printf("enter three numbers please\n");
	scanf("%d%d%d",a,b,c);
	delta=b*b-4*a*c;
	if (a==0)
		x=(-c/b);
	printf("the root is %f\n",x);
	else 
		printf("Delta=b2-4ac is equal to %f\n",delta);

	if (delta<0)
		printf("there are no roots\n");
	else
		if (delta==0)
			y=(-b/(2*a));
	printf("we have double roots x1=x2=%d/2*%d=%f\n",a,b,y);
		else
			if (delta>0)
				x1=((-b+sqrt(delta))/(2*a));
	x2=((-b+sqrt(delta))/(2*a));
	printf("there are two roots which are %f and %f\n",x1,x2);

	return 0;	     	 
}
__________________
WARNING: Any and all code samples I post are not tested unless explicitly mentioned otherwise. Use at your own risk.
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Why did the Java creators shoot themselves in the foot?
Elysia is offline   Reply With Quote
Old 04-30-2010, 12:17 PM   #10
Registered User
 
Join Date: Apr 2010
Posts: 7
Quote:
Originally Posted by Elysia View Post
For example, Visual Studio has an indentation tool. There are others.
But it isn't difficult to do it yourself. Consider this indented code of yours. You should be able to spot the problem:
Code:
#include <stdio.h>
#include <math.h>
int main (void)
{
	int a,b,c;
	float x,x1,x2,y,delta;
	printf("enter three numbers please\n");
	scanf("%d%d%d",a,b,c);
	delta=b*b-4*a*c;
	if (a==0)
		x=(-c/b);
	printf("the root is %f\n",x);
	else 
		printf("Delta=b2-4ac is equal to %f\n",delta);

	if (delta<0)
		printf("there are no roots\n");
	else
		if (delta==0)
			y=(-b/(2*a));
	printf("we have double roots x1=x2=%d/2*%d=%f\n",a,b,y);
		else
			if (delta>0)
				x1=((-b+sqrt(delta))/(2*a));
	x2=((-b+sqrt(delta))/(2*a));
	printf("there are two roots which are %f and %f\n",x1,x2);

	return 0;	     	 
}
im using notepad
and command line Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

i downloaded "Microsoft Visual C++ 2008 Express Edition"
though i'm ashamed to admit that i didn't know how to use it..

PS: thanks for your effort by indenting but i'm still not noticing the prob..
i'm reading the notes ive taken from the net over and over again..
im pretty sure there's something im missing but im not finding it out..

Last edited by rollyah; 04-30-2010 at 12:19 PM.
rollyah is offline   Reply With Quote
Old 04-30-2010, 12:24 PM   #11
The Beautiful C++ Utopia
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,455
Code:
#include <stdio.h>
#include <math.h>
int main (void)
{
	int a,b,c;
	float x,x1,x2,y,delta;
	printf("enter three numbers please\n");
	scanf("%d%d%d",a,b,c);
	delta=b*b-4*a*c;
	if (a==0)
		x=(-c/b);
	printf("the root is %f\n",x);
	else 
		printf("Delta=b2-4ac is equal to %f\n",delta);

	if (delta<0)
		printf("there are no roots\n");
	else
		if (delta==0)
			y=(-b/(2*a));
	printf("we have double roots x1=x2=%d/2*%d=%f\n",a,b,y);
		else
			if (delta>0)
				x1=((-b+sqrt(delta))/(2*a));
	x2=((-b+sqrt(delta))/(2*a));
	printf("there are two roots which are %f and %f\n",x1,x2);

	return 0;	     	 
}
Problematic lines are in red.
We see from the indentation that, for example, the printf lines do not belong to the IF statement. Braces are required if you want more than one statement in an if.
As a result of this, the else does not directly follow the if, and therefore, the compiler cannot determine which if statement it belongs to and so it complains.
There are many good tutorials on Visual Studio on the web.
Even if you don't use it, you shouldn't use Notepad for creating code. Either use a real editor or an IDE.
__________________
WARNING: Any and all code samples I post are not tested unless explicitly mentioned otherwise. Use at your own risk.
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Why did the Java creators shoot themselves in the foot?
Elysia is offline   Reply With Quote
Old 04-30-2010, 12:37 PM   #12
Registered User
 
Join Date: Apr 2010
Posts: 7
Quote:
Originally Posted by Elysia View Post
Code:
#include <stdio.h>
#include <math.h>
int main (void)
{
	int a,b,c;
	float x,x1,x2,y,delta;
	printf("enter three numbers please\n");
	scanf("%d%d%d",a,b,c);
	delta=b*b-4*a*c;
	if (a==0)
		x=(-c/b);
	printf("the root is %f\n",x);
	else 
		printf("Delta=b2-4ac is equal to %f\n",delta);

	if (delta<0)
		printf("there are no roots\n");
	else
		if (delta==0)
			y=(-b/(2*a));
	printf("we have double roots x1=x2=%d/2*%d=%f\n",a,b,y);
		else
			if (delta>0)
				x1=((-b+sqrt(delta))/(2*a));
	x2=((-b+sqrt(delta))/(2*a));
	printf("there are two roots which are %f and %f\n",x1,x2);

	return 0;	     	 
}
Problematic lines are in red.
We see from the indentation that, for example, the printf lines do not belong to the IF statement. Braces are required if you want more than one statement in an if.
As a result of this, the else does not directly follow the if, and therefore, the compiler cannot determine which if statement it belongs to and so it complains.
There are many good tutorials on Visual Studio on the web.
Even if you don't use it, you shouldn't use Notepad for creating code. Either use a real editor or an IDE.
ah that thts the prob..
i checked all my notes i didnt have anything concerning "braces are required if theres more than a statement"
thanks for the info..
i'm gonna look into mroe tutorials online..
PS: i were able to compile it but it's crashing..
any logical error that you can notice?
don't point it out i want to try and find it out on my own if possible..
sorry for being pushy..
rollyah is offline   Reply With Quote
Old 04-30-2010, 12:45 PM   #13
The Beautiful C++ Utopia
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,455
All I can say is that it's not a logic problem that causes your crash.
I could elaborate on where and why, if you want, but seeing as you did state you want to find it yourself, you'll have to make another reply if you want it.
Take your time.
__________________
WARNING: Any and all code samples I post are not tested unless explicitly mentioned otherwise. Use at your own risk.
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Why did the Java creators shoot themselves in the foot?
Elysia is offline   Reply With Quote
Old 04-30-2010, 12:51 PM   #14
Registered User
 
Join Date: Apr 2010
Posts: 7
Quote:
Originally Posted by Elysia View Post
All I can say is that it's not a logic problem that causes your crash.
I could elaborate on where and why, if you want, but seeing as you did state you want to find it yourself, you'll have to make another reply if you want it.
Take your time.
i commented everything to be able to pinpoint the prob..

Code:
#include <stdio.h>
//#include <math.h>
int main (void)
{
	int a,b,c;
	//float x,x1,x2,y,delta;
	printf("enter your first number	please\n");
	scanf("%d",a);
	printf("enter your second number please\n");
	scanf("%d",b);
	printf("enter your third number	please\n");
    scanf("%d",c);
	printf("%d,%d,%d",a,b,c);
	//delta=(b*(b-(4*a*c)));
	//if (a==0)
	    //{
		//x=(-c/b);
	    //printf("the root is %f\n",x);
		//}
	
	//else 
		//printf("Delta=b2-4ac is equal to %f\n",delta);

	//if (delta<0)
	//	printf("there are no roots\n");
	//else
	//if (delta==0)
	//{
	//		y=(-b/(2*a));
	 //       printf("we have double roots x1=x2=%d/2*%d=%f\n",a,b,y);
//			}
//	else
//	if (delta>0)
//			x1=((-b+sqrt(delta))/(2*a));
//	        x2=((-b+sqrt(delta))/(2*a));
//	printf("there are two roots which are %f and %f\n",x1,x2);

	return 0;	     	 
}
and it's still crashing..
what did i do wrong?
rollyah is offline   Reply With Quote
Old 04-30-2010, 12:53 PM   #15
The Beautiful C++ Utopia
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,455
scanf requires pointers to where it should store the input. You are merely passing the contents of the variables to scanf.
Aside from that, you should probably be getting warnings in some form or another.
__________________
WARNING: Any and all code samples I post are not tested unless explicitly mentioned otherwise. Use at your own risk.
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Why did the Java creators shoot themselves in the foot?
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Which Library Files for these unreferenced functions? lehe C++ Programming 3 01-31-2009 10:30 PM
Including lib in a lib bibiteinfo C++ Programming 0 02-07-2006 02:28 PM
<Gulp> kryptkat Windows Programming 7 01-14-2006 01:03 PM
pointers InvariantLoop C Programming 13 02-04-2005 09:32 AM
Struct *** initialization Saravanan C Programming 20 10-09-2003 12:04 PM


All times are GMT -6. The time now is 08:28 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22