-
July 21st, 2004, 11:06 AM
#11
AFAIK, atoi() isn't in string.h. I've been compiling all my tests in gcc without string.h and it works fine. If you're not getting a message about undeclared identifiers, then it probably isn't string.h, it'll be the way it has been coded.
[edit]atoi() is in stdlib.h[/edit]
ac
-
July 21st, 2004, 07:27 PM
#12
well, i've tried your code like this:
Code:
char mychar;
for(get_answer=0;get_answer<=4;get_answer++)/*get the answer given*/
{
mychar=(char)getchar();
answer[get_answer]=atoi(&mychar);
/*scanf("%d",&answer[get_answer]);*/
printf("\n");
}
and it works but now i've got another problem:
when i enter '12345', and it prints my input, it prints '01234', why is this? can i solve this by adding 1 to each number i put in, or is there another solution to this?
-
July 22nd, 2004, 12:12 AM
#13
The answer is to not use getchar() I think we should resign ourselves to the fact that getchar isn't going to work in your problem. I think you either need to get the user to enter the whole number and scanf() it, then parse the individual numbers from the input, or you need to compile on a windows system and use getch() (or rather, compile using a non-standard compiler).
ac
-
July 22nd, 2004, 10:53 AM
#14
i see what you mean, but if the user would enter 12345 as an integer, how can i make sure that i've got all the number seperated?
would it be something like:
Code:
scanf("%d",&myint);
answer[0]=myint/10000;
answer[1]=(myint/11000)*2;
answer[2]=(myint/11100)*3;
etc...
don't mind the alghoritm, cause it will not work if user inputs something else then 12345,
but is it possible to solve it like this?
-
July 23rd, 2004, 08:07 PM
#15
i've altered my code and it seems to work, but it doesn't
can someone please tell me why???
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define false 0
#define true 1
int main()
{
srand( (unsigned)time( NULL ) );
int play_or_instruct;
int get_random;
int max_tries;
int get_answer;
int print_answer;
int count_equal;
int correctplace=0;
int run,a;
int correctdigit=0;
int secret_code[5];
int answer[5];
int check[5];
printf("\nWelcome to mastermind.\n");
printf("press 1 to play, press 2 for instructions :");
scanf("%d",&play_or_instruct);
if (play_or_instruct==2) /*check for playing the game or instructions*/
{
printf("\n\nThe goal of the game is to guess the secret\n");
printf("secret code. You have 10 tries for this.\n");
printf("The code contains 5 random numbers from 0 - 9.\n");
printf("Every \'*\' you see next to your input, \n");
printf("means that you have one number correct and\n");
printf("at the right place.\n");
printf("Every \'0\' you see next to your input,\n");
printf("means that you have one number correct, but\n");
printf("not at the right place.\n");
play_or_instruct=1;
}
if (play_or_instruct ==1)
{
printf("\nHere we go....\n\n");
for (get_random = 0; get_random <5; get_random++) /*get the secret code with the*/
secret_code[get_random] = rand()%10; /*random function*/
for(max_tries=0;max_tries<=10;max_tries++) /*count number of tries*/
{
printf("\nEnter your guess: ");
for(get_answer=0;get_answer<=4;get_answer++)/*get the answer given*/
{
scanf("%d",&answer[get_answer]);
}
printf("\n");
for(print_answer=0;print_answer<=4;print_answer++)/*print the answer*/
printf("%d",answer[print_answer]);/*given to the screen*/
printf("\t");
check[get_answer] = false;
for (get_answer = 0; get_answer < 5; get_answer++)
{
if (answer[get_answer] == secret_code[get_answer])
{
correctplace++;
check[get_answer] = true;
}
}
for (get_answer = 0; get_answer < 5; get_answer++)
{
for (run = 0; run < 5; run++)
{
if ((answer[get_answer] == secret_code[run]) && (check[run] == false))
{
correctdigit++;
check[run] = true;
}
}
}
if(correctplace ==5)
{
printf("\nCongratulations, you've won!\n");
return 0;
}
for(;correctplace >=0;correctplace--)
printf("*");
for(;correctdigit >=0;correctdigit--)
printf("0");
printf("\n");
if(max_tries==10)/*if maximum number of tries have passed, let them know*/
{ /*they didn't made it and end the game*/
printf("\nUnfortunately you did not make it.\n");
return 0;
}
}
}
return 0;
}
especially the part where the *'s and 0's should be printed..
-
July 23rd, 2004, 10:50 PM
#16
Hey man, can you do us a favour and send a worked example. As in just post an example of what it should look like (pretend you're playing the game and type up as a post what you would see on the screen) That'll make it more obvious what's going wrong.
ac
-
July 23rd, 2004, 11:06 PM
#17
You need to use special chars to display special chars with printf
And I think what you need to do is %' instead of just ' to print it out correctly. Similarily, cout uses \ to display such characters. I recommend you review the syntax of some of the more basic functions and always have a good referrence book [or two] handy while coding, especially when bugs show up.
/ \\
-
July 23rd, 2004, 11:16 PM
#18
Hey man, can you do us a favour and send a worked example. As in just post an example of what it should look like (pretend you're playing the game and type up as a post what you would see on the screen) That'll make it more obvious what's going wrong.
the above code is a working example, but if you want i can compile it for you and put it in as an attachment, tell me what you want, linux or windows?
as for the things that go wrong, when you enter 12345, it could give an output like this:
12345 *0
if you then enter 11111
it might give an output like:
11111 0
that's what i mean, how is it possible when i enter 11111 it says that there is one digit correct, but not at the right place??
and more of these strange things happen...
You need to use special chars to display special chars with printf
And I think what you need to do is %' instead of just ' to print it out correctly. Similarily, cout uses \ to display such characters. I recommend you review the syntax of some of the more basic functions and always have a good referrence book [or two] handy while coding, especially when bugs show up.
i don't think i do, because if you compile the above source, it does print *'s and 0's ,but not like they should in order to let the game work, the program is working though, just not like the game should....
hope this clears things up...
-
July 24th, 2004, 12:11 AM
#19
Okay it wasn't clear for me, I guess Dev uses a different way than gcc maybe... or maybe I'm wrong.
It seems as a bug, so I can't give you a quick answer, I'll need to take some time and review the code.
/ \\
-
July 25th, 2004, 10:23 AM
#20
well, i've compiled it with DEV-C++ too, and the program works with it, (on windows), just the same as on linux, and with also the same problem, program works, but not like the game is intended...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|