我的作业问题是修改为之前的作业编写的代码,该代码保存了一组(用户指定的大小)测试分数,这样它也可以保存与这些考试分数相关的并行学生名称数组 . 我们尽可能使用指针和指针表示法 .
我的问题发生在早期:我要求学生的全名并使用getline cin对象和cin.ignore()接收输入,以避免将姓氏传递给我的下一个cout / cin请求 .
但是,当我尝试使用指针符号(deref指针)或数组符号来回忆学生名称时,我得到的只是空格 . 字符串对象对我来说是最难掌握的,所以有可能只是因为我错过了一点关键的推理 . 我已经回过头来阅读我的文字,但它并不是非常有用,因为它显示了我的确切格式以获得类似的期望结果 . 此外,大多数在线研究已经产生的信息远远超出像我这样的第一季度学生 . 很感谢任何形式的帮助!以下是我认为我的问题所在的代码片段:
#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);
编辑:
我玩了它,并能够通过以下代码获得我想要的结果:
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);