#include
#include
#include
#include
using namespace std;
Char en [16], ch [16].
Int score=0;
Int a, [3].
int n;
The class word
{
Private:
Char eng [16].
Char chi [16].
Char e2 [16].
Public:
Word (char e [16], char c [16])//constructor
{
Strcpy (eng, e);
Strcpy (chi, c);
Strcpy (e2, e);
}
Void Dis (int I)//whether fill in the blanks the correct
{
Char x;
For (int flag=0;; )
{
Cin> x;
If (x==eng [a] [I]) {cout<& lt;" Good!" & lt; & lt; Endl; Score +=5; break; }
The else
{
Cout<& lt;" Try again?" & lt; & lt; Endl;
Flag++;
If (flag<=10 | | flag> 3) {cout<& lt; Chi [a] [I]; }
Else if (flag> 10) {cout<& lt;" Oh my god!" & lt; & lt; Endl; break; }
}
}
}
Void deal (char [] a);
Void the display ();
};
Void word: : display ()//output function
{
int i;
If (nIf (n<=7 & amp; & N>=5) I=1;
If (n>=) I=3;
Else I=2;
Cout<& lt; E2 & lt; & lt; Endl;
int j;
The switch (I)
{
Case (0) : Dis (0); break;
Case: (1) for (j=0; J<2; J++)
Dis (j);
break;
Case (2) : the for (j=0; J<3; J++)
Dis (j);
break;
Case (3) : the for (j=0; J<4. J++)
Dis (j);
break;
}
}
Void word: : deal (char []) b//hide the words of a letter
{
Srand (time (NULL));
N=strlen (b);
Cout<& lt; B<& lt;" "& lt; & lt; n<& lt; Endl;
a [0]=rand () % n;
E2 [a [0]]='_'.
If (strlen (b) & gt; 4)
{
Retry: a [1]=rand () % strlen (b);
If (a [1]==a [0]) goto retry.
The else e2 [a [1]]='_'.
}
If (strlen (b) & gt;=8)
{
Retry1:
A [2]=rand () % strlen (b);
If (a [2]==a [0] | | a [2]==a [1]) goto retry1;
The else e2 [a [2]]='_'.
}
If (strlen (b) & gt;=11)
{
Retry2:
A [3]=rand () % strlen (b);
If (a [3]==a [2] | | a [3]==a [1] | | a [3]==a [0]) goto retry2;
The else e2 [a [3]]='_'.
}
}
Void word_add ()//add new words
{
Char new_word [32].
Ofstream output (" new. Dat ", the ios: : app);
Cout<& lt;" Please enter the word to add (format for English Chinese) : "& lt; & lt; Endl;
Cin> New_word;
Output<& lt; New_word & lt; & lt; Endl;
}
Void readi (char * en, char * ch, int I)//read the words I
{
Ifstream infile (" English ");
While (I -)
{
Infile. Getline (en, 16);
Infile. Getline (ch, 16);
}
Infile. Close ();
}
Void game ()
{
Srand (time (NULL));
for(int i=0; i<20; I++)
{
Readi (en, ch, rand () % 20);
Cout<& lt; En<& lt; Endl;
Word w1 (en, ch);
W1. Deal (en);
/* w1. The display (); */
}
}
Int main ()
{
Int number;
Cout<& lt;" \ n \ n \ n \ n \ n "& lt; & lt; Endl;
Cout<& lt;" \ t * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * "& lt; & lt; Endl;
Cout<& lt;" \ t * * * * * * * * * * * * * * * * * * * * * * * * "& lt; & lt;" Word games "& lt; & lt;" * * * * * * * * * * * * * * * * * * * * * * * * * * * "& lt; & lt; Endl;
Cout<& lt;" \ t * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * "& lt; & lt; Endl;
Cout<& lt;" 1. Start game "& lt; & lt; Endl;
Cout<& lt;" 2. Add the words "& lt; & lt; Endl;
Cout<& lt;" 0. The end of the game "& lt; & lt; Endl;
Cout<& lt;" Please enter the option: "& lt; & lt; Endl;
Cin> Number;
system("cls");
While (cin. The get ()!=0)
{
The switch (number)
{
Case (0) : cout<& lt;" Good bye!" & lt; & lt; Endl; exit(1);
Case (1) : game ();
Case (2) : word_add (); The exit (1);
}
} return 0;
}
Compile error free run 1 error debugging shows that the above stated when the line intger means I'm strange strange by 0
CodePudding user response:
Modulus is division, if n=0 will cause in addition to the zero error,CodePudding user response:
Debugging when I tried to the commented outShow n is not equal to 0
CodePudding user response:
Int a, [3].A [3]=rand () % strlen (b); Crossing the line,
Other did not check,
CodePudding user response:
Then how to modify? I don't know much about c + + cross-border all heard what haven't seenCodePudding user response:
Int a, [3].//array defines a here, only three elements,A [3]=rand () % strlen (b); Crossing the line,//C/C + + array subscript begins with 0, a [3] is the fourth element,
You only defines the three elements, is assigned to the fourth element, this behavior is called crossing the line, can produce unpredictable behavior,
Change the method is very simple,
Int a, [4].//define the four elements of an array, you can use a [3]