By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,649 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

Inputting Data Into New Objects

P: n/a
I'm doing an assignment for class where I need to create a gradebook.

I have it working well but I'm having a hard time getting student names
inputted with spaces. I am using a pointer to create a new object (to
dynamically size the student array to the size specified by the
teacher).

Here is my code:
string * ps = new string[students];
for(int i = 0; i < students; i++)
sa[i] = new int[assignments];
cout << "\nNow I need you to enter the names of your students\n";
for (int i = 0; i < students; i++) {
cout << "Please enter the name of student " << i + 1 << ": ";
cin >> ps[i];
}

I've tried cin.getline(ps[i]); but that gave me a compiler error (error
C2664). I'm using the Sams book: C++ Primer Plus and I've searched for
a while but can't fine the solution. Can anyone help me with this so I
can enter first and last names of students?

Thanks,

--
Jerry

Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

<je*******@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
I'm doing an assignment for class where I need to create a gradebook.

I have it working well but I'm having a hard time getting student names
inputted with spaces. I am using a pointer to create a new object (to
dynamically size the student array to the size specified by the
teacher).

Here is my code:
string * ps = new string[students];
for(int i = 0; i < students; i++)
sa[i] = new int[assignments];
cout << "\nNow I need you to enter the names of your students\n";
for (int i = 0; i < students; i++) {
cout << "Please enter the name of student " << i + 1 << ": ";
cin >> ps[i];
}

I've tried cin.getline(ps[i]); but that gave me a compiler error (error
C2664). I'm using the Sams book: C++ Primer Plus and I've searched for
a while but can't fine the solution. Can anyone help me with this so I
can enter first and last names of students?


#include <iostream>
#include <string>

int main()
{
std::string name;
std::cout << "Enter name: ";
std::getline(std::cin, name); /* note: not the same as cin.getline() */
std::cout << "Name is: " << name << '\n';
return 0;
}

-Mike
Nov 22 '05 #2

P: n/a
je*******@gmail.com wrote:
I'm doing an assignment for class where I need to create a gradebook.

I have it working well but I'm having a hard time getting student names
inputted with spaces. I am using a pointer to create a new object (to
dynamically size the student array to the size specified by the
teacher).

Here is my code:
string * ps = new string[students];
for(int i = 0; i < students; i++)
sa[i] = new int[assignments];
cout << "\nNow I need you to enter the names of your students\n";
for (int i = 0; i < students; i++) {
cout << "Please enter the name of student " << i + 1 << ": ";
cin >> ps[i];
}

I've tried cin.getline(ps[i]); but that gave me a compiler error (error
C2664). I'm using the Sams book: C++ Primer Plus and I've searched for
a while but can't fine the solution. Can anyone help me with this so I
can enter first and last names of students?


Simple

getline(cin, ps[i]);

John
Nov 22 '05 #3

P: n/a
That worked. Thanks John! I had a feeling it'd be something easy.

--
Jerry

Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.