I've looked around for about an hour and can't find anything that was to terribly helpful, or I didn't know how to use it. So here it is.

#include<iostream>
#include<new>
#include<cstdlib>
#include<string>
using namespace std;

struct student {
	string name;
	double gpa;
};

student doSurvey(student s);
void reportSurvey(student s);

int main()
{

	student myStu;
	student * pStu;
	pStu = &myStu;
	int  i, n;

	cout << "How many surveys are going to be entered? ";
	cin >> i;
	pStu= new (nothrow) student[i];

	if(i == 0){
		cout << "No surveys entered.";
	} else {
		for(n=0; n<i; n++){
			doSurvey(pStu[i]);
		}
		for(n=0; n<i; n++){
			reportSurvey(pStu[i]);
		}
	}
	system("PAUSE");
	return 0;
}

student doSurvey(student s){
	cout << "Students name: ";
	cin >> s.name;
	cout << "Students GPA: ";
	cin >> s.gpa;
	return s;
}

void reportSurvey(student s){
	cout << "Name: " << s.name << endl;
	cout << "GPA: " << s.gpa << endl;
}

Exact Error:

error C2146: syntax error : missing ';' before identifier 'pStu'

Error Code:

pStu= new (nothrow) student pStu[i];

Still trying to figure out how to use pointers and everything so I probably messed up with them.

Edited 4 Years Ago by FierstArter: n/a

pStu= new (nothrow) student pStu[i];

Should be this:

pStu= new (nothrow) student[i];

You also need to include <cstdlib> because that's where system() is declared.

Sure enough, that solved the problem, but brought another problem.

The program runs but once I enter the number for "i" it breaks and throws me this.

Unhandled exception at 0x1026ed6a (msvcr90d.dll) in Week1 - Practical.exe: 0xC0000005: Access violation reading location 0xabababab.

Is this a problem with the memory be allocated?


Edit: Looking into it a little more. It says <Bad ptr> when going line by line when it gets to pStu.name

Edited 4 Years Ago by FierstArter: n/a

After a bit of tweaking it works fine now. Here is the code for anyone interested.


Could someone explain to me the difference between or point at what to search for.
student *doSurvey(student *s, int i)
vs
student doSurvey(student s, int i)

#include<iostream>
#include<new>
#include<cstdlib>
#include<string>
using namespace std;

struct student {
	string name;
	double gpa;
};

student *doSurvey(student *s, int i){
	int n;
	for(n=0;n<i;n++){
		cout << "Students name: ";
		cin >> s[n].name;
		cout << "Students GPA: ";
		cin >> s[n].gpa;
	}
	return s;
}
void *reportSurvey(student *s, int i){
	int n;
	for(n=0;n<i;n++){
		cout << "Name: " << s[n].name << endl;
		cout << "GPA: " << s[n].gpa << endl;
	}
	return 0;
}

int main()
{

	student myStu;

	int  i;

	cout << "How many surveys are going to be entered? ";
	cin >> i;
	student* pStu= new student[i];

	if(i == 0){
		cout << "No surveys entered.";
	} else {
		doSurvey(pStu, i);
		reportSurvey(pStu, i);
	}
	system("PAUSE");
	return 0;
}

Edited 4 Years Ago by FierstArter: n/a

This article has been dead for over six months. Start a new discussion instead.