Go Back   Flash Flash Revolution > General Discussion > Chit Chat
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
Old 02-19-2009, 04:31 PM   #1
xObserveRx
FFR Simfile Author
FFR Simfile Author
 
xObserveRx's Avatar
 
Join Date: Aug 2003
Location: Ontario
Age: 41
Posts: 1,148
Send a message via AIM to xObserveRx Send a message via MSN to xObserveRx
Default 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.

Quote:
#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).

Quote:
#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.
xObserveRx is offline   Reply With Quote
Old 02-19-2009, 04:41 PM   #2
Necamus
FFR Player
 
Necamus's Avatar
 
Join Date: Nov 2005
Age: 34
Posts: 853
Default 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
Necamus is offline   Reply With Quote
Old 02-19-2009, 05:34 PM   #3
dean_machine
FFR Veteran
FFR Veteran
 
dean_machine's Avatar
 
Join Date: Oct 2006
Location: inside the box
Posts: 1,267
Default Re: C++ programming question (Not sure where to put this thread)

Quote:
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.
__________________
dean_machine is offline   Reply With Quote
Old 02-19-2009, 05:37 PM   #4
JKPolk
tool
 
JKPolk's Avatar
 
Join Date: Aug 2003
Location: satan
Age: 37
Posts: 3,737
Send a message via AIM to JKPolk Send a message via Skype™ to JKPolk
Default 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.
__________________
JKPolk is offline   Reply With Quote
Old 02-20-2009, 10:53 AM   #5
CyanoticXtC
FFR Player
Retired StaffFFR Veteran
 
CyanoticXtC's Avatar
 
Join Date: Dec 2003
Location: Aviano, Italy
Age: 39
Posts: 259
Send a message via AIM to CyanoticXtC
Default Re: C++ programming question (Not sure where to put this thread)

All I can say is
Howdy Dan
__________________
CyanoticXtC is offline   Reply With Quote
Old 02-20-2009, 03:03 PM   #6
argo15
FFR Veteran
FFR Veteran
 
argo15's Avatar
 
Join Date: May 2006
Age: 33
Posts: 1,863
Default Re: C++ programming question (Not sure where to put this thread)

Quote:
Person::Person(int initAge, string initName)
{
initAge = itsAge;
initName = itsName;
}
Should be.
Quote:
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 at 03:15 PM..
argo15 is offline   Reply With Quote
Old 02-20-2009, 04:01 PM   #7
PsYcHoZeRoSk8eR
Network Security Analyst
FFR Simfile AuthorFFR Veteran
 
PsYcHoZeRoSk8eR's Avatar
 
Join Date: May 2004
Location: ɥɔʇɐdı sʞuɐɥʇ
Age: 36
Posts: 5,183
Send a message via AIM to PsYcHoZeRoSk8eR Send a message via MSN to PsYcHoZeRoSk8eR
Default 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.
__________________

Quote:
Originally Posted by Lightdarkness
I'm light f**king darkness
PsYcHoZeRoSk8eR is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 12:48 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution