Hi. I am having a small problem with an exercise from Bronson's "A First Book of C++" second edition. It can be found in section 9.3, exercise #6. Here is my code and the comments tell what it is supposed to do:
-----------------------------------------
/*
Date: 2003.12.10
Purpose: Write a function that will accept ten lines of user-input text and store the entered lines
as ten individual strings. Use a pointer array in your function.
*/
#include <iostream>
using namespace std;
const int MAXCHARS = 80;
const int MAX = 10;
void getLines(char *m);
int main()
{
char *messages = new char[MAX];
char *pnt[MAX];
int i;
for(i=0; i<MAX; i++)
{
cout << "Enter a string: ";
cin.getline(messages+i, MAXCHARS);
}
//getLines(messages);
for(i=0; i<MAX; i++)
{
//*(pnt+i) = *(messages+i);
cout << "Line " << i+1 << ": " << *(messages+i) << endl;
}
return 0;
}
// get the user input
void getLines(char *m)
{
int i = 0;
while(i<MAX)
{
cout << "Please enter a string: ";
cin.getline(m+i, MAXCHARS);
m++;
i++;
}
}
/*
Analysis:
*/
-----------------------------------------
Any ideas? cin.getline() only grabs one character, what's wrong with me?
Netsniper
-----------------------------------------
/*
Date: 2003.12.10
Purpose: Write a function that will accept ten lines of user-input text and store the entered lines
as ten individual strings. Use a pointer array in your function.
*/
#include <iostream>
using namespace std;
const int MAXCHARS = 80;
const int MAX = 10;
void getLines(char *m);
int main()
{
char *messages = new char[MAX];
char *pnt[MAX];
int i;
for(i=0; i<MAX; i++)
{
cout << "Enter a string: ";
cin.getline(messages+i, MAXCHARS);
}
//getLines(messages);
for(i=0; i<MAX; i++)
{
//*(pnt+i) = *(messages+i);
cout << "Line " << i+1 << ": " << *(messages+i) << endl;
}
return 0;
}
// get the user input
void getLines(char *m)
{
int i = 0;
while(i<MAX)
{
cout << "Please enter a string: ";
cin.getline(m+i, MAXCHARS);
m++;
i++;
}
}
/*
Analysis:
*/
-----------------------------------------
Any ideas? cin.getline() only grabs one character, what's wrong with me?
Netsniper