Anyone experienced with C++?

Discussion in 'Tech Talk' started by CoopTang, Feb 16, 2013.

  1. CoopTang
    Veteran

    Joined:
    Jan 22, 2013
    Messages:
    49
    Likes Received:
    0
    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.
     
  2. Rbstr
    Veteran

    Joined:
    Feb 16, 2012
    Messages:
    391
    Likes Received:
    0
    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?
     
  3. CoopTang
    Veteran

    Joined:
    Jan 22, 2013
    Messages:
    49
    Likes Received:
    0
    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.
     
  4. CoopTang
    Veteran

    Joined:
    Jan 22, 2013
    Messages:
    49
    Likes Received:
    0
    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;
    }
     
  5. mwhays
    Veteran

    Joined:
    Nov 19, 2011
    Messages:
    1,286
    Likes Received:
    0
    Occupation:
    Stock Broker / Financial Planner / Registered Inve
    Location:
    Cincinnati
    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.
     
  6. Utherix
    Member

    Joined:
    Sep 16, 2012
    Messages:
    598
    Likes Received:
    1
    #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;
    }
     
  7. Rbstr
    Veteran

    Joined:
    Feb 16, 2012
    Messages:
    391
    Likes Received:
    0
    All I've got to add for now is that the concept of putting strings together is call "concatenation"
     
  8. CoopTang
    Veteran

    Joined:
    Jan 22, 2013
    Messages:
    49
    Likes Received:
    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.
     
  9. EniGmA1987
    Veteran Staff Member Xenforcer

    Joined:
    Aug 25, 2010
    Messages:
    4,778
    Likes Received:
    34
    I dont know a lot about programming in C, but yes the // does comment out that line.
     
  10. Regis
    Veteran

    Joined:
    Dec 30, 2012
    Messages:
    702
    Likes Received:
    0
    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.
     
  11. mwhays
    Veteran

    Joined:
    Nov 19, 2011
    Messages:
    1,286
    Likes Received:
    0
    Occupation:
    Stock Broker / Financial Planner / Registered Inve
    Location:
    Cincinnati
    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.
     
  12. CoopTang
    Veteran

    Joined:
    Jan 22, 2013
    Messages:
    49
    Likes Received:
    0
    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.
     
  13. mwhays
    Veteran

    Joined:
    Nov 19, 2011
    Messages:
    1,286
    Likes Received:
    0
    Occupation:
    Stock Broker / Financial Planner / Registered Inve
    Location:
    Cincinnati
    Yeah me neither, maybe even a basic text book for c++ you might be able to find on amazon.
     
  14. ArmoredMoose
    Guest

    Joined:
    Feb 22, 2013
    Messages:
    4
    Likes Received:
    0