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.
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).
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.
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;
}
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;
}
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;
}
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;
}
Any help on clearing up my confusion on this subject would be greatly appreciated.



Comment