# while loops...a problem with my simple code?

Printable View

• 02-20-2008
niceguy
while loops...a problem with my simple code?
I think there may be a problem with my usage of brackets below.
Any ideas on this simple program to find roots of quadratic equation?

many thanks!

include
Code:

``` #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { char answer; int a,b,c; float single_root, root_part1, root_part2;//declaring function variables these                                           //being different elements of the                                           //quadratic formula float root1, root2; int is_complex; do printf("Enter coeficients a b c: "); scanf("&#37;f %f %f", &a, &b, &c); is_complex = 0;  //(is_complex) function = 0 if a=0     if (a==0)   {   single_root=-1.0*c/b;//stating variables   }     else     {   root_part1 = -1.0*b/(2.0*a); //stating variables   root_part2= b*b-4.0*a*c;         if (root_part2<0.0)       {                               is_complex = 1;             root_part2= -1.0*root_part2;//remember this is not equal to,                                   //this is a directive       }                         root_part2=sqrt(root_part2)/(2.0*a);                   }                     if(a==0.0)                   {         Printf("single_root = %f\n ", single_root);//single root case       }                     else if  (is_complex==1)                     {       printf("root 1 = %.2f+%.2f i\n", root_part1, root_part2);       printf("root 1 = %.2f-%.2f i\n", root_part1, root_part2);       }                     else                     {       root1= root_part1+root_part2;        root2= root_part1-root_part2;                 printf("root1 = %.2f", root1);   printf("root2 = %.2f", root2);   }                 {   printf("enter next set of coefficients (y/n): ");   scanf(" %c", &answer);   }             while (answer != 'n' && answer != 'N');             scanf("d\n");    exit(0);   }```
• 02-20-2008
tabstop
You can only have one statement between a do and a while. If you need more than one statement (which you often do) you have to turn that group of statements into a single complex statement, using the curly braces:
Code:

```do { lots; of; statements; here; } while (etc)```
You should also pay attention to the warnings you're getting about scanf and the undefined variable "Printf".
• 02-20-2008
niceguy
but i dont understand this undeclared variable warning about print f?

is it a bracket problem?
• 02-20-2008
tabstop
Quote:

Originally Posted by niceguy
but i dont understand this undeclared variable warning about print f?

is it a bracket problem?

No. Note how "Printf" causes problems and "printf" works just fine.
• 02-20-2008
dwks
C is case sensitive. "printf" is not the same thing as "Printf". Only "printf" works. (Nearly all standard library functions and identifiers are in lowercase, except things like _Bool.)

Code:

`while (answer != 'n' && answer != 'N');`
Consider tolower() or toupper() from <ctype.h>.
• 02-20-2008
Elysia
As an optional suggestion, indenting a little bit better will not hurt. http://cpwiki.sf.net/User:Elysia/Identation
It will make the code easier to read and make it easier to spot bugs as well (or avoiding them)!
• 02-20-2008
dwks
I'm actually wondering if it was all on one line or something . . . .
Quote:

Last edited by Dave_Sinkula : Today at 12:04 PM. Reason: My eyes tire of the wide view.
This, of course, does nothing useful:
Code:

`scanf("d\n");`
• 02-20-2008
niceguy
Thanks Elysia, wil cleanup the indentation.

It was indeed the capital P in Printf that was wrong. However with the console running now it is just not going further than asking for a,b,c. So am still stuck.

The scanf("d/n") just holds the console on my compiler!
• 02-20-2008
matsp
Quote:

Originally Posted by dwks
Code:

`scanf("d\n");`

Not only does it _NOT_ do anything useful, but it's quite possibly going to cause a crash on a modern system, as it takes the next word down on the stack as an address to write an integer value to. If this address is for example the return address or the frame-pointer, it will lead to very bad things. If it's "only" a local variable [and this contains a valid memory address], you'll have strange results. In short, very strange things could happen here, and very few of the potential scenarios are on the good side of "nothing".

--
Mats
• 02-20-2008
Elysia
So what compiler do you use?
• 02-20-2008
dwks
Quote:

Originally Posted by matsp
Not only does it _NOT_ do anything useful, but it's quite possibly going to cause a crash on a modern system, as it takes the next word down on the stack as an address to write an integer value to. If this address is for example the return address or the frame-pointer, it will lead to very bad things. If it's "only" a local variable [and this contains a valid memory address], you'll have strange results. In short, very strange things could happen here, and very few of the potential scenarios are on the good side of "nothing".

--
Mats

You may not have noticed that there is no %; it's just "d\n". As far as I can tell, it requires the user to enter "d" before continuing.

Note to OP: /n is not the same thing as \n.
• 02-20-2008
niceguy
Thanks Guys!

The console holding code i required now is

system(pause)

It works now cheers!
• 02-20-2008
Elysia
A better way would be to use getchar() instead :) More portable.
• 02-20-2008
dwks