Like Tree3Likes
  • 2 Post By jimblumberg
  • 1 Post By Matticus

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

This is a discussion on can you help me where is my mistake ?? please this is my homework last day:( within the C++ Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include <stdlib.h> int main(void){ int number1; int numberofestimates; number1= (1 + rand()%1000); printf( "Enter your estimate, -1 to ...

  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
    2,698
    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
    stahta01 and Elysia like this.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,521
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    2,244
    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!
    stahta01 likes this.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 08:31 AM

Tags for this Thread


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