First Program printing problem

Hello,

I wrote my first program, and although the first printing is ok
the second one is something like "!!"and letters after that, and I have no idea why. If it helps at all, i'm also posting my code down there. It's supposed to be hangman-game
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  #include <iostream>
#include <string.h>
#include <process.h>
using namespace std;

int main(){
	void hangman(int n);
	int i,s=0,flag=0,n=1,right=0;
	char word[21],guess[21],letter;
	
	cout << "Enter the word\n";
	cin >> word;
	for (i=0;i<24;i++){
		cout << "\n";
	}
	hangman(n);
	for (i=0;i<20;i++){
		word[i]=0;
	}
	
	for (i=0;i<20;i++){
		if (word[i]!=0){
			s=s+1;
		}
	}
	
	for (i=0;i<s;i++){
		guess[i]='*';
	}
	
	while (n<7 && right==0){
		
	right=1;
	
	cout << "Type a letter\n";
	cin >> letter;
	for(i=0;i<s;i++){
		if (word[i]=letter){
			
			flag=1;
			guess[i]=letter;
		}
	}
	
	if (flag=1){
		cout << guess;
	}
	else {
		n=n+1;
		hangman(n);
	}
	for (i=0;i<s;i++){
		if (guess[i]='*'){
			right=0;
		}
	}
	
}
    cout << flag;
	return 0;
}

void hangman(int n){
	if (n=1){
		cout << "++----\n";
	    cout << "|\n";
	    cout << "|\n";
	    cout << "|\n";
	    cout << "\n";
	}
	else if (n=2){
	    cout << "++----\n";
	    cout << "|\to\t\t\t\n";
	    cout << "|\n";
	    cout << "|\n";
	    cout << "\n";
	}
	else if (n=3){
		cout << "++----\n";
	    cout << "|\t\t\t\to\n";
	    cout << "|\t\t\t\t|\n";
	    cout << "|\n";
	    cout << "\n";
	}
		else if (n=4){
		cout << "++----\n";
	    cout << "|\t\t\t\to\n";
	    cout << "|\t\t\t/|\n";
	    cout << "|\n";
	    cout << "\n";
	}
		else if (n=5){
		cout << "++----\n";
	    cout << "|\t\t\t\to\n";
	    cout << "|\t\t\t/|\\n";
	    cout << "|\n";
	    cout << "\n";
	}
		else if (n=6){
		cout << "++----\n";
	    cout << "|\t\t\t\to\n";
	    cout << "|\t\t\t/|\\n";
	    cout << "|\t\t\t/\n";
	    cout << "\n";
	}
		else if (n=7){
		cout << "++----\n";
	    cout << "|\t\t\t\to\n";
	    cout << "|\t\t\t/|\\n";
	    cout << "|\t\t\t/\t\\n";
	    cout << "\n";
	}
	
	}
Last edited on

Where to begin??

1
2
3
4
int i,s=0,flag=0,n=1,right=0;    // Seriously?

int i,s,flag,right=0;
int n=1;


1
2
3
4
5
6
7
8
9
10
char word[21],    // Okay, so word is a char?
word[i]=0;         // Yet you assign an integer value...

if (word[i]=letter){    // = assigns a value,  == compares values.

if (flag=1){     // again   ==

if (guess[i]='*'){  and yet again

n=n+1;   is  n++;



I picked your code apart like that, not to be mean or embarrass you. This looks very much like my first program, you're in good company. :) Your code is very hard to understand, and even harder to read.

Try to get away from using variables like s, i... It's always a lot easier to remember variables that have meaningful names like flag... even though I have no clue what flag does.
which brings me to my last tip... commenting your code. A few comments telling what 's' does, saves a whole lot of scrolling to see how it's used and guessing what it is supposed to do.

+1 point for consistent indenting.
Topic archived. No new replies allowed.