Inputting Data Into New Objects

J

jerryalan

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 = 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've tried cin.getline(ps); 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,
 
M

Mike Wahler

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 = 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've tried cin.getline(ps); 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
 
J

John Harrison

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 = 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've tried cin.getline(ps); 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);

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top