Hello World V 03 02 02

This is a discussion on Hello World V 03 02 02 within the C++ Programming forums, part of the General Programming Boards category; Code: // Hello World! V 3_02_02 // Use of functions and variables // by SRS & the assistance of the ...

  1. #1
    SRS
    SRS is offline
    3rd Week In C++ SRS's Avatar
    Join Date
    Nov 2007
    Posts
    22

    Hello World V 03 02 02

    Code:
    // Hello World! V 3_02_02
    // Use of functions and variables
    // by SRS & the assistance of the
    // fine people at cprogramming.com
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    string get_name()
    {
      string firstName;
      string lastName;
      string fullName;
      for(;;)
      {
        cout << "Please Enter Your First Name:" << endl;
        cin >> firstName;
        cout << "Please Enter Your Last Name:" << endl;
        cin >> lastName;
        fullName = firstName + " " + lastName;
        char yn;
        for(;;)
        {
          cout << "Please verify your correct name is " <<  fullName << endl;
          cout << "By entering \"Y\" or \"Yes\" if it is correct" << endl;
          cout << "or \"N\" or \"No\" if it is incorrect:" << endl;
          string verify;
          cin >> verify;
          int verifySize = verify.size();
          if(verifySize <= 0)
          {
            cout << "Please enter \"Y\" or \"N\":" << endl;
            continue;
          }
          else
          {
            yn = verify[0];
            break;
          }
        }
        if(tolower(yn) == 'y')
        {
          cout << "Hello World, from " << fullName << "!!"<< endl;
          break;  //  Exit the loop upon verification
        }
        else
        {
          cout << "Please try again:\n" << endl;
          continue;  //  Repeat the loop for any other answer
        }
      }
      return fullName;
    }
    int main()
    {
      string newUser = get_name();  //  Get & Verify User's First & Last Name
      /*
      |Actions -----|
      |to ----------|
      |be ----------|
      |performed ---|
      |on ----------|
      |new ---------|
      |user --------|
      */
      cout << newUser << ", this is the end of the road!" << endl;
      system ("PAUSE");
    }

    Ok many versions into this and that is what it looks like. Please feel free to suggest improvements, changes, possible errors, or any other comment you like before I move on to something else. Thanks to all of you who have helped me getting started with this.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Since you're only saving one name, you can use getline to get the entire line. That will also allow somebody with three (or more) names (e.g. John Paul De La Salle). The getline function will read until the end of the line, rather than stopping at a space like cin >> does.

  3. #3
    SRS
    SRS is offline
    3rd Week In C++ SRS's Avatar
    Join Date
    Nov 2007
    Posts
    22

    Thanks Daved

    Quote Originally Posted by Daved View Post
    Since you're only saving one name, you can use getline to get the entire line. ...
    getline is new to me but I will look into it now.

    This "thing" I am writing is odd as it's only purpose is to contain working samples of code structure for my learning... I have never used an object oriented language before and I think where I am going with this next is to construct something like personnel records so I actually will end up saving more than just a name. I want to use this to get into the abilities of classes. Any suggestions on that would be greatly appreciated.

    Till then checking out getline...

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Example of getline:

    Code:
    std::string myName;
    
    std::cout << "Enter your full name: ";
    getline ( std::cin, myName );
    
    std::cout << "\nHello " << myName << std::endl;
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Also, you should include <cctype>, which is where tolower() resides.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    SRS
    SRS is offline
    3rd Week In C++ SRS's Avatar
    Join Date
    Nov 2007
    Posts
    22

    Nice to know

    Quote Originally Posted by dwks View Post
    Also, you should include <cctype>, which is where tolower() resides.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    Right now this is what I have, yet it works with caps or lower input. Is it necessary? or is the compiler searching for it even though I did not instruct include? other reason it works without including <cctype>? Thanks for the help!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Yes, always add the appropriate #include even if it works without it. In some cases you will try something else that will suddenly not work, or if you change to a different compiler or give your code to someone else it also might not work.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I agree with Daved. If you enable warnings in the compile, it should tell you that you have "function called without a prototype". This is often not a problem, but sometimes it is:
    On 68K family processors, the return of a pointer is in register A0, but numbers are returned in D0. So guess what happens if you mess up your prototype and the compiler gets it wrong? It uses the wrong register to pick the result from, and your code doesn't work right!

    Other such things exist too - calling printf() in x86_64 Linux will not work right without the compiler knowing that printf is a "varargs" function - but only when you use floating point arguments, so most of the time it works fine without the right header, then all of a sudden it fails when you change the code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    SRS
    SRS is offline
    3rd Week In C++ SRS's Avatar
    Join Date
    Nov 2007
    Posts
    22

    Yes...

    Quote Originally Posted by matsp View Post
    I agree with Daved. If you enable warnings in the compile, it should tell you that you have "function called without a prototype".
    I agree also, as this I am nearing my first 24 hours in C++ lol...
    However, warnings are enabled, and it did not produce one. (I have included it now anyway)
    Using Dev-C++ V 4.9.9.2

    Anyway, thanks for all the advice. I am soaking it up as much as I can.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    It doesn't have to give a warning (and rarely will... usually you will get an error or nothing). It might work 100&#37; perfectly because one of your other includes might include the header that declares that function. The standard allows the compiler to be flexible with these things, it's just up to you to be vigilant.

  11. #11
    SRS
    SRS is offline
    3rd Week In C++ SRS's Avatar
    Join Date
    Nov 2007
    Posts
    22

    Yes, thanks

    Quote Originally Posted by Daved View Post
    one of your other includes might include the header that declares that function.
    That is the reason I posted my current includes... This is probably a question most know the answer to but... where could I look these up? Know of a good site or anything?

    ty again

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I usually look them up in a book (TC++PL by Stroustrup or The C++ Standard Library by Josuttis). You can also use reference sites of standard libraries, but it's not always easy to find them in there. dinkumware.com, sgi.com, or msdn or possible options that I've used myself.

  13. #13
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    to construct something like personnel records so I actually will end up saving more than just a name. I want to use this to get into the abilities of classes.
    Perfect! You can make an employee class with member variables like FirstName, LastName, Title, Salary, etc. And, you can have member functions like GetLastName(), IncreaseSalary(), etc. The object-name might be an employee number.

    But, take it one step at a time... You'll probably want to learn more about the basics, including functions and reading/writing to the disk before you start working on an object oriented "database".

  14. #14
    SRS
    SRS is offline
    3rd Week In C++ SRS's Avatar
    Join Date
    Nov 2007
    Posts
    22

    Right on track DougDbug

    Quote Originally Posted by DougDbug View Post
    Perfect! You can make an employee class with member variables like FirstName, LastName, Title, Salary, etc. And, you can have member functions like GetLastName(), IncreaseSalary(), etc. The object-name might be an employee number.

    But, take it one step at a time... You'll probably want to learn more about the basics, including functions and reading/writing to the disk before you start working on an object oriented "database".
    I am already familiar with functions and variables and the other basics similiar to an event driven language, its the object oriented stuff that is new. struct - class ... Here is where I am so far in my latest learning tool...

    Code:
    // Hello World! V 5_00_00
    // struct and class
    // by SRS & the assistance of the
    // fine people at cprogramming.com
    * --------------------------------------------------------------------INCLUDE*/
    #include <iostream>  //  cin/cout
    //#include<iomanip>
    //#include<fstream>
    #include <string>  //  string
    #include <cctype>  // tolower
    using namespace std;
    /* ---------------------------------------------------------------------STRUCT*/
    struct contacts
    {
        string firstName;
        string lastName;
        string companyName;
        string homeAddress;
        int id_number;
        int age;
    };
    /* --------------------------------------------------------------------DECLARE*/
    void add_someone();  //  required to compile
    /* ------------------------------------------------------------------FUNCTIONS*/
    int yes_no()  //  Return 1(yes) or 2(no) or re-enter invalid answers
    {
        char yn;
        for(;;)
        {
            cout << "Please enter \"Y\" or \"N\"" << endl;
            string verify;
            getline(cin, verify);
            int verifySize = verify.size();
            if((verifySize <= 0) || (verifySize >= 2))  //  next line:
            //  User enters other than single character reply, re-enter
            {
                cout << "ERROR: YOUR ANSWER IS INVALID..." << endl;
                cout << "Please try again..." << endl;
                continue;
            }
            else  //  User enters a single character reply
            {
                yn = verify[0];  //  Should not be necessary
                yn = tolower(yn);  //  Force lower input
                if(yn == 'y')  //  User enters Y or y only
                {
                    return 1;
                    break;
                }
                else if(yn == 'n')  //  User enters N or n only
                {
                    return 0;
                    break;
                }
                else  //  User enters any other reply, re-enter
                {
                    cout << "ERROR: YOUR ANSWER IS INVALID..." << endl;
                    cout << "Please try again..." << endl;
                    continue;
                }
            }
        }
    }
    void get_name()  //  Get & Verify the first and last name, offer repeat
    {
        string fullName;
        for(;;)
        {
            contacts person;
            cout << "Please Enter Your First Name:" << endl;
            getline(cin, person.firstName);
            cout << "Please Enter Your Last Name:" << endl;
            getline(cin, person.lastName);
            fullName = person.firstName + " " + person.lastName;
            cout << "Please verify your name is correct: " << fullName << endl;
            int reply = yes_no();
            if(reply)
            {
                cout << "Hello World, from " << fullName << "!!"<< endl;
                break;  //  Exit the loop upon verification
            }
            else
            {
                cout << "Please try again:\n" << endl;
                continue;  //  Repeat the loop for any other answer
            }
        }
        add_someone();
    }
    void add_someone()  //  Add a user or exit program
    {
        cout << "Do you wish to add a new contact now?" << endl;
        int reply = yes_no();
        if(reply)
        {
            get_name();
        }
        else
        {
            cout << "Very well, have a nice day!" << endl;
        }
    }
    /* -----------------------------------------------------------------------MAIN*/
    int main()
    {
        add_someone();
        system ("PAUSE");
    }
    PLEASE ANYONE/EVERYONE with any suggestions, ideas, alternatives, improvements, post it... my feelings will not be hurt, I am eager to learn and willing to do what ever it takes...

    THANK YOU ALL who have helped me learn so far, your time is GREATLY appreciated!
    Last edited by SRS; 11-05-2007 at 07:40 PM. Reason: oops wrong button...

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Use bool instead of int for true/false situations (like the reply variable or the yes_no() return value).

    Instead of calling add_someone from inside get_name(), just make a loop inside add_someone(). Otherwise, if you keep adding people then the stack will overflow because the functions will keep calling each other over and over created a deeper and deeper stack that never unwinds.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The 7 New Wonders of the World
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 12-08-2006, 01:55 PM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 01:51 PM
  5. Too much to ask ?
    By deflamol in forum C Programming
    Replies: 2
    Last Post: 05-06-2004, 05:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21