Beginers Coding Help Required

This is a discussion on Beginers Coding Help Required within the C++ Programming forums, part of the General Programming Boards category; I need codign help Problem : ******************************* Consider an entity as Customer. Let its attributes be Name, Contact address and ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    36

    Beginers Coding Help Required

    I need codign help

    Problem :
    *******************************
    Consider an entity as Customer. Let its attributes be Name, Contact address and Phone no. A function is responsible for setting and another for printing the values for an instance of this entity. Implement this entity with its attributes and functions as a struct in C++.

    Code:
    #include <iostream>
    using namespace std;
    
    struct Customer
    {
           char Name;
           char Address;
           double Phone;
    };
    
    int main()
    {
             { cout << "Enter Name:" ; cin >> Name; }
             { cout << "Enter Address:" ; cin >> Address; }
             { cout << "Enter Phone Number" ; cin >> Phone; }
    };

    I need a help with above problem..

    Is my above code correct ?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's correct, provided in your world no one has names or addresses longer than one letter and people have decimals in their phone numbers. Make them std::strings instead.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    You have way too many braces. You don't need any of those in your main() function except the first and last one. A single char is not enough to contain a name or an adress so I would make it a string instead.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    hurray !!!!

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    Code:
    #include <iostream>
    using namespace std;
    
    struct Customer
    {
           string Name;
           string Address;
           double Phone;
    };
    
    int main()
    {
             { 
             cout << "Enter Name:" ; cin >> Name;
             cout << "Enter Address:" ; cin >> Address;
             cout << "Enter Phone Number" ; cin >> Phone; }
    };

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    How to implement as a class in C+ ?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    How to implement as a class in C+ ?
    You could read the Introduction to Classes for starters.

    By the way, the name of the language is C++, not C+.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    Sorry but that was a Type Error :-)

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Don't forget to #include <string>.

    If that's your code, congratulations. Good work.

    You haven't quite solved the problem yet, though. First, you can change it from a struct to a class by changing the word struct to class. However, you'll run into some other problems, since you are accessing those variables in main. I would wait until later for that, leave it as a struct for now.

    The problem says to write functions that set and get those values. You are setting the values in main, but you need to do each separately in its own function.

    Your next step is to write a simple function. You can try to write an empty function first and get it to compile, then add more later. So add a member function to your struct and see if you can get it right.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    When you say

    "you need to do each separately in its own function."

    Can you help me with oen samll Example please ?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Look at the processorspeed variable in the tutorial linked to by laserlight. There is a getspeed and setspeed function for it.

    Your assignment says, "A function is responsible for setting and another for printing the values..." I don't know if that means you need a function for each variable, or if you need just one set and one print function. Either way, you'll need a member function like the ones in that example, so I'd start with one that sets the name and one that prints it.

    Also, I mentioned you need to change your code to a class, but the assignment says tomake a struct, so that's not necessary.

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Setter and getter functions - basic classes

    Code:
    class MyClass
    {
    public:
    MyClass(); // constructor
    // get functions
    // set functions
    private:
    // variables to get and set
    };
    I'm just trying to be a better person - My Name Is Earl

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    Code:
    class MyClass
    {
    public:
    MyClass(); // constructor
    // get functions
    // set functions
    private:
    // variables to get and set
    };



    I guess above code is good for Class, not for Struct ?
    Last edited by kolucoms6; 03-09-2008 at 12:48 PM. Reason: Typo

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    I guess above code is good for Class, not for Struct ?
    You can use it for struct as well. The difference between the two lie mainly in the default access of members (struct: public; class: private).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I guess above code is good for Class, not for Struct ?

    In that code you can change the word class to struct and it will be exactly the same. Don't worry about class or struct. Just use struct (since the assignment asks for struct), and the code that works for class should work for struct as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM
  5. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM

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