plz tell me where the error lies!

This is a discussion on plz tell me where the error lies! within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<stdlib.h> #include<time.h> #include <conio.h> int main(void) { int num; int randnum; int tries; char choice = 'y'; randnum ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    1

    Question plz tell me where the error lies!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include <conio.h>
    
    
    
    
    
    
    
    
    int main(void)
    {
         int num;
         int randnum;
         int tries;
    
    
         
         char choice = 'y';
         
         
    
    
         randnum = 1 + rand() % 1000;
         tries = 1;
    
    
         printf("I have a number between 1 and 1000.\n");
         printf("Can you guess it?\n");
         printf("Please type your guess: ");
         scanf("%d", &num);
         printf("\n\n");
    
    
        do {    
              while(num != randnum)
              {
                   if(num < randnum)
                   {
                        printf("Too low. Try again.\n\n");
                        printf("Next guess:\t ");
                        
                        scanf("%d", &num);
                        printf("\n");
                   }
    
    
                   if(num > randnum)
                   {
                        printf("Too high. Try again.\n");
                        printf("Next guess: \t");
                       
                        scanf("%d", &num);
                        printf("\n"); }
    
    
                   tries ++;}
                    
                   if(num == randnum)
                   {
                    printf("Excellent! You guessed the number with %d tries!\n\n", tries );
                                            
                    printf("Would you like to play again (y or n)? ");
                    scanf("%c", &choice);}}
        while(choice == 'y');
                    
    
    
        getchar();
    
    
         return 0;
        }


    ANOTHER OPTION I AM TRYING IS TO USE goto

    Code:
    if (choice == 'y') {
    goto loop; }
    where loop is at the begging of the code

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your spacing and placement of braces is terrible. Fix that and someone might help you, or you may even see your own mistake.

    Also, don't just say "error in code", say exactly what's going wrong: what do you expect it to do, what does it actually do?

    And don't use a goto.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    By definition, setting choice to 'y' before the loop is unnecessary, as a do-while loop will always execute the body at least once, and then continue based on the condition.

    You should also fix the newline character buffering issue, your scanf() calls never deal with the '\n' left in the input buffer, and you don't fix this with subsequent getchar() calls like you should.

    Fix these issues, and fix the indentation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CPU temp lies?
    By -=SoKrA=- in forum Tech Board
    Replies: 1
    Last Post: 09-10-2003, 06:36 AM
  2. Top 11 Lies Comp Sci Majors Tell Themselves
    By joshdick in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-03-2003, 04:04 AM
  3. Self-Esteem = lies of equality!
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 10-08-2002, 08:10 PM
  4. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 03:39 PM

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