Thread: can you help me where is my mistake ?? please this is my homework last day:(

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    Unhappy can you help me where is my mistake ?? please this is my homework last day:(

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    int main(void){
    int number1;
    int numberofestimates;
    
    
    
    
    
    
    number1= (1 + rand()%1000);
    printf( "Enter your estimate, -1 to end: " );
    scanf( "%d", &numberofestimates );
    
    
    
    
    while (numberofestimates!=-1)
    {
    printf("I have a number between 1 and 1000.Can you guess my number?Please type your first guess.Enter your estimate, -1 to end");
    fflush(stdout);
    scanf("%d\n",&numberofestimates);
    
    
    
    
    if (number1<numberofestimates)
    {
    printf("Too high. Try again.");
    fflush(stdout);
    scanf("%d\n",&numberofestimates);
    break;
    }
    if (number1>numberofestimates)
    {
    printf("too low.try again.");
    fflush(stdout);
    scanf("%d\n",&numberofestimates);
    
    
    break;
    }
    if (number1==numberofestimates)
    {
    printf("Excellent! You guessed the number! ");
    printf("do you want to play again please write a new guess ");
    break;
    }
    }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Your first error would be posting this C program in the C++ forum.

    Next fflush() is not designed to be used with input streams.

    Lastly asking specific questions would be advised.

    Jim

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Ask yourself when do you wish to break out of the while loop?
    Now ask yourself why do you have 3 breaks in your code?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    im sorry to post wrong
    i can't speak english very well
    thanks

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It would be helpful if you provided more information, such as the exact problem you're having.
    (How To Ask Questions The Smart Way)

    1. "numberofestimates" is a misleading variable name - "guess" might be better.
    2. Get rid of the "\n" in the "scanf()" strings.
    3. Get rid of the "break" statements for the "less-than" and "greater-than" conditions - you want to keep looping in that instance, not break out of it.
    4 Your logic needs re-structuring. Draw it out on paper first. For instance, you ask for a guess, then enter the loop, then ask for a guess again. You print the main message (which should be above the loop, not in the loop) each time the loop runs, and ask for a guess twice with each iteration.

    [edit] Three posts occurred while I was typing!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is my mistake
    By Dave Couture in forum C Programming
    Replies: 8
    Last Post: 08-14-2012, 10:28 PM
  2. mistake of using awk
    By lehe in forum Linux Programming
    Replies: 6
    Last Post: 04-02-2009, 04:41 PM
  3. What is my mistake ?
    By Freelander1983 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2007, 09:31 AM

Tags for this Thread