0

My homework problem is to modify code written for a previous assignment that saved an array (of user-specified size) of test scores so that it also saves a parallel array of student names associated with these test scores. We are to use pointers and pointer notation whenever possible.

My problem occurs early on: I ask for the student's full name and receive the input using the getline cin object and cin.ignore() to avoid passing the last name into my next cout/cin request.

However, when I attempt to recall the student name using either pointer notation (deref the pointer) or array notation all I get is blank space. String objects have been the most difficult for me to grasp this quarter, so it is possible that it is just that I am missing a crucial bit of reasoning here. I have gone back and read through my text but it is not incredibly helpful as it shows my exact format for a similar desired result. Also, most online research has been yielding info that is well beyond a first quarter c++ student like myself. Any help is very much appreciated! Here is the snippet of code where I think my problem lies:

#include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;

void sortArray(int*, string*, int);
void averageScore(int*, int);



int main()
{
    //pointer to point to test scores
    int* scores; 
    string* student;
    // integer test to indicate size of array
    int test;

    // get user defined array size
    cout << "How many tests would you like to save?" << endl;
    cin >> test;

    // define dynamically allocated array scores
    scores = new int[test];
    student = new string[test];

    // for loop to enter each test score as an element of scores array
    for (int i = 0; i < test; i++)
    {
        cout << "Enter the student's name: " << endl;
        getline(cin, student[i]);
        cin.ignore();


        cout << "Enter the test score for " << *(student+i) << " : "<< endl;
        cin >> *(scores+i);

EDIT:

I played around with it and was able to get the results I wanted with the following code:

    for (int i = 0; i < test; i++)
    {
        cout << "Enter the student's first name: " << endl;
        cin >> first;
        cout << "Enter the student's last name: " <<endl;
        cin >> last;

        *(student+i) = first + " " + last;



        cout << "Enter the test score for " << *(student+i) << " : "<< endl;
        cin >> *(scores+i);
  • Your `cin.ignore()` call is pointless because the next extraction discards the leading whitespace anyway. You also have a [bug](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). – chris Aug 09 '14 at 23:02
  • Thank you, these cin objects (particularly working with strings) has been very confusing, and my text does not clarify the string part very well. Can I ask why you think this question might be downvoted? I tried to follow all the guidelines, and I thought I did a thorough job explaining the problem. I am new to programming, I do research before asking, but it always seems to not be good enough. – Giraffelope Aug 09 '14 at 23:06
  • 5
    `We are to use pointers and pointer notation whenever possible.` Almost exactly the opposite of what is done in the real world of C++ programming. – PaulMcKenzie Aug 09 '14 at 23:06
  • Thank you, and that is fine. I understand. However, it is just a small assignment to reinforce the chapter we just completed on pointers. So, I think that is why they ask that. – Giraffelope Aug 09 '14 at 23:13
  • There is too much story-telling in the question. I'm still not sure exactly what is the problem. It would help to post the entire code so I could compile and run it myself. – Neil Kirk Aug 09 '14 at 23:37

0 Answers0