Results 1 to 4 of 4

Thread: Disordered code? uh?

  1. #1

    Thread Starter
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986

    Question

    Ok, I know there was a question about this earlier, but I can't find it anymore, sorry.

    I'm making a mastermind game for a school assignment, it's almost working, but...

    My code isn't runnin' in the order I write it?
    I think I coded it ok, but it doesn't seem to work very well...

    here's the code
    PHP Code:
    //MasterMind game by Jop

    #include <iostream.h>
    #include <stdlib.h>
    #include "declarations.h"
    #include <time.h> 


    void main(int argcchar **argv)
    {
        
    MMResult sResult;
        
    int iy;

    //Input numbers:
        /*
        cout << "Player1: Please enter 4 (1 digit) numbers:\n";
        for(y=0;y<4;y++)
        {
            cout << "Number " << y << ": ";
            cin >> sResult.iGoodNum[y];
        }
        cout << "\nThank you, now put player2 behind your computer\n";
        system("cls");
        */

        //Random number generator :)
       
    srand( (unsigned)timeNULL ) ); 
        
       for(
    y=0;y<4;y++)
       {
           
    sResult.iGoodNum[y] = rand() % 10;
               
    //cout << sResult.iGoodNum[y] << "\n";
       
    }

    while(
    sResult.iCorPos != 4)
    {
    sResult.iCorPos 0;
    sResult.iCorNum 0;
        
    system("cls");
        
    cout << "\nPlayer2, enter 4 numbers\n";
        for(
    y=0;y<4;y++)
        {
            
    cout << "Number " << << ": ";
            
    cin >> sResult.iNum[y];
        }
        
        
    cout << "\nThank you, here are the results:\n\n";

    //Calculate score
    for (y=0;y<4;y++)
    {
        if (
    sResult.iNum[y] == sResult.iGoodNum[y]) //Correct pos!
        
    {
            
    cout << sResult.iNum[y] << ": Good number, good pos\n";
            
    sResult.iCorPos sResult.iCorPos++; //increment counter
            //break;
        
    }
        else 
    //Check if number is correct but at wrong pos
            
    {
                    for (
    i=0;i<4;i++)
                    {
                        if (
    sResult.iNum[y] == sResult.iGoodNum[i]) //Good Num, wrong pos
                        
    {
                            
    cout << sResult.iNum[y] << ": Good number, wrong pos\n";
                            
    sResult.iCorNum sResult.iCorNum++; //increment counter
                            
    continue;
                        }
                    }
                    
            }    
        
    }
    cout << "\n" << sResult.iCorPos << " correct number(s) at correct position(s)\n";
    cout << sResult.iCorNum << " correct number(s) at wrong position(s)\n";

    }
        
    cout << "\nCongratulations! You've won! woooooooh!\n";
        
    system("pause");

    Now for example at the last line, system("pause")
    is executed before cout << "\nCon..."
    also the system("cls") is not executed where I write it in the code...

    So there must be some problem, does it execute aynchronously or something?
    are there any substitus for those system thingies?

    Thanks!
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  2. #2

    Thread Starter
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    hmm ok, those \ aren't working withing the Php code tag, so here it is withing the normal code tag

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include "declarations.h"
    #include <time.h> 
    
    
    void main(int argc, char **argv)
    {
    	MMResult sResult;
    	int i, y;
    
    //Input numbers:
    	/*
    	cout << "Player1: Please enter 4 (1 digit) numbers:\n";
    	for(y=0;y<4;y++)
    	{
    		cout << "Number " << y << ": ";
    		cin >> sResult.iGoodNum[y];
    	}
    	cout << "\nThank you, now put player2 behind your computer\n";
    	system("cls");
    	*/
    
    	//Random number generator :)
       srand( (unsigned)time( NULL ) ); 
    	
       for(y=0;y<4;y++)
       {
    	   sResult.iGoodNum[y] = rand() % 10;
    		   //cout << sResult.iGoodNum[y] << "\n";
       }
    
    while(sResult.iCorPos != 4)
    {
    sResult.iCorPos = 0;
    sResult.iCorNum = 0;
    	system("cls");
    	cout << "\nPlayer2, enter 4 numbers\n";
    	for(y=0;y<4;y++)
    	{
    		cout << "Number " << y << ": ";
    		cin >> sResult.iNum[y];
    	}
    	
    	cout << "\nThank you, here are the results:\n\n";
    
    //Calculate score
    for (y=0;y<4;y++)
    {
    	if (sResult.iNum[y] == sResult.iGoodNum[y]) //Correct pos!
    	{
    		cout << sResult.iNum[y] << ": Good number, good pos\n";
    		sResult.iCorPos = sResult.iCorPos++; //increment counter
    		//break;
    	}
    	else //Check if number is correct but at wrong pos
    		{
    				for (i=0;i<4;i++)
    				{
    					if (sResult.iNum[y] == sResult.iGoodNum[i]) //Good Num, wrong pos
    					{
    						cout << sResult.iNum[y] << ": Good number, wrong pos\n";
    						sResult.iCorNum = sResult.iCorNum++; //increment counter
    						continue;
    					}
    				}
    				
    		}	
    	
    }
    cout << "\n" << sResult.iCorPos << " correct number(s) at correct position(s)\n";
    cout << sResult.iCorNum << " correct number(s) at wrong position(s)\n";
    
    }
    	cout << "\nCongratulations! You've won! woooooooh!\n";
    	system("pause");
    }
    Thanks
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Your problem is buffering. What happens is, you may tell it to print "\n", but in reality it will print it at some time later when the buffer is full. So, if you use:
    Code:
    cout << endl;
    then that prints a newline and flushes the buffer.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Thanks I'll try that mike!
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width