|
-
Jan 27th, 2002, 03:00 AM
#1
Thread Starter
Addicted Member
the newbie returneth
#include<iostream.h>
#include<conio.h>
const int MAX=4;
class person
{
char name[10];
public:
void display()
{
cout<<name;
}
void accept()
{
cout<<"ENter Name";
cin>>name;
}
};
void main()
{
person *parr[MAX];
for(int i=0;i<MAX;i++)
{
parr[i]=new person;
parr[i]->accept();
}
for(int j=0;j<MAX;j++)
{
parr[i]->display();
}
getch();
}
1)..#include<Iostream> does not work..it doesnt recognise cin/cout/<</>> etc...I have 2 add the ".h" at the end.
2) getch()..the function I think is supposed 2 b used to halt the program so tt the user can read the screen output before the Window vanishes...it apparently stops the program right before any output is done on screen...
3)..the program here is giving me garbage output..cant figure out y.Probably the error would be painfully obvious the next time I look at the code..but still...
-
Jan 27th, 2002, 07:40 AM
#2
Monday Morning Lunatic
1. You need to have a using namespace std; in there.
2. Before the getch, use cout << flush;
Code:
#include<iostream>
#include <string>
#include<conio.h>
using namespace std;
const int MAX = 4;
class person {
string name; // Changed this so you're not limited on names
public:
void display() {
cout << name << endl;
}
void accept() {
cout << "Enter Name: ";
cin >> name;
}
};
int main() {
person parr[MAX]; // You don't need a pointer since it's fixed
for(int i = 0; i < MAX; ++i) {
parr[i]->accept();
}
for(int j = 0; j < MAX; ++j) {
parr[j]->display(); // Changed this to j from i
}
cout << flush; // endl actually does this anyway, but its good practice
getch();
return 0;
}
Hopefully the string change should fix 3.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 27th, 2002, 12:34 PM
#3
<< and >> aren't overloaded for string. Use getline to input it and cout << name.c_str() for output.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 27th, 2002, 12:36 PM
#4
Monday Morning Lunatic
They are, they act in exactly the same way you'd expect (get up to the first whitespace character).
getline gets up until a newline.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 28th, 2002, 08:35 AM
#5
Strange, I usually get an error when doing
string str("Hello!");
cout << str << endl;
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 28th, 2002, 01:02 PM
#6
Monday Morning Lunatic
You have to be using the templated iostreams (#include <iostream>) or it won't work.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 28th, 2002, 01:16 PM
#7
I do. But I haven't tried in a while. I'll do it now.
Ok, it works. Never mind.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|