read input string

I hv an assignment to translate words enter by user.

I have done some code and it is work to translate single letter enter by user... e.g: the user enter "suka" will be translate to "like" because in the txt file (i make as a database file and it is linked with cpp file) I already put the word and translated word..so that each time it read the word entered it will output the translated word as provided...

but I having problem in how I can modify the code so that it can translate all words entered. e.g: the user enter "saya suka" that have two words will be output "I like"...currently, it will translate or match the 1st word only...

plz help me how to modify the code so that it will read all word/letter enter by user include with space....


#include <iostream>
#include <cstdio>
#include <fstream>
#include <D:\ctt\translation.txt>
using namespace std;

int main() {

int i;
char str[80];


ifstream in("translation.txt");
if(!in) {
cout<<"cannot open file\n";
return 1;
}

in>>str;

in>>i;

cout<<"Enter Malay word to translate: \n";
cout<<"============================== \n\n";
cout<<"\t";
cin>>str;
cout<<"\n\n";



for (i=0; i<10000; i+=2)

if (!strcmp(str, numbers[i]))
{
cout<< "English word is : \n"<<"================================\n\n\n"<<"\t"<<numbers[i+1]<<" "<<"\n\n\n\n\n";
break;

}



if(i==10000) cout<< "Not found\n\n\n\n\n\n\n\n";


in.close();

return 0;

}
Ok... confused.

The code as posted above does not compile, so how is it you are able to run? Or are you just saying that you think it will do what you said?

Your first problem is that you only ever read the first word from translation.txt. I assume that the file looks like:


saya I
suka like


If so, you have to read two words from each line and store them in an array such that you can look up the word the user entered and find the corresponding translation. Once you've figured out how to read all the lines in the file, you essentially have what you need to do the second part; you'll just need a loop (that loops until the user wants to quit).

It really work....ok the code is as below

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
#include <iostream>
#include <cstdio>
#include <fstream>
#include <D:\ctt\translation.txt>
using namespace std;

int main() {

int i;
char str[80];


ifstream in("translation.txt");
if(!in) {
cout<<"cannot open file\n";
return 1;
}

in>>str;

in>>i;

cout<<"Enter Malay word to translate: \n";
cout<<"============================== \n\n";
cout<<"\t";
cin>>str;
cout<<"\n\n";



for (i=0; i<10000; i+=2) 

if (!strcmp(str, numbers[i])) 
{
cout<< "English word is : \n"<<"================================\n\n\n"<<"\t"<<numbers[i+1]<<" "<<"\n\n\n\n\n";
break;

}



if(i==10000) cout<< "Not found\n\n\n\n\n\n\n\n";


in.close();

return 0;

}


...and te translation.txt is as below (just as an example)

char numbers[][80]= {
"saya", "I",
"suka", "like",
"makan", "eat",
"nasi", "rice",
}


this translation.txt is diferent file from cpp and it is linked.

I hv try to use looping (eg. while / do while) but it failed and the code as follow...
1
2
3
4
5
6
7
8
9
10
11
12
13
14

for (i=0; i<10000; i+=2) 
	do {

		if (!strcmp(str, numbers[i])) 
		{
		cout<< "English word is :   \n"<<"================================\n\n\n"<<"\t"<<numbers[i+1]<<" "<<"\n\n\n\n\n";
		break;
		i+1;
		}

	}while ( i+=2 && i<10000);



I have no idea to modify this code so that it can read next character input by user....plz help me....thank a lot cause give me a respond .... :)
Topic archived. No new replies allowed.