Thread: why is my code not working from here it looks ok

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    9

    Unhappy why is my code not working from here it looks ok

    I'm new to coding and to me this code looks ok but it isnt returning the values it should. I really dont know how to correct this! Please help

    Code:
    # include <stdio.h>
    # include <conio.h>
    
    int main()
    {
        /* Variables */
        float root;
        float guess;
        float answer;
        answer = guess*guess;
        
          printf ("Please enter a number that you wish to guess the square root of  ");
          scanf ("%f",&root);
          printf ("\nPlease enter your guess  ");
          scanf ("%f", &guess);
          answer = guess*guess;
                
                
          while (answer == root) 
          printf ("\nWell done. your guess is exactly the square root");     
          if (answer < root)
          answer = guess*guess;
          {
          printf ("\nYour guess is lower than the square root of %f. Please enter a higher guess  ", root );
          scanf ("%f", &guess);
          if (answer > root)
          answer = guess*guess;
          printf ("\nYour guess is higher than the square root of %f. Please enter a lower guess  ", root );
          scanf ("%f", &guess);
          if (answer + 0.05 == root)
          answer = guess*guess;
          printf ("\nYour guess of %f is within 0.5 of the square root of %f  ", guess, root);
          scanf ("%f", &guess);
          if (answer - 0.05 == root)
          answer = guess*guess;
          printf ("\nYour guess of %f is within 0.5 of the square root of %f  ", guess, root);
          scanf ("%f", &guess);
          }
          getch();
          }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Lisa_townsend View Post
    I'm new to coding and to me this code looks ok but it isnt returning the values it should. I really dont know how to correct this! Please help
    Well, I know I'm confused. The first number you are asking for, is "root", but "root" is the number you'll be finding the sqr root *of*. Very confusing. Why not change that variable name to "number"?

    Then your while() loop never executes, because your condition is wrong. It should be while(answer != root), instead.

    After the guess is correct, you need a "continue" statement to stop further looping RIGHT NOW. A "break" statement, would have the same effect.

    Change those and you'll be very close.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    We have to give a number we want to find the square root of and guess what it is by a refinement method. I tried putting while(answer != root) instead and now when I enter the root then the guess it says well done I have guessed it correctly for any number. I have 3 different books in front of me and I cant find where I'm going wrong.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    first things first - stop calling the number you're inputting "root". It is NOT a root. That is the number that you need to find the square root OF.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    The variable is now been changed from root to number, now what?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    change the inital declaration of answer = guess * guess. Just int answer is fine.

    Then it's "while(guess * guess != answer)"

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    This is probably not what you intended:
    Code:
    if (answer < root)
          answer = guess*guess;
          {

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Remove all instances of this line of code:
    Code:
    answer = guess*guess;
    
    /* and add this line right after the start of the while() loop: */
    
    scanf ("%f", &guess);
    
    //remove all other instances of that line from inside the loop

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    I have made the changes, when I enter a guess of 3 for the square of 9 it says correct but I have entered the number I wanted to guess for of 9 again then entered a guess of 2 which came back saying its too low which is correct but when I enter the guess of 1 after it says the guess is too high. Then once another number is entered it says the new guess is within the square of 9 which is wrong so can you tell me why this is and how do I correct it.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Make the changes for post #8 in this thread. You posted at the same time.

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    I dont know what you mean by instances, do you mean delete all the lines of scanf ("%f", &guess); because I have tried this and it wont print anything after the first 2 numbers are entered.

    Could ya take my code and correct it for me please or advise me of a site which can correct it? I may as well be trying to correct chinese without any basis knowledge of it or something thats the best way I can explain trying to make this code out put what I want. Sorry to be a pain in the butt!

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, I mean delete all the scanf() lines, inside the while loop, except one. That one, goes right under the while() condition:

    Please post your revised program so I can refer to the latest version.

  13. #13
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    I have left the variables as they were so I know what they are, but you can take the code and change it to what you think is right. I called the first var root because its the square root I am guessing if ya know what I mean

    Code:
    # include <stdio.h>
    # include <conio.h>
    
    int main()
    {
        /* Variables */
        float root;
        float guess;
        float answer;
        answer = guess*guess;
        
          printf ("Please enter a number that you wish to guess the square root of  ");
          scanf ("%f",&root);
          printf ("\nPlease enter your guess  ");
          scanf ("%f", &guess);
          answer = guess*guess;
                
                
          while (answer == root) 
          scanf ("%f", &guess);
          answer = guess*guess;
          printf ("\nWell done. your guess is exactly the square root");     
          if (answer < root)
          answer = guess*guess;
          {
          printf ("\nYour guess is lower than the square root of %f. Please enter a higher guess  ", root );
          
          if (answer > root)
          answer = guess*guess;
          printf ("\nYour guess is higher than the square root of %f. Please enter a lower guess  ", root );
          if (answer + 0.05 == root)
          answer = guess*guess;
          printf ("\nYour guess of %f is within 0.5 of the square root of %f  ", guess, root);
          if (answer - 0.05 == root)
          answer = guess*guess;
          printf ("\nYour guess of %f is within 0.5 of the square root of %f  ", guess, root);
          }
          getch();
          }

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Lisa_townsend View Post
    I have left the variables as they were so I know what they are, but you can take the code and change it to what you think is right. I called the first var root because its the square root I am guessing if ya know what I mean
    Here's a better idea: Tell us if it does what you expect it to do / if you are having problems, what kind, and where specifically.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    Heres what I want to do:
    1). Choose a number to find the square root of lets say 9
    2). make a guess at what the square root could be, I know its 3 but lets say 2
    3). take the guess of 2 multiply it by itself then compare it to the square of 9
    4) If your guess is correct in this case if you choose 3 you would be correct and a well done message would come up
    5). If your guess is within 0.05 higher or lower than the square of 9 say you are close enough
    6). If your guess is less or more than 0.05 print message your guess is lower or higher than the square root please enter a higher or lower number

    so as I picked 2 which multiplied is 4 the message would say its lower than the root please enter a higher guess etc till you guess correctly or come within 0.05 of the square. All guesses should be displayed underneath each other until you find the number.

    I know this would be a simple program to a programmer but to someone who isnt computer literate or code literate Its like a foreign language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  2. Replies: 3
    Last Post: 02-24-2009, 08:49 PM
  3. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM
  4. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 12:00 PM