Problem with values changing

This is a discussion on Problem with values changing within the C++ Programming forums, part of the General Programming Boards category; At first i would like to apologize for my bad English. Second, i have a problem with my C++ program. ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    10

    Problem with values changing

    At first i would like to apologize for my bad English.

    Second, i have a problem with my C++ program.
    I have class

    Code:
    class Trena   {
          public:
          string dromologia[6];
          int thesis[3];
          int kr_dr;
          int kr_thesis;
          int change;
        
          void initialize_t();
    in a hyper class Metaforika

    I set values to this with this code :

    Code:
    void Metaforika::Trena::initialize_t(){
         dromologia[1] = "patra athina";
         dromologia[2] = "patra pyrgos ";
         dromologia[3] = " patra thesalloniki";
         dromologia[4] = " patra lianokladi";
         dromologia[5] = "athina larisa";  
         thesis[1] = 6;
         thesis[2] = 12;
         thesis[3] = 0;
    BUT when i try to change the values kr_dr, kr_thesis through a function, the cannot be changed.

    Code:
    void Metaforika::kratisi(int x,int y,int z){
    
    Trena train;
    train.kr_dr =x;
    train.kr_thesis=y;
    it gives me values of "424577" ( not exactly like this )

    i've trying using a constructor
    Code:
    Metaforika::Trena::Trena(int y,int z) {
                                 
                                 kr_dr =y;
                                 kr_thesis=z;
                                 thesis[z]--;
    but same thing happens.

    Any help would be appreciated

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Arrays start at index 0, and the item at index [array_size] is the first one out of bounds.

    It is also possible that you are changing the values of a local and leaving the object you are interested in (member of Metaphorika?) uninitialized. You might post something more complete and be more careful with indexing arrays.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    As you told me, i initialize kr_dr and kr_thesis.

    Code:
    void Metaforika::Trena::initialize_t(){
         dromologia[1] = "patra athina";
         dromologia[2] = "patra pyrgos ";
         dromologia[3] = " patra thesalloniki";
         dromologia[4] = " patra lianokladi";
         dromologia[5] = "athina larisa";  
         thesis[1] = 6;
         thesis[2] = 12;
         thesis[3] = 0;
         kr_dr=0;
         kr_thesis=0;
    I have another class called Ploio.

    THIS code give me the fault numbers.
    Code:
    void Metaforika::test() {
                
                 Trena train;
                 Ploa boat;       
    
    train.initialize_t();
    boat.initialize_p();
    kratisi(1,2,2);
    BUT if i call it from main
    Code:
    int main() {
        int i;
       Metaforika Meta;
       Meta.test();
       Meta.kratisi(1,2,2);
    
       cin>> i;
        
         
        return(0);
    the numbers fix. Thats the real problem i have i think.
    The kratisi's function variables cannot be changed through another function but they can be changed through main . Is there something i'm missing?
    eg. The array index problem doesn't matter because the value is always >0 [ i've created tha same program with structures and didn't have problem]

  4. #4
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Your string array (if you want to store strings in your array), should be declared something like:

    string dromologia[6][40];

    Then you can assign a string to dromologia[0] and so on.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    've fixed that but still, my problem is with numbers kr_dr and kr_thesis that dont really change. only in main the can be changed.
    am i missing something?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't see where you are outputting any results, so how do you know the values are wrong?

    But I do see that in kratisi you declare a local object and set its values. This object doesn't exist any longer once the function returns and is not the same object as the one with the same name in the calling code. May-be you want to set the values of this object, not a local?

    Quote Originally Posted by kcpilot View Post
    Your string array (if you want to store strings in your array), should be declared something like:

    string dromologia[6][40];

    Then you can assign a string to dromologia[0] and so on.
    This is wrong. std::string is not a char array.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    Code:
     cout << train.kr_dr<< endl;
    i'm outputting the results, i thought it wasn't necessary to post it.

    i declare an object of class Trena named train. Doesn't that suppose to change the values of the variables declared in the structure of the class.

    example.
    Code:
    Class Metaforika { 
    Class Trena {
    int c;
    void test();
    }}
     
    void Metaforika::test() {
    Trena train;
    train.c =5;
    }
    isn't that suppose to print 5 in train.c ? the int c; declared in the class.
    I'm just talking theoretical so if i'm saying something wrong you can tell me.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by patra_user View Post
    Code:
     cout << train.kr_dr<< endl;
    i'm outputting the results, i thought it wasn't necessary to post it.

    i declare an object of class Trena named train. Doesn't that suppose to change the values of the variables declared in the structure of the class.

    example.
    Code:
    Class Metaforika { 
    Class Trena {
    int c;
    void test();
    }}
     
    void Metaforika::test() {
    Trena train;    // This is a LOCAL copy of class Trena. It disappears when the function returns. 
    train.c =5;
    }
    isn't that suppose to print 5 in train.c ? the int c; declared in the class.
    I'm just talking theoretical so if i'm saying something wrong you can tell me.
    See above comment in red.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    OK. I think you've got that right.
    What do i do to fix that?
    Thank you guys.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you'd like to have an instance of Trena as part of your Metaforika class?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    Instance of trena in Metaforika Class
    Code:
    public:   
              void kratisi(int x,int y,int z );  
              Trena train;
    Constructor to set kr_dr , and kr_thesis
    given values of y = 2 , z = 2
    Code:
     Metaforika::Trena::Trena(int y,int z) {
                                 
                                 kr_dr =y;
                                kr_thesis=z;
                                 thesis[z]--;
                                 cout << y << " " << z;
    Printing the values in the other function, the function that has problem
    Code:
    void Metaforika::kratisi(int x,int y,int z){
    if (x==1) {
    Trena(y,z);
    cout << train.kr_dr<< endl; }}
    OUTPUT
    Code:
    2 2
    
    20898778896
    still..

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by patra_user;697226
    [CODE
    void Metaforika::kratisi(int x,int y,int z){
    if (x==1) {
    Trena(y,z);
    cout << train.kr_dr<< endl; }}
    [/CODE]
    Yes, because it should be:
    Code:
    void Metaforika::kratisi(int x,int y,int z){
    if (x==1) {
    train(y,z);
    cout << train.kr_dr<< endl; }}
    Your code just creates a temporar Train object that is never stored or used [it is created, then destroyed when the function is finished].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    It gets me this error:
    no match for call to `(Metaforika::Trena) (int&, int&)'
    i'm starting to be a pain in the ............sorry matsp.

    what changes shall i do to this code?
    Code:
    Metaforika::Trena::Trena(int y,int z) {

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of course, that doesn't work. You need to have either a constructor to train() in your Metaforika constructor, or use a set function for your train component.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    in my Metaforika constructor? How is that possible? My Metaforika constractor is just empty. I'm missing serious subjects here i think.

    or the other way,
    set function for my train component?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. List iteration problem, it resets values
    By Mike Borozdin in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2006, 08:42 AM
  3. Replies: 1
    Last Post: 02-03-2005, 02:33 AM
  4. problem of garbage values
    By aldajlo in forum C Programming
    Replies: 5
    Last Post: 10-02-2004, 04:41 PM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21