Inputting Data Into New Objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jerryalan@gmail.com

    #1

    Inputting Data Into New Objects

    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

  • Mike Wahler

    #2
    Re: Inputting Data Into New Objects


    <jerryalan@gmai l.com> wrote in message
    news:1131921099 .231973.65330@g 47g2000cwa.goog legroups.com...[color=blue]
    > 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?[/color]

    #include <iostream>
    #include <string>

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

    -Mike


    Comment

    • John Harrison

      #3
      Re: Inputting Data Into New Objects

      jerryalan@gmail .com wrote:[color=blue]
      > 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?
      >[/color]

      Simple

      getline(cin, ps[i]);

      John

      Comment

      • Jerry

        #4
        Re: Inputting Data Into New Objects

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

        --
        Jerry

        Comment

        Working...