class???

This is a discussion on class??? within the C++ Programming forums, part of the General Programming Boards category; im new to c++....... was trying something out using "class", and my program doesnt work proerpely............ Code: #include <iostream.h> #include ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    30

    class???

    im new to c++....... was trying something out using "class", and my program doesnt work proerpely............

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    class storenumber {
    		int numbers[6];
    	public:
    		storenumber(int, int);
    		int return_number(int c) {return numbers[c];}
    };
    
    storenumber::storenumber (int a, int b)
    {
    	numbers[b] = a;
    }
    
    int main()
    {
    	storenumber storednumber(0, 0);
    	int n, number;
    	cout << "Enter 6 numbers \n";
    	for (n = 0; n < 6; n++) {
    	cin >> number;
       storenumber storednumber (number, n);
    	}
    	for (n = 0; n < 6; n++)
    	cout << storednumber.return_number(n);
    	getch();
    	return 0;
    }

    the program accepts 6 numbers from the user and then store it in "class" until its called out. the program does run but gives out an unexpected result.
    Code:
    a
    Code tags added by kermi3

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    First, [ code ] tags.
    Second, the constructor is only called at the objects creation. The following line(s):
    Code:
    for(... etc ...) {
      storenumber storednumber (number, n);
    }
    Create a bunch of variables with local scope (one each loop cycle, which dies at the ending curly brace of the loop). Create another method (not a constructor) to set these values. As for printing the character, just cast to int on printing -- that is probably why it is printing the character 'a' (it thinks it is getting a character).

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Welcome to the board. Please read about using code tags to properly display your code when posting.

    Your constructor only assigns a single value to a single element of numbers. That's legal, but probably not what you want.


    The line below declares a new and unique object of type storenumber called storednumber each time through the loop, each object having a single value in a single element. each time through the loop you overwrite the previous storednumber.

    storenumber storednumber (number, n);


    Since numbers is a private variable you need to provide a mutator function or use the constructor to place a default value in each element. Maybe something like this:

    void enter_number(int, int);

    defined like this:
    Code:
    void storenumber::enter_number(int a, int b)
    {
       numbers[a] = b;
    }
    Then in place of this:

    storenumber storednumber (number, n);

    use this

    storednumber.enter_number(n, number);

  4. #4
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    Hi, welcome to our message board. I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do(and they'll be happier about helping you )

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    Again welcome to our boards if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    30
    ok, ill use code tag next time. oh and the "a" at the bottom of my post, its a typo! dont even know how it got there

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    30
    oh and i forgot to say "thank you" for the help -_-

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>oh and i forgot to say "thank you" for the help -_-
    Err, hope that wasn't sarcasm...

    I think you've got your 'class' understanding wrong. Declaring a 'class' is sort of like creating a new type, except it's not really. I think this is what you meant to do:
    Code:
    #include <iostream>  //No .h, because .h is the old version
    #include <conio.h>
    
    using namespace std;  //To accomodate change to #include
    
    class storenumber 
    {
    private:  //Doesn't change anything; just makes code clearer
    	int numbers[6];
    public:
    	//Constructor
    	storenumber(int, int);
    
    	//Member functions
    	void enter_number(int, int);
    	int return_number(int c) {return numbers[c];}
    };
    
    //Constructor: stores a at the index of b in numbers
    //You don't really need this, actually.
    storenumber::storenumber (int a, int b)
    {
    	numbers[ b] = a;
    }
    
    //Does the same as constructor, but can be called more than once
    //to modify the data in the existing 'storenumber' object
    void storenumber::enter_number(int a, int b)
    {
    	numbers[ b] = a;
    }
    
    int main()
    {
    	//Declare a variable of type 'storenumber', initialize with 0,0
    	storenumber storednumber(0, 0);
    	//so now storednumber.numbers[0] is 0
    
    	int n, number;
    
    	//Input the numbers
    	cout << "Enter 6 numbers \n";
    	for (n = 0; n < 6; n++) 
    	{
    		cin >> number;
    		storednumber.enter_number(number, n);
    	}
    
    	//Display the numbers
    	for (n = 0; n < 6; n++)
    		cout << storednumber.return_number(n);
    
    	getch();	//Pause so user can read output
    	return 0;
    }
    As a side note, it makes code easier to read when you indent code properly. I re-indented the code, and this is just my personal preference of how to indent, but I suggest that you get into the habit of indenting code in a regular systematic way.

    Hope this helps, if you need any more info just ask!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    30
    ok thanks hunter2. the program seems to work well with dev-c++4 but doesnt work with borland c++ 5.02 ide whatever.
    Code:
    using namespace std;
    borland c++ refers to this line of code and says "Namespace name expected". i personally prefer borland c++ and i want to make it work with borland c++. do u know wats wrong?

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm, that's odd. I believe it SHOULD work, unless your Borland compiler is outdated and doesn't support the new-style standard headers. Do you get an error on the #include <iostream>?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    30
    no... i dont get errors on that one. i guess its just my crappy compiller. -_- thnx anyway!

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm sorry, that's got me beat Maybe if someone else here who has Borland could step in now and help?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Works fine in Borland 5.5
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  13. #13
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by Chobo_C++
    ok thanks hunter2. the program seems to work well with dev-c++4 but doesnt work with borland c++ 5.02 ide whatever.
    Code:
    using namespace std;
    borland c++ refers to this line of code and says "Namespace name expected". i personally prefer borland c++ and i want to make it work with borland c++. do u know wats wrong?
    I've seen others with the same problem recently with 5.02. Doesn't appear to be standard compliant in this respect. Best bet is simply to drop the using directive and go back to <iostream.h> until you can update your compiler.
    Gambling. The new yoga, with cash prizes!

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Another thing, not sure if it'll work or not since the using statement didn't work, but anyways...

    instead of just "cout << something", with the new header (iostream without the .h) you need to say "std::cout << something", unless you put the "using namespace std;" line in. But if you don't put the 'using' in, you can still try putting the full name with std:: in front. In fact, that's what I usually do anyways, because I prefer to not take 'shortcuts' A mere peculiarity on my part, of course, but it might help you out anyways if you don't want to use the .h (I think it might be nonstandard now to use .h).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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