• Forum
  • Lounge
  • how can i code an app that accepts or re

 
how can i code an app that accepts or rejects numbers

Hello, I am coding an application and I actually got the concept from a game I used to play on players. Don't really know the real name but I called it " dead & injured". Now it uses numbers and must be two payers. The two players must select a random 4 digit number ranging from 0 - 9 and no number should be repeated. The goal is the predict the number of your opponent. If the your opponent selected 2479 and you send 2974 the app is suppose to reply you 2 dead, 2 injured. If the numbers you guessed are in the same position and is the exact number of your opponent it becomes a "dead" and if you guess a number but it is not in the same position but the number exists in your opponents numbers it becomes an " injured". I tried using the IF & ELSE & WHILE statements but I keep getting errors. I'll be so greatful if some can help me out.

Thanks all
Last edited on
Can you post the if/else/while statements you've tried, and the actual text of errors you're getting?

Edit: Gah, didn't realize this was in Lounge. MOVE THIS TO BEGINNERS OR GENERAL C++. Don't post C++ questions in Lounge!
Last edited on
Why do people do this? What makes people look at the list of forums here, and skip over all the ones that say "Programming" in the title, and then post their programming question to the one forum that isn't about programming?

I mean, even if one were the kind of lazy, selfish person who couldn't even make the tiny bit of effort to look at what the forums are for before posting - surely, there's nothing about the name "Lounge" that makes it stand out among all the other forum names as the one that's the best one to post your programming questions in, is there?
Last edited on
@kokpidama
Click [edit] to edit your post.
At the top, click [edit topic].
Select “Beginners” or “General C++ Programming” from the drop-down menu.
[Save]

@yous other guys
Be nice. MB, I’d report you for abuse if it would make any difference. People make mistakes. Asking them nicely to fix it while showing an interest in actually helping them goes a long way.

@kokpidama
I did not see your original posting, so I do not know if you had your code there. It is hard to help without specific code, input, output, and errors.

Without that information, I can do nothing more than suggest some high-level thoughts.

 • Do not use integers. Use strings. This will make your life so much easier.
 • Write a function that returns the number of times a character is found in a string.
 • Write a function that validates a string has exactly four characters and no character is repeated.
   You can use the last function to help. Use a loop for every possible character ('0' through '9').
 • Write a function that takes two strings as argument and returns the number of “dead” characters.
   This is probably the easiest function to write.
 • Write a function that takes two strings as argument and returns the number of “injured” characters.
   Remember, a character that is found in the other string may be either injured or dead, so count up
   the number of characters found in the other string and subtract the number of dead.

Hope this helps.
Hello @kokpidama,

I guess you've received the message that we'd prefer this direct coding question in the "Beginners" or "General C++ programming" sections. Take that as done: we look forward to seeing it there in due course.

Anyway ... when I was a child we called this game "Mastermind" and we played it with coloured pegs, rather than 4-digit codes. The guesses were marked with black pegs for right-and-in-the-correct-position (what you call "dead") and white pegs for remaining-pegs-that-were-right-but-in-the-incorrect-position (what you call "injured"). So, here's a skeleton for such a game. I've written a main() routine, but you would have to write the functions indicated.

Note that I've used vector<int> to hold my 4-digit code, rather than @Duthomas's suggestion of strings. Either would do. Both would be slightly more flexible and easy to copy than normal arrays of ints or chars.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>           // for srand() and rand()
#include <ctime>             // for time() (to seed srand())
using namespace std;

using vec = vector<int>;     // saves writing vector<int> lots of times

vec generateCode( int N );
vec getGuess( int N );
void checkGuess( vec code, vec guess, int &black, int &white );

//======================================================================


int main()
{
   int N = 4;
   srand( time( 0 ) );
   vec code, guess;

   code = generateCode( N );
   for ( int d : code ) cout << d;   cout << endl;         // only for cheating/checking

   int black = 0, white = 0, num = 0;
   while ( black < N )
   {
      num++;
      guess = getGuess( N );
      checkGuess( code, guess, black, white );
      cout << "Black: " << black << "     White: " << white << "\n\n";
   }
   cout << "You took " << ( num < 10 ? "a brilliant " : "an abysmal " ) << num << " goes to solve";
}


//======================================================================


vec generateCode( int N )              // returns an N-digit code as a vector<int>
{
   vec code( N );
   vector<bool> seen( 10, false );     // indicates which digits 0-9 I have used so far

   // Your code goes here
   // ...

   return code;
}


//======================================================================


vec getGuess( int N )                  // Get a string from the user and convert to vector<int>
{                                      // Use s[i] - '0' to convert character s[i] to a digit
   string s;
   vec guess( N );

   // Your code goes here
   // Get a string from the user
   // Convert to a vector<int>

   return guess;
}


//======================================================================


void checkGuess( vec code, vec guess, int &black, int &white )
{                                      // Compares guess with code and returns numbers of
                                       //    black: right and in right place
                                       //    white: right but in wrong place
   // Count blacks (right and in the right place)
   // Your code goes here
   // Maybe set used values of code and guess to negative numbers to avoid examining later
   // ...

   // Count whites (right but in the wrong place)
   // ...
}


//====================================================================== 


Have fun. This game was the first one that I ever programmed ... in BASIC ... on a Sinclair ZX Spectrum!
Last edited on
Registered users can post here. Sign in or register to post.