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

This is a discussion on while loops...a problem with my simple code? within the C Programming forums, part of the General Programming Boards category; I think there may be a problem with my usage of brackets below. Any ideas on this simple program to ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    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);
       
    }
    Last edited by niceguy; 02-20-2008 at 01:18 PM. Reason: My eyes tire of the wide view.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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".

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    but i dont understand this undeclared variable warning about print f?


    is it a bracket problem?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by niceguy View Post
    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.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    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)!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I'm actually wondering if it was all on one line or something . . . .
    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");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    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!

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dwks View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    So what compiler do you use?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by matsp View Post
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Thanks Guys!

    The console holding code i required now is

    system(pause)

    It works now cheers!

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    A better way would be to use getchar() instead More portable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. Odd problem in main(), code won't proceed
    By aciarlillo in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2005, 11:00 PM
  3. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  4. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 10:34 AM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM

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