Help fixing bugs in program?

This is a discussion on Help fixing bugs in program? within the C Programming forums, part of the General Programming Boards category; I'm in the process of writing a program. I've written most of it, but there are a few things that ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    17

    Help fixing bugs in program?

    I'm in the process of writing a program. I've written most of it, but there are a few things that I can't seem to fix. Firstly, after the user inputs "h" or "t," the output gets displayed twice. Also, it seems as though, even if the computerpointcounter reaches 25, it will not display the message: "Computer won!." Can anyone help me with these? Thanks.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdio.h>
    int main () {
    	int userpointcounter=0, computerpointcounter=0,random=0;
    	char userpick;
    	
    	printf("Welcome to MindReader.\n\n");
    	
    	while(userpointcounter<25 || computerpointcounter<25)
    	{
    		printf("Guess heads or tails and I'll predict your guess.\nWhat is your guess [h/t] ?");
    		scanf("%c",&userpick);
    		int computerpick = srand( (unsigned)time( NULL ) %2);
    		if (computerpick == 1)
    		{
    			random='t';
    		}
    		if (computerpick == 0)
    		{
    			random='h';
    		}
    		if(userpick==random)
    		{
    			printf("Yes! I too predicted heads.\n");
    			userpointcounter++;
    		}
    		if(userpick!= random)
    		{
    			printf("No. I gussed the opposite.\n");
    			computerpointcounter++;
    		}
    	}
    	if(userpointcounter==25)
    	{
    		printf("Congratulations! You won!\n");
    	}
    	if(computerpointcounter==25)
    		printf("Computer won!\n");
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while(userpointcounter<25 || computerpointcounter<25)
    That should be && not ||. You want to continue while both of them are less than 25. As to it displaying output twice, it's because you are reading a single character from the keyboard, but you are inputting 2.

    T + ENTER

    There's a FAQ on the topic if you can't figure it out on your own.


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

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Tell you what...

    1) srand() does not produce random numbers.
    2) you really need to rethink the logic in your program... I could do that in about 20 lines.
    3) your while() loop probably doesn't behave like you think it does. (what happens if the computer never scores a point?)
    4) you should also know that many random number generators used with %2 output 0 and 1 alternately.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    17
    Quote Originally Posted by quzah View Post
    Code:
    while(userpointcounter<25 || computerpointcounter<25)
    That should be && not ||. You want to continue while both of them are less than 25. As to it displaying output twice, it's because you are reading a single character from the keyboard, but you are inputting 2.

    T + ENTER

    There's a FAQ on the topic if you can't figure it out on your own.


    Quzah.
    Could you point me to the place that will help me figure out the multiple input? Thanks.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by celticpride View Post
    Could you point me to the place that will help me figure out the multiple input? Thanks.
    Well, see, this is one of the challenges in programming... figuring things out on one's own mental horsepower.

    Approach it as a process... be the process... what do you have to do --in little tiny steps-- to play this game?
    Work it out on paper if you need to...
    Now cross out all the steps you don't really need.
    Next translate human to C... and there's your code.

    Well...
    I need to reset my scores to 0 for a new game.
    I need to decide T or F as the computer...
    Next I need to tell the user to take a guess...

    and so on...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Ctr+D and Few Bugs in Program
    By scaven in forum C Programming
    Replies: 2
    Last Post: 04-15-2003, 07:29 PM
  3. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  4. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 04:59 PM

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