people's names manager

This is a discussion on people's names manager within the C++ Programming forums, part of the General Programming Boards category; how could i simplify this code? Code: #include <iostream> #include <iomanip> using namespace std; class person { public: static int ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    people's names manager

    how could i simplify this code?

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    class person
    {
        public:
            static int total;
            person();
            string name;
    };
    
    int person::total = 0;
    
    person::person()
    {
        total++;
    }
    
    int main()
    {
        int x;
        do
        {
            if(x==0)
            {
                cout<<"Gotta create at least one person."<<endl<<endl;
            }
            cout<<"How many people do you wanna create? ";
            cin>>x;
        }
        while(x == 0);
    
        person people[x];
        cout<<"There are currently "<<person::total<<" people."<<endl;
        cin.get();cin.get();
    
    
        x=0;
        string newName;
        do
        {
            cout<<"Whose name do you wanna set? ";
            cin>>x;
    
            if(x > person::total)
            {
                cout<<"There is no such person."<<endl;
                cin.get();
                cout<<endl;
                continue;
            }
    
            if(x != 0)
            {
                cout<<endl<<"Enter a name for Person N. "<<x<<": ";
                cin>>newName;
                people[x].name = newName;
                cout<<"Person N. "<<x<<"'s name has been set to "<<newName<<".";
                cout<<endl<<endl;
            }
        }
        while(x != 0);
    }
    also, i'd like to list all the people's names that have been set, but i still havent figured out how to do that.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Ignoring everything that is wrong about what you are doing presently, the fact is that the number of people without names taken away from the total people leaves you with the number of people who have names.

    I don't recall variable length arrays being a feature in C++, and even if they are, it's already done by the vector class.
    Last edited by whiteflags; 09-03-2010 at 03:43 AM.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    dhuan, without wanting to offend, where or how are you learning to code in C++ as that is some pretty poor code (even for a beginner). I am assuming that whatever methods you are using are teaching pretty poor form and therefore I would suggest changing.

    I would recommend either the tutorials on this site or Accelerated C++ or Programming Principles and Practice using C++.

    Please don't take this as an attack, as I am genuinely trying to help by referring you to better teaching materials.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    dhuan, without wanting to offend, where or how are you learning to code in C++ as that is some pretty poor code (even for a beginner)
    Trust me, my classmates write much worse code ...

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    i was going to reply to this earlier but then thought no... anyway the points made are valid, the code is flawed and looks like running before walking, or having the idea but reaching for tools not yet available, which is a good thing, imo so long as you take some advice and practice basics with similar themes first
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,423
    Quote Originally Posted by whiteflags View Post
    I don't recall variable length arrays being a feature in C++, and even if they are, it's already done by the vector class.
    There is no such thing in C++ (probably never will be either).
    As such, this is not valid ISO C++ code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Some points, arranged in order of what I consider "advancedness":
    • The variable x is uninitialized at the beginning of main(). Uninitialized variables can take on any value, which you cannot predict and which may change between runs of the program. If x happens to be 0, the user will get a spurious warning message about creating at least one person. I suggest you initialize it to a non-zero value, like maybe -1.
    • You have an off-by-one error in this code:
      Code:
      if(x > person::total)
      The intent is to prevent a buffer overflow on the array people[]. But consider if 1 was entered originally; people will have one element, element [0], and total will have been incremented once to 1. If the user later enters 1, it will not be the case that 1 > 1, and the code will try to access people[1].name. This is a buffer overrun. The if statement I quoted should use >= instead of >. (Or subtract 1 from the x the user enters if you don't want the user to think about zero-based indexing.)
    • The variable-length array is an issue, of course. You could use a vector instead. But with a vector there's no need for the user to enter the number of people in beforehand. You could have the user enter names and as they do so, add new people onto the end of the vector. It would certainly make the program easier to use.
    • It's really not a best practise to use public variables in classes. If you felt up to it you could create getters and setters for name and total, maybe write some member functions like readNameFromUser(), that sort of thing.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 07-30-2009, 07:14 AM
  2. Sorting out a resource manager
    By psychopath in forum Game Programming
    Replies: 1
    Last Post: 11-10-2008, 06:12 PM
  3. reading folder names..how is it done ?
    By roalme00 in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2008, 09:34 AM
  4. Templated Generic Resource Manager, WIP..
    By Shamino in forum C++ Programming
    Replies: 13
    Last Post: 02-19-2006, 05:29 PM
  5. Graphical interface in C ?
    By Golgot in forum C Programming
    Replies: 5
    Last Post: 11-18-2001, 03:10 PM

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