translating the code in pure C

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
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
void randomize(char []);
void Guess(char [], int&,int&);
void main()
{
    clrscr();
    char word[7]={'\0'};
    long int x;
    int b,score=0,ctr2=0;
    time_t t;
    srand((unsigned)time(&t));
    ifstream inFile;
    b:
    inFile.open("words.txt");
    if(inFile.fail())
    {
        cout<<"The file was not opened succesfully\n";
        exit(1);
    }
    inFile.seekg(0L,ios::beg);
    x=rand()%1000;
    inFile.seekg(x);
    inFile>>word;
    if(strlen(word)<6)
    {
        inFile.close();
        goto b;
    }
    cout<<word<<"\n\n\n";
    inFile.close();
    randomize(word);
    Guess(word,score,ctr2);
    cout<<"\n\n\t\tYou got a "<<score;

    getch();
 
}
 
void randomize(char y[7])
{
    char A[7]={'\0'};
    int i=0,x,k,j,z;
    int n[7]={'\0'};
    x=strlen(y);
        time_t t;
    srand((unsigned)time(&t));
    cout<<"\n\n\n\t\t\t\t";
    while(i<x)
    {
    a:
    j=rand()%x;
    z=0;
    for(k=0,z=0;k<i;k++)
        if(n[k]==j)
        z++;
 
    if(z!=0)
        goto a;
 
    else
        {
        n[i]=j;
        A[i]=y[j];
        cout<<A[i]<<" ";
        i++;

        }

    }

    return;

}
  
void Guess(char word[7],int &score,int &ctr2)
{
    int ctr;

        a:

    ctr=score;

    char guess[7]={'\0'};

    char quit[4]={'e','x','i','t'};

    cout<<"\n What is your guess? "<<ctr2;

    cin>>guess;

    if(strcmpi(guess,quit)==0);

        exit(1);

 

    ifstream inFile;

    inFile.open("words.txt");

    inFile.seekg(0l,ios::beg);

    while(!inFile.eof())

    {

        inFile>>word;

        if(strcmpi(word,guess)==0)

        {

            score+=strlen(guess)*10;

            cout<<"\t"<<strlen(guess)*10;

            //Delete(word);

            break;

        }

 

    }

    inFile.close();

    if(score==ctr)

    {

        cout<<"\nYou had a wrong guess\n\n Try again!!\n";

        goto a;

 

    }

    else if(score>ctr&&score<100)

    {
        cout<<"\n\n \t\tYou got it right"<<"\nGuess another word";

        goto a;

    }

    else

    {

        cout<<"\m \n\t\tCONGRATULATIONS\n\n You got all right ";

        return;

    }

}


Guys can anyone help me to translate this cod in pure C, because I really don't know c++.

Thanks for the reply guys.
Last edited on
It's just the I/O that's C++.

Replace the file object with FILE*. Open with fopen, close with fclose, seek with fseek and so on.

Replace cin with scanf, cout with printf.
Quick and dirty translation so there might be a lot of errors. I didn't analyze the code, just translate. BTW, don't use goto.

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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <conio.h>

void randomize(char*);
void Guess(char*,int*,int*);

void main()
{
  char word[256] = {'\0'};
  long int x;
  int b, score=0, ctr2=0;
  time_t t;
  FILE* inFile;
  srand((unsigned)time(&t));

b:
  inFile = fopen("words.txt", "r");
  if (inFile == NULL)
  {
    printf("The file was not opened succesfully\n");
    exit(1);
  }
  fseek(inFile, 0L,SEEK_SET);
  x = rand()%1000;
  fseek(inFile, x, SEEK_SET);
  fscanf(inFile, "%s", word);
  if (strlen(word)<6)
  {
    fclose(inFile);
    goto b;
  }

  printf("%s\n\n\n", word);
  fclose(inFile);
  randomize(word);
  score = ctr2 = 0;
  Guess(word,&score,&ctr2);
  printf("\n\n\t\tYou got a %d", score);
  getch();
}


void randomize(char* y)
{
  char A[256] = {'\0'};
  int i=0, x, k, j, z;
  int n[256]= {'\0'};
  time_t t;
  x = strlen(y);
  srand((unsigned)time(&t));
  printf("\n\n\n\t\t\t\t");
  while (i<x)
  {
a:
    j = rand()%x;
    z = 0;
    for (k=0,z=0; k<i; k++)
      if (n[k]==j)
        z++;

    if (z!=0) goto a;
    else
    {
      n[i] = j;
      A[i] = y[j];
      printf("%c ", A[i]);
      i++;
    }
  }
  return;
}


void Guess(char* word,int* score,int* ctr2)
{
  int ctr;
  FILE* inFile;
  char guess[256] = {'\0'};

a:

  ctr = *score;
  printf("\n What is your guess? %d ", *ctr2);
  scanf("%s", guess);

  if (strcmpi(guess,"EXIT")==0)
      exit(1);

  inFile = fopen("words.txt", "r");
  fseek(inFile, 0l, SEEK_SET);

  while (feof(inFile)==0)
  {
    fscanf(inFile, "%s", word);
    if (strcmpi(word, guess)==0)
    {
      *score += strlen(guess)*10;
      printf("\t%d", strlen(guess)*10);
      break;
    }
  }

  fclose(inFile);

  if (*score==ctr)
  {
    printf("\nYou had a wrong guess\n\n Try again!!\n");
    goto a;
  }
  else if(*score>ctr && *score<100)
  {
    printf("\n\n \t\tYou got it right\nGuess another word");
    goto a;
  }
  else
  {
    printf("\m \n\t\tCONGRATULATIONS\n\n You got all right \n");
    return;
  }
}


Thanks a lot Black Coder.. :P
Topic archived. No new replies allowed.