C++ programming question (Not sure where to put this thread)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xObserveRx
    FFR Simfile Author
    FFR Simfile Author
    • Aug 2003
    • 1148

    #1

    C++ programming question (Not sure where to put this thread)

    Okay, so I'm rather new to C++ programming and I was wondering if someone out there could help me clear up some confusion.

    I'm having trouble working with and understanding how to use character strings as input for a program. I have two examples of programs I've created with (what I figured would be working) character string input types. One of the two programs compiles and runs perfectly fine, while the other causes a ton of compiling errors.

    #include <iostream>

    int main()
    {
    using namespace std;


    string a, b, c, test;
    int z;

    cout << "Input a word: ";
    getline (cin, a);
    cout << a << " is the word you typed, right?\n";

    cout << "Well gimme another word now:";
    cin >> b;
    cout << b << " is a great word!\nNow gimme one more!\n";
    cin >> c;
    cout << c << "? Ha ha ha! I love that word!\n";

    cout << "What's your name?: ";
    cin >> test;
    cout << "Hello " << test << ", you look wonderful today!\n";

    cout << "I still remember those words! " << a << " " << b << " ";
    cout << "and " << c << "!\n";

    cin >> z;

    return 0;
    }
    That program compiles and runs with no problems.

    This next one, however, does not. (Note that this program is much more complicated and that may very well be the reason for the problems. Also, please disregard any of my other newb mistakes and focus simply on the character string inputs and why they are causing problems. Also, this program was originally broken up into two parts, a header file and the regular .cpp file. I've been troubleshooting and was tired of switching back and forth between the two files and just put the class declaration, definition and functions in the .cpp file).

    #include <iostream>


    class Person
    {
    public:
    Person(int initAge, string initName);
    ~Person();

    void setAge(int age);
    int getAge();

    void setName(string name);
    string getName();

    void yell();

    private:
    int itsAge;
    string itsName;

    };

    Person::Person(int initAge, string initName)
    {
    initAge = itsAge;
    initName = itsName;
    }

    Person::~Person
    {

    }

    void Person::setAge(int age)
    {
    itsAge = age;
    }

    int Person::getAge()
    {
    return itsAge;
    }

    void Person::setName(string name)
    {
    itsName = name;
    }

    string Person::getName()
    {
    return itsName;
    }

    void Person::yell()
    {
    std::cout << "Woo Hoo!\n";
    }

    int main()
    {
    using namespace std;

    string inputName;
    int inputAge, z;
    Person John(34, John);

    John.yell();

    cout << "This is " << John.getName() << ". He is "
    cout << John.getAge << " years old.\n Let's change those things...\n";

    cout << "Give " << John.getName() << " a new age please: ";
    cin >> John.setName(inputName);

    cout << "Good, " << John.getName() << " is John's new name!\n";
    cout << "Now let's change " John.getName() << "'s age: ";
    cin >> John.setAge(inputAge);
    cout << John.getName() << " is now " << John.getAge() << " years old!";

    cin >> z;

    return 0;
    }
    Now I know someone out there clearly sees the problem and wants to laugh at it. Go right ahead, but please tell me (without too much sarcasm) what I did wrong in the second program. I don't understand why defining a simple string data type (like a, b, and c in the first program) doesn't work in the second program. Is using strings in classes much different than it is in a simple program? If you know how to do it but don't feeling like explaining the answer because it'll take 20 pages to do so, I'd appreciate it if you could direct me somewhere that has an answer that I'd have access to.

    Any help on clearing up my confusion on this subject would be greatly appreciated.
    Come Play The Werewolf Game!
  • Necamus
    FFR Player
    • Nov 2005
    • 853

    #2
    Re: C++ programming question (Not sure where to put this thread)

    My initial thought was that you ****ed up the classes, but they seem fine.

    Okay, what's this:

    cin >> John.setName(inputName);

    I see what you're trying to do, but I don't think you can do that (Haven't done C++ in a while).
    Try:

    cin >> inputName;
    John.setName(inputName);

    Same for:
    cin >> John.setAge(inputAge);

    cin >> inputAge;
    John.setAge(inputAge);
    www.freerice.com

    Comment

    • dean_machine
      FFR Veteran
      • Oct 2006
      • 1267

      #3
      Re: C++ programming question (Not sure where to put this thread)

      cout << "Give " << John.getName() << " a new age please: ";
      cin >> John.setName(inputName);
      Shouldn't that say say " a new name please: " instead of " a new age please: " ?

      That might have been one of those newb mistakes I wasn't suppose to point out.

      Anyway I would try doing what Necamus said.

      Comment

      • JKPolk
        tool
        • Aug 2003
        • 3737

        #4
        Re: C++ programming question (Not sure where to put this thread)

        What Necamus said. You're trying to read in with a method call; while you think it might be able to streamline your code, there are some things you just can't do. On a related note, you should always read in to a String anyway to store for future use. If you really and truly only need to use the input one time, you can release the String after you use it.

        Comment

        • CyanoticXtC
          FFR Player
          • Dec 2003
          • 259

          #5
          Re: C++ programming question (Not sure where to put this thread)

          All I can say is
          Howdy Dan

          Comment

          • argo15
            FFR Veteran
            • May 2006
            • 1863

            #6
            Re: C++ programming question (Not sure where to put this thread)

            Person::Person(int initAge, string initName)
            {
            initAge = itsAge;
            initName = itsName;
            }
            Should be.
            Person::Person(int initAge, string initName)
            {
            itsAge = initAge;
            itsName = initName;
            }
            Edit:
            If your having problems still post what your debugger says.
            Last edited by argo15; 02-20-2009, 02:15 PM.

            Comment

            • PsYcHoZeRoSk8eR
              Threat Emulation
              FFR Simfile Author
              • May 2004
              • 5184

              #7
              Re: C++ programming question (Not sure where to put this thread)

              I'm happy I've been done with programming since last year. I'm no good at it.

              Originally posted by Lightdarkness
              I'm light f**king darkness

              Comment

              Working...