Program doesnt run properly

Apr 28, 2013 at 2:59pm
Hi. I wanted to test if I have successfully
understood simple classes so I wrote this and
now I think I haven't succeed in learning classes!
Here's my main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include "dict.h"

int main()
{
    string entered;
    
    cout << "Enter your word: \n";
    cin >> entered;
    
    DictWord sad(entered);
    cout << sad.translatedWord();
}


And here's my dict.h:

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
 
#include <iostream>
#include <string>
using namespace std;

class DictWord
{
private:
  string inputWord;
  string outputWord;
  string tranWord;
  
public:

    DictWord(string enteredWord)
    {
        setWord(enteredWord);
    }

    void setWord(string enteredWord)
    {
        inputWord = enteredWord;
    }

    string getWord()
    {
        setTranslate(inputWord);
        return inputWord;
    }

     void setTranslate(string gotWord)
    {
        if (gotWord == "apple") tranWord = "sib";
        else tranWord = "Not found!";
    }

    string translatedWord()
    {
        return tranWord;
    }

    };


This program builds and runs but the translatedWord()
which was supposed to return "sib" or "Not found!", returns null string.
Couold you please tell me where the problem is.
thanks in advance.
Apr 28, 2013 at 3:27pm
Line 12: You are calling constructor of DictWiord, which calls setWord member function. It sets inputWord equal to enteredWord.

Line 13: YOu are calling translatedWord member function, which returns tranWord. However that variable was never set, so it returns an empty string.
Make setWord function call setTranslate to achieve something.
Apr 28, 2013 at 3:28pm
You never call your translate function.
Apr 28, 2013 at 3:28pm
The function setTranslate() is not called.
Apr 28, 2013 at 3:44pm
everything under

void setTranslate(string gotWord)

never gets called.
Apr 28, 2013 at 3:45pm
I thought it would be called itself! thanks. my problem is solved now.
Topic archived. No new replies allowed.