Change from C++ to C

hello there my friends,

I would like to know what i have to change to make this code run for C and not C++ which runs perfectly fine. Here is the code. Thank you for your time. I'd appreciate any help!
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
 
using namespace std;
 
string THE_WORD;
int wrong;
string soFar;
string used;
 
bool match(char letter, string word);
char askGuess(string usedLettersStr);
bool playAgain();
 
int main()
{
    srand(time(0));
 
    vector<string> words;
    words.push_back("ACHIVEMENT");
    words.push_back("ABDICATION");
    words.push_back("ABSOLUTELY");
    words.push_back("BLACKJACKS");
    words.push_back("MAXIMIZING");
    words.push_back("JACKHAMMER");
    words.push_back("OBJECTIVES");
    words.push_back("MOBILIZING");
    words.push_back("HYPNOTIZED");
    words.push_back("EMPATHIZED");
    words.push_back("EXCELLENCY");
    words.push_back("EMPHASIZED");
    words.push_back("VAPORIZING");
    words.push_back("BANKRUPTCY");
    words.push_back("PREJUDICED");
    words.push_back("PROJECTING");
    words.push_back("FREQUENTLY");
    words.push_back("EXPENDABLE");
    words.push_back("BLACKBERRY");
    words.push_back("ACCOMPLISH");
 
    cout << "Welcome to Hangman.  Good luck!\n";
 
 
 bool done = false;
do
 {
    const int MAX_WRONG = 10;
    wrong = 0;
 
    random_shuffle(words.begin(), words.end());
    THE_WORD = words[0];
 
    soFar = string(THE_WORD.size(), '-');
    used = "";
    while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
    {
        cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
        cout << "\nYou've used the following letters:\n" << used << endl;
        cout << "\nSo far, the word is:\n" << soFar << endl;
 
        used += askGuess(used);
 
 
    }
 
    if (wrong == MAX_WRONG)
    {
      cout << "\nYou lost, as a result you've been hanged!";
    }
 
    else {
     cout << "Fascinating! You won. Congratulations!!\n";
    }
     cout << "\nThe word is " << THE_WORD << endl;
 
} while(playAgain());
 
return 0;
}
 
inline bool match(char letter, string word)
{
       return ( word.find(letter) != string::npos );
}
 
char askGuess(string usedLettersStr)
{
        char guess;
        cout << "\n\nEnter your guess: ";
        cin >> guess;
        guess = toupper(guess);
        while (match(guess, used))
        {
            cout << "\nYou've already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }
 
        if (match(guess, THE_WORD))
        {
            cout << "That's right! " << guess << " is in the word.\n";
 
            for (int i = 0; i < THE_WORD.length(); ++i)
                if (THE_WORD[i] == guess)
                    soFar[i] = guess;
        }
        else
        {
            cout << "Sorry, " << guess << " isn't in the word.\n";
            ++wrong;
        }
return guess;
}
 
bool playAgain()
{
     char again;
     cout << "\n\nWould you like to play again? <y/n>: ";
     cin >> again;
 
     cin.clear();
     cin.ignore();
 
     again = toupper(again);
 
     system("cls");
 
     return (again == 'Y');
}
Nope, you could almost copy any pure C code, but you still need to add in the headers.
what i have to change to make this code run for C and not C++
Everything. You will have to rewrite your code from scratch if you want to make it C code.
can anyone suggest my a written C code for hangman with the same specifications as this one?
This is my project from my university I wrote the program to C++ and my fucking professor doesn't accept anything except C code. So I have to figure out something fast!
Just convert it from C++ to C, you are taking the class you should have the fundamentals down for c. We aren't going to do that for you.

Also, if your professor only accepts c why code it in c++ :P
Last edited on
Well for starters C will have different #includes, and almost 85% of your code will have to be rewritten. And the resulting C program will also be longer.
http://stackoverflow.com/questions/737257/how-to-convert-c-code-to-c
http://www.parashift.com/c++-faq/convert-to-c.html
Last edited on
Okay a friend of mine gave me this code. but there is something wrong.
can anyone help me out and find what is wrong with that code?

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
/* is it in the word? */
int in_word(char, char*);
int
in_word(char z, char* word) {
        int i = 0;
        while(word[i] != '\0') {
                if(word[i] == z) return 1;
                i++;
        }
        return 0;
}
 
 
/* random number generation */
int RNG(int, int);
int
RNG(int min, int max) {
    srand(time(NULL));
    int res = rand();
    while(res < min || res > max) {
        srand(time(NULL));
        res = rand();
    }
   
    return res;
}
 
 
/* --- */
int show_guessed(char*, char*);
int
show_guessed(char* word, char* correct) {
         char pavles[20];
         int i      = 0;
         int length = 0;
         
         while(word[i] != '\0') {
                 pavles[i] = '-';
                 i++; length++;
         }
         
         pavles[i] = '\0';
         i         = 0;
         while(correct[i] != '\0') {
                 int j = 0;
                 while(word[j] != '\0') {
                         if(word[j] == correct[i])
                                pavles[j] = correct[i];
                         j++;
                 } i++;
         }
         
         printf("Current word : %s\n", pavles );
         i = 0;
         while(pavles[i] != '\0') {
                 if(pavles[i] == '-') return 0;
                 i++;
         }
         return 1;
}
 
 
/* :D */
void show_mistake(int);
void
show_mistake(int mistake) {
        if(mistake == 0) {
                printf("    +-------+\n");
                printf("    |       |\n");
                printf("            |\n");
                printf("            |\n");
                printf("            |\n");
                printf("            |\n");
                printf("    ============\n");
        }
        else if(mistake == 1) {
                printf("    +------+\n");
                printf("    |      |\n");
                printf("    o      |\n");
                printf("           |\n");
                printf("           |\n");
                printf("           |\n");
                printf("   ============\n");
        }
        else if(mistake == 2) {
                printf("    +------+\n");
                printf("    |      |\n");
                printf("    o      |\n");
                printf("    |      |\n");
                printf("           |\n");
                printf("           |\n");
                printf("   ============\n");
        }
        else if(mistake == 3) {
                printf("    +------+\n");
                printf("    |      |\n");
                printf("    o      |\n");
                printf("   /|      |\n");
                printf("           |\n");
                printf("           |\n");
                printf("   ============\n");
        }
        else if(mistake == 4) {
                printf("    +------+\n");
                printf("    |      |\n");
                printf("    o      |\n");
                printf("   /|\\    |\n");
                printf("           |\n");
                printf("           |\n");
                printf("   ============\n");
        }
        else if(mistake == 5) {
                printf("    +------+\n");
                printf("    |      |\n");
                printf("    o      |\n");
                printf("   /|\\    |\n");
                printf("   /       |\n");
                printf("           |\n");
                printf("   ============\n");
        }
        else if(mistake == 6) {
                printf("    +------+\n");
                printf("    |      |\n");
                printf("    o      |\n");
                printf("   /|\\    |\n");
                printf("   / \\    |\n");
                printf("           |\n");
                printf("   ============\n");
        }
        else if(mistake == 7) {
                printf("    +------+\n");
                printf("    |      |\n");
                printf("    o      |\n");
                printf("   /|\\    |\n");
                printf("   / \\    |\n");
                printf("         --|\n");
                printf("   ============\n");
        }
        else if(mistake == 8) {
                printf("    +------+\n");
                printf("    |      |\n");
                printf("    o      |\n");
                printf("   /|\\    |\n");
                printf("   / \\    |\n");
                printf("       ----|\n");
                printf("   ============\n");
        }
        else if(mistake == 9) {
                printf("    +------+\n");
                printf("    |      |\n");
                printf("    o      |\n");
                printf("   /|\\    |\n");
                printf("   / \\    |\n");
                printf("     ------|\n");
                printf("   ============\n");
        }
        else if(mistake == 10) {
                printf("    +------+\n");
                printf("    |      |\n");
                printf("    |  o   |\n");
                printf("   /|\\    |\n");
                printf("   /|\\    |\n");
                printf("    |------|\n");
                printf("   ============\n");
        }
}
 
 
/* main function */
int
main() {
        char correct[100];
        char words[20][10] = { "ACHIVEMENT", "ABDICATION", "ABSOLUTELY", "BLACKJACKS",
                               "MAXIMIZING", "JACKHAMMER", "OBJECTIVES", "MOBILIZING",
                               "HYPNOTIZED", "EMPATHIZED", "EXCELLENCY", "EMPHASIZED",
                               "VAPORIZING", "BANKRUPTCY", "PREJUDICED", "PROJECTING",
                               "FREQUENTLY", "EXPENDABLE", "BLACKBERRY", "ACCOMPLISH" };
 
 
        int correct_i = 0;
        int mistake   = 0;
        int all_      = 0;
        char letter   = '\0';
        char word[10]; strcpy(words[ RNG(0, 19) ], word);
        printf("Welcome. You have only 10 tries.\n");
 
        while(all_ == 0 && mistake < 10) {
                printf("Type a letter: ");
                scanf(" %c", &letter);
               
                if(!  in_word(letter, correct)
                   && in_word(letter, word)
                ) {
                        correct[correct_i] = letter;
                        correct_i++;
                        correct[correct_i] = '\0';
                       
                        all_ = show_guessed(word, correct);
                }
                else { mistake++; show_mistake(mistake); }
        }
        if(mistake == 10) printf("--HANGED: '%s'\n", word );
        else printf("++WIN\n");
 
        return 0;
}
can anyone help me out and find what is wrong with that code?

You could ask your friend.
Go to the C forum...
Okay a friend of mine gave me this code.

Does that mean you plan to turn in plagarized code?

If you know C and C++, it's pretty straightforward to port this code to C, but you'll have to go through it line by line.

Did the professor specify C code up front? If not then I think you should argue the point with him/her. That would certainly be the path of least resistance. If they refuse to take the C++ program then ask for an extension based on the unclear directions and the fact that you have a working program. If the prof did specify C up front then ouch: consider it a valuable lesson in reading the instructions carefully.
i don't know C well, but you may try this:
you can use a 2d char array to store the words.
then using nested loop you can shuffle them.

you can also search c libraries:
http://www.cplusplus.com/reference/clibrary/

It is not possible to change c++ to C code as it is. becaue there are different header files used in both languages. you can change code from c++ to C, but you need to make a small change in code.
in your friends code,
#include <string.h> is required to use strcpy() and the format used in strcpy is wrong, see correct format here: http://www.cplusplus.com/reference/cstring/strcpy/

use srand(time(NULL)); only once in your program.
RNG function is wrong. use res = rand()%(max-min+1) + min; instead.


another beginners thing: char ch[5]; allows you maximum 4 chars + null char at the end. but that code used [10] for 10 length string!!!
wow! i didn't know that C doesn't support bool b= true; !

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
// Hangman Game 
// use CAPITALS/lowercase
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h> 


int main() {
		srand(time(NULL)); 
		int strlength =10;
		int nosTry=10; // max wrong try
		int min=0, max=19;

char words[20][11] = { "ACHIVEMENT", "ABDICATION", "ABSOLUTELY", "BLACKJACKS",
                               "MAXIMIZING", "JACKHAMMER", "OBJECTIVES", "MOBILIZING",
                               "HYPNOTIZED", "EMPATHIZED", "EXCELLENCY", "EMPHASIZED",
                               "VAPORIZING", "BANKRUPTCY", "PREJUDICED", "PROJECTING",
                               "FREQUENTLY", "EXPENDABLE", "BLACKBERRY", "ACCOMPLISH" };
                               
	
	int R = rand()%(max-min+1) + min; // min to max
	char hypens[11] = "----------";
	int correct = 0;
	int mistake = 0;        
	char letter;  
	printf("Welcome. You have only %d wrong tries.\n", nosTry);
	int Bool; // true/false 1/0

	while(1){
		printf("Type a letter: \n");
		scanf(" %c", &letter);
		if(letter>='a' && letter<='z'){
			letter -= 'a'- 'A';   //convert input to uppercase  
		}
		
		Bool=0;
		for(int i=0; i<strlen(words[R]); i++){
			if(words[R][i] == letter){
				correct++;
				hypens[i] = letter;
				Bool=1;
				printf("%s\n",hypens);
			}			
		}
		
		if(Bool==0) {
			mistake++;
			printf("mistakes = %d\n",mistake);
		}
		

		if(correct== strlen(words[R])){
			printf("\n\nyou Won!\n");
			break;
		} 
		if(mistake>=nosTry){
			printf("\n\nyou Lost! The word is %s\n", words[R]);
			break;	
		} 
		
	} 

	return 0;
}
I didn't know that C doesn't support bool b= true;

Of course it does, you just need to #include <stdbool.h> and make sure your compiler is not configured to use some prehistoric version of C (for gcc, I use -std=c11 or -std=c99)
Topic archived. No new replies allowed.