-
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 tags added by kermi3
-
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).
-
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);
-
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
-
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
-
oh and i forgot to say "thank you" for the help -_-
-
>>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! :cool:
-
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?
-
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>?
-
no... i dont get errors on that one. i guess its just my crappy compiller. -_- thnx anyway!
-
Hmm sorry, that's got me beat ;) Maybe if someone else here who has Borland could step in now and help? :)
-
Works fine in Borland 5.5
-
Quote:
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.
-
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).