Thread: I could use a little help

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    5

    I could use a little help

    Im working on this program and most of it works except it has a few little problems. I cant pinpoint what is causing them, and I am kind of new to C, although I do have extensive experience with other languages. Anyway im hoping someone here can help, for one the loop is ending when just one of the two variables equals zero but both of them should have to equal one. And the other thing is its not stoping at the first getchar() it just goes straight to player2. Heres the code it has

    Code:
    int Roll(int nMark)
    {
        int nDie1;
        int nDie2;
        int nRoll;
    	int nP1Total = 0;
    	int nP2Total = 0;
    	char cP1Cont;
    	char cP2Cont;
    	int nP1Continue;
    	int nP2Continue;
    
    
    	printf("What would you like to roll too?: ");
    	scanf("%d", &nMark);
    	printf("\n\nThe object of this game is for two players to take turns throwing the dice. \n Each player can continue to throw trying to get the total of the throws \n as close to %d as possible.\n ", nMark);
    	
        system("PAUSE");
    
    
    	while((1 != nP1Continue) && (1 != nP2Continue))
    	{
    		if (nP1Continue != 1)
    		{
    			printf("\n\nPlayer 1, get ready to roll the dice. ");
    			system("PAUSE");
    					nDie1 = (rand() % 6) + 1;
    					nDie2 = (rand() % 6) + 1;
    				printf("\n\tFirst Die: %d", nDie1);
    				printf("\n\tSecond Die: %d", nDie2);
    			nP1Total = nP1Total + nDie1 + nDie2;
    				printf("\nPlayer 1, you stand at %d.  Do you wish to continue rolling?(y/n): ",nP1Total);
    				cP1Cont = getchar();
    
    				if (('n' == cP1Cont) || ('N' == cP1Cont))
    				{
    					nP1Continue = 1;
    				}
    
    		}
    		if (nP2Continue != 1)
    		{
    			printf("\nPlayer 2, get ready to roll the dice. ");
    			system("PAUSE");
    					nDie1 = (rand() % 6) + 1;
    					nDie2 = (rand() % 6) + 1;
    				printf("\n\tFirst Die: %d", nDie1);
    				printf("\n\tSecond Die: %d", nDie2);
    			nP2Total = nP2Total + nDie1 + nDie2;
    				printf("\nPlayer 2, you stand at %d.  Do you wish to continue rolling?(y/n): ",nP2Total);
    				cP2Cont = getchar();
    
    				if (('n' == cP2Cont) || ('N' == cP2Cont))
    				{
    					nP2Continue = 1;
    				}
    		}
    
    	}
    
    
    
    }
    some of the code may seem redundant but thats because i have been trying to fix these problems various ways. I appreciate any help you can provide thanks.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    sorry a quick correction i said that it ends when one of the vaiables equals zero but that should be it ends when one of the variables equals one. also i apologize about it being so wide i didnt realise my code was going to cause that.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Re. the getchar() problem: that scanf() you have will take the line of input and convert it to an integet, but it will leave a newline character in the buffer, which will subsequently get read by getchar().
    To solve this, change the format string to "%d ". This is not really the optimal solution, but it will be good enough for your purposes.

    As for the problem of the loop ending, note that && evaluates to false if either or both of its operands is false, thus, supposing that nP1Continue = 1 and nP2Continue = 0:

    Code:
    while((1 != nP1Continue) && (1 != nP2Continue))
    while((1 != 1) && (1 != 0))
    while((0) && (1))
    while(0)
    And so the loop ends.

    Oh, and using system("pause"); is a bad habit to get into.
    Last edited by zx-1; 10-08-2006 at 05:02 PM.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Ok i see what you are saying but now it stops on the first scanf then skips all the other ones. Also note that i changed the getchar()'s to scanf's like scanf("%d", &cP2Cont);

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Ok i fixed it, but in the most inneficent way possable. I didnt quite get what you said the first time i read it but then i saw the thing about the buffer so i decided to add a getchar(); before the scanf and it worked. anyway thanks alot for your help.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If you want to have headaches and buggy code, use scanf before you think you're capable of implementing its underlying functionality.

    If you want to make life easy and correct, use fgets for all user input, and use sscanf or other conversion functions if need be.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    I will keep that in mind in the future Dave_Sinkula.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This advice isn't for programmers in the intermediate or advanced stages, it's for when you're starting out.

    User Input: Strings and Numbers [C]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed