Question about returning an apstring in C++

Discussion in 'OT Technology' started by Danja, May 18, 2007.

  1. Danja

    Danja New Member

    Joined:
    Nov 16, 2005
    Messages:
    63
    Likes Received:
    0
    I wrote a small test program that I intend to join into a larger program which is supposed to randomly pick an apstring, return it to the main function, and display it. However, it seems like the apstrings are not being returned and I can't figure out why. Can anyone give me a tip on what I'm doing wrong?

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <time.h>
    #include <apstring.h>
    #include <apstring.cpp>
    
    apstring random(apstring insult);
    
    int main()
    {
        apstring insult;
        
        random(insult);
        cout << insult << endl;
        random(insult);
        cout << insult << endl;
        random(insult);
        cout << insult << endl;
        random(insult);
        cout << insult << endl;
        random(insult);
        cout << insult << endl;
        random(insult);
        cout << insult << endl;
    
        return (0);
    }
    
    
    apstring random(apstring insult)
    {
        int low=1, high=10, x;
    
        x = rand()%(high-low+1)+low;
    
        switch (x)
        {
      case 1:
        insult = "you are a loser";
        break;
      case 2:
        insult = "you are weird";
        break;
      case 3:
        insult = "you are dumb";
        break;
      case 4:
        insult = "you are stupid";
        break;
      case 5:
        insult = "you are hairy";
        break;
      case 6:
        insult = "you are butthead";
        break;
      case 7:
        insult = "you are beavis";
      case 8:
        insult = "you are crazy";
      case 9:
        insult = "you are assinine";
      case 10:
        insult = "you are a bad programmer";
        }
    
    cout<<x<<endl;
    
        return insult;
    }
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    youre passing by value you need to pass by reference:

    void random(apstring& insult);

    that should be your function. you dont have to return anything if youre passing in the variable reference you want to get assigned the new phrase
     
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Since you're returing a value, you could've done this:

    cout << random(insult) << endl;
    cout << random(insult) << endl;
    cout << random(insult) << endl;
    cout << random(insult) << endl;
    cout << random(insult) << endl;
    cout << random(insult) << endl;

    But it's still highly inefficient, and the method GOGZILLA presented is prefered.
     
  4. Danja

    Danja New Member

    Joined:
    Nov 16, 2005
    Messages:
    63
    Likes Received:
    0
    Thanks guy. That solved it for me.
     
  5. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    dude do you have some resource that tells you the most efficient way to do very common stuff that you might not be doing? like pre-incrementing is faster than post-incrementing and shit like that?
     
  6. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Read Effective C++ and More Effective C++ by Scott Meyers. Covers a lot of common things you should and shouldn't do in C++.

    The inefficiency is due to copying the object when you pass by value, and then copying it again when it is returned by the function. From the original program. it appears that the intention was to do a pass by reference, in which case the return value is unnecessary and no copies would be made.
     
  7. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    im reading effective C++ right now, literally, and will get more effective soon. i know about why passing by value is bad but was wondering if you knew of any other repository of nuances(passing by value is pretty major).
     
  8. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Well, there is the "slicing" problem covered in the book. If you have a derived class and pass it by value to a function that takes the base class as an argument, the features of the derived class get chopped off. The function only gets a copy of the base class portion.

    Another thing to watch out for when copying objects is that you'll get a default copy constructor that does a bitwise copy if you don't define a copy constructor yourself. This is especially important if your class contains dynamically allocated data. Suppose that apstring class has a dynamically allocated buffer to contain the string, and the programmer forgets to define a copy contructor. When the apstring is copied and passed to the function, the member pointer to the buffer gets copied, not the buffer itself. So, the copy and the original point to the same buffer. Now, the function ends, and the copied instance goes out of scope. The destructor is going to get called, and being conscientious of memory leaks, the programmer has designed the destructor to deallocate the buffer. Well, you see that the original instance is going to be left with a dangling pointer if this happens. If the programmer is going to allow the object to be copied, he has to implement a copy constructor that makes a copy of any dynamic data.
     
  9. Danja

    Danja New Member

    Joined:
    Nov 16, 2005
    Messages:
    63
    Likes Received:
    0
    Damn. It worked perfectly and all of a sudden it stopped. I can't break out of my while loop. The program is designed to ask 25 questions (with a menu function after each one of them), then stop, clear the screen, do the credits function, stop, and exit. However, as soon as my last question is answered, it asks it over again, and once it is answered it goes back to question 1 and repeats itself. I have a feeling I did something extremely stupid and didn't catch myself.

    Here is my main function where the while loop is at.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <time.h>
    #include <apstring.h>
    #include <apstring.cpp>
    #include <windows.h>
    
        int x=25;
    
    void question1();
    void question2();
    void question3();
    void question4();
    void question5();
    void question6();
    void question7();
    void question8();
    void question9();
    void question10();
    void question11();
    void question12();
    void question13();
    void question14();
    void question15();
    void question16();
    void question17();
    void question18();
    void question19();
    void question20();
    void question21();
    void question22();
    void question23();
    void question24();
    void question25();
    
    int question();
    void credits();
    
    apstring random(apstring insult);
    
    int main()
    {
    
        char z;
    
        cout << "Bienvenidos a la videojuega de Simon Bolivar. Soy su caballero robotico," << endl;
        cout << "RoboJose 3000. Ahora, vamos a jugar. Si contesta la pregunta correctamente," << endl;
        cout << "recibira un dulce. Si su respuesta no es correcta, no recibira NADA." << endl << endl;
        cout << "Lo siento que no puedo usar caracteres espanoles. Ahora, empecemos." << endl;
        cin.get();                 
        system("cls");
    
        while(1)
        {
    
        question1(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question2(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question3(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question4(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question5(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question6(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question7(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question8(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question9(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question10(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question11(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question12(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question13(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question14(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question15(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question16(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question17(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question18(); cout << endl;
        cout << endl;
        question();
    
    
        system("cls");
        question19(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question20(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question21(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question22(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question23(); cout << endl;
        cout << endl;
        question();
    
        system("cls");
        question24(); cout << endl;
        cout << endl;
        question();
        
        system("cls");
        question25(); cout << endl;
        cout << endl;
        cin.get();
        z = 'n';
        
        }
    
        cout << "Muy bien hecho. Applauso!" << endl;
        cout << "Adios, clase. Adios, Senora Moore." << endl;
        
        
        
        system("cls");
        credits();
        cin.get();
        exit( 0 );
        return 0;
        
    }
    Why isn't it breaking out?
     
  10. TheUser20

    TheUser20 OT Supporter

    Joined:
    Sep 7, 2005
    Messages:
    1,835
    Likes Received:
    0
    Location:
    Illinois
    :rofl: how is a constant going to break a while loop?
     
  11. TheUser20

    TheUser20 OT Supporter

    Joined:
    Sep 7, 2005
    Messages:
    1,835
    Likes Received:
    0
    Location:
    Illinois
    oh and write a function to reduce some of that damned redundancy
     

Share This Page