Hey, I've been interested in getting started with programming for a long time, so I decided I'll look up some videos and try it out finally. I came across The New Boston Education (http://thenewboston.org/list.php?cat=16) and I'm on video 13. Anyway, I'm trying to do something and I'm stuck. I'd like to know if anyone could tell me what I'm doing wrong.
I don't know C++ but I've used Java and Basic and Visual basic and VBA for doing data analysis stuff - Numerical differential equations and least squares ect. I can probably help with algorithm kind of things, but not specific syntax sort of things. What're you stuck on, exactly?
It's a syntax thing. I'm dealing with public and private classes (or is it objects?) and I don't know if I'm going about this the wrong way. Plus dealing with strings. To be exact, I'm trying to put my first name and last name together (so it would be like Full name = first name + last name. I'm not exactly sure how I can explain it.
This is what the code I currently have. I'm just trying to put my name together. It runs, but it won't show the names. #include <iostream> #include <string> using namespace std; class MyName{ public: void setFirstName(string x){ firstName = x; } void setLastName(string y){ lastName = y; } void myName(){ name = firstName + lastName; } string getName(){ return name; } private: string firstName; string lastName; string name; }; int main() { MyName mn; mn.setFirstName("Michael"); mn.setLastName("Cooper"); cout << ("My name is ") << mn.getName(); return 0; }
http://www.cplusplus.com Have you given this resource a try? They have a disturbingly vibrant community and I bet the tutorial stuff will resolve your questions... Also, its been a LOOONG time since I've done any programming. But, I reviewed the video and i don't see where you may be getting stuck. Try making the same program as is made in this video. But, where he is defining the name variable in the code, do a do a cin >> to define the setName variable in the complied program rather than define it in the code. If I recall my teachings correctly, wouldnt that just be a:- cout << "What's your name, bro?\n"; cin >> bo.setName (); If that works... which I admit is pretty iffy given that I haven't looked at this in literally 12 years (Sorry, if it doesn't)... then just expand those class strings from only name to firstName and lastName in your private class. Also, in the public class create your set/getfirstName and set/getlastName functions. and the the output would look like:- cout << "'Sup, " << bo.getfirstName << " " << bo.getlastName << "!" << " What's crackin'?"; I hope that works... seriously, though... check out that c++ forum. Thats gonna be your best bet.
#include <iostream> #include <string> using namespace std; class MyName{ private: string firstName; string lastName; string name; public: // constructors MyName() { // do nothing } MyName(string x, string y) { firstName = x; lastName = y; name = firstName + " " + lastName; } void setFirstName(string x){ firstName = x; } void setLastName(string y){ lastName = y; } string getFullName(){ return firstName + " " + lastName; } string getName(){ return name; } }; int main() { MyName mn; int wait; mn.setFirstName("Michael"); mn.setLastName("Cooper"); cout << ("My name is ") << mn.getFullName(); cin.get(); mn = MyName("Sheldon","Pooper"); cout << ("My name is ") << mn.getName(); cin.get(); return 0; }
Ok, I figured out a lot of that stuff I can cut and still get the result I wanted. A few questions though. Why do I have to put the "" between firstName + and + lastName? And why do I have to put a second + in that? What does cin.get() do? Does // allow me to put comments down about the code? I think that's all for now. I've never taken a programming class, so the only jargon I know are what I've been hearing in these videos.
cin.get() will try to get a line from the command prompt. It will basically pause it so that you can actually see what the output is. putting firstname + " " + lastname should print out your first name space lastname. The second + sign is because you are now adding 3 strings together: firstname, " ", and lastname are all strings.
ha! looks like I was on the right track with my help! Not bad for over a decade of dust. Does this answer your questions? In any case, I think you ought to slow down the videos a bit and get a little more basic with your learning. These are important and basic concepts you are questioning in this thread here. And I looked ahead on these videos- the guy doing them is pretty fantastic at spitting out code quickly, but makes some pretty big assumptions about your progress and prior knowledge.. Might I recommend you get a book as well? I learn pretty well with reading a tutorial, and following along. I tend to get lost in the youtube videos. Just a thought.
I'm starting to understand more of it now and applying what I know to try to put some basic things together. It's been going good so far. I don't know what book I should start with anyway.
http://www.amazon.com/Starting-Out-...1576661&sr=8-8&keywords=starting+out+with+C++ Used this one ages ago when I was in school, found it quite good.