Do...While loop

Do...While loop

Post by Mendax_Sane on Tue Nov 30, 2010 11:27 am
([msg=49652]see Do...While loop[/msg])

So I've been watching some of the CS50 Computer Science lectures/trying the problem sets but for Problem set 1 I can't get a Do while loop to work - I've tried loads of different ways but I either end up with it just saying "number too low" an infinite amount of times or the target number starts to chancge

Code: Select all
    GNU nano 2.2.2                                              File: skittles1.c                                                                                                 


#include <cs50.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(void)
{
    // seed PRNG
    srand(time(NULL));
   
    //pick psuedorandom number in [0, 1023]
    int skittles = 4;
   
    // TODO
    printf("There are between 0 and 1023 skittles!\n");
    printf("Try to guess how many skittles there are!\n");
    int n = GetInt();
    do
    {
    GetInt();
            if(n==skittles)
            printf("Well Done!\n");
        else if(n>=0&&n<skittles)
            printf("Your number was too low.\n");
        else if(n>skittles&&n<1024)
            printf("your number was too high.\n");
        else
            printf("you chose an invalid number.\n");   
    }
    while(n!=skittles);
    return 0;

}



- its supposed to keep going until you get the right number, telling you whether its too high or too low each time.
Any help is appreciated - I've spent quiet floundering around on this now :oops:
User avatar
Mendax_Sane
New User
New User
 
Posts: 19
Joined: Sun Aug 01, 2010 3:32 pm
Blog: View Blog (0)


Re: Do...While loop

Post by Rizzle on Tue Nov 30, 2010 12:06 pm
([msg=49653]see Re: Do...While loop[/msg])

I never really did sit down and learn C++ but I have done enough programming to debug some code. Just throwing that out there.

It looks like you have stumbled upon an infinite loop using the do while. If you are trying to set up the variable n equal to the function, as in you want to fill the skittles jar, You don't want to fill the jar every time you run the loop waiting for the right answer. Define the variable outside of the loop and then have a checker function to compare the guessed number to the variable n.

essentially some pseudocode hashed up on the spot....
Code: Select all
start
define n = GetInt()
   do loop
     Prompt user input
     Compare user input to var "n"
   while input not = to "n"
end loop When user input is = to "n"
output congratulatory message
end or go back to start and redefine new variable


hope that helps
Rizzle
New User
New User
 
Posts: 3
Joined: Tue Jun 24, 2008 12:31 pm
Blog: View Blog (0)


Re: Do...While loop

Post by Mendax_Sane on Tue Nov 30, 2010 12:57 pm
([msg=49656]see Re: Do...While loop[/msg])

I finally got it working - thanks for the help :D
Now onto part 2...
User avatar
Mendax_Sane
New User
New User
 
Posts: 19
Joined: Sun Aug 01, 2010 3:32 pm
Blog: View Blog (0)


Re: Do...While loop

Post by Rizzle on Tue Nov 30, 2010 2:41 pm
([msg=49661]see Re: Do...While loop[/msg])

If you are looking to show off a bit and also make it a bit easier for the end user to guess you could always add a list of previously guessed answers. Probably just have to define a list and fill it with items. You can just tag that to the end of your loop before it goes back to the beginning and lets the user try again. The output of the list would be near the beginning of the loop.

Go for the extra credit points!
Rizzle
New User
New User
 
Posts: 3
Joined: Tue Jun 24, 2008 12:31 pm
Blog: View Blog (0)


Re: Do...While loop

Post by thetan on Tue Nov 30, 2010 4:43 pm
([msg=49671]see Re: Do...While loop[/msg])

no, that wouldn't be showing off, or even fun.

If you want to have fun and show off write an algorithm that generates a psuedo random number and that guesses it in as little guesses as possible.

Hint: you should be able to do this logarithmically and binary search like

EDIT: or better yet write a program that makes you mentally choose a number between 1-5,000, and guesses your number in less than 13 guesses (it should always guess correctly and never miss by it's 13'th guess), on every guess it would present a number and have 3 options:
1: this is greater then my number
2: this is less then my number
3: this is my number
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: Do...While loop

Post by Rizzle on Wed Dec 01, 2010 12:42 am
([msg=49698]see Re: Do...While loop[/msg])

my suggestion would be easy enough for a newcomer to programming but your idea sounds much more intriguing, i may have to pick up a compiler and start playing because of what you just said. it's been far too long.
Rizzle
New User
New User
 
Posts: 3
Joined: Tue Jun 24, 2008 12:31 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests