-
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 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");
}
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!
-
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
-
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:
then that prints a newline and flushes the buffer.
-
Thanks I'll try that mike!