Well people... I already know that c++ is not what I want to do in the future, but I do have to pass this class. That said, I need help with this array. I understand how they work, but my instructor wants us to use them in a function!!!!! I have tried for hours to maker it work but it will not process properly in main. I am trying to establish a dynamic array in which a person wants to input the number of students the user needs to input grades for. Here is what I have:

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

	void getStudentCount(int students); 
	void getExamScores(int score); 
	void getLabScores(int labArray);
	void calculatePointGrades(int ); 
	void calculateLetterGrades(int); 
	void showGradeData(int); 



int main()
{

	int increment,
		score,
		students;
	
		
		getStudentCount(students);
				
		for(increment = 0; increment < students; increment++)
		{
			cout << stuArray[increment] << endl;
		}
		getExamScores(score);
	

//	if(gradeAvg >= 90)
//		cout<< "A";
//	else if (gradeAvg >= 80)
//		cout<< "B";
//	else if (gradeAvg >= 70)
//		cout<< "C";
//	else if (gradeAvg >= 60)
//		cout<< "D";
//	else
//		cout<< "F";
	


cin.ignore(2);
return 0;
}

int getStudentCount(int students) 
{
	int* stuArray;
	cout << "Enter the amount of students you wish to process\n" << endl;
	cin >> students;
	
	stuArray = new int[students];

	return stuArray;
}

void getExamScores(int score)
{	
	cout << "Please enter in the scores for your student's Exam\n:" << endl;
	cin >> score;
}

Afterwords, I am supposed to input the grades for the amount of students and some more stuff. Can anyone help?

In case you people we curious, this is what I would have come up with without using functions:

#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
	int* students;
	int increment,
		numberOfStudents;

	cout << "Please enter in the amount of students needed:\n" << endl;
	cin >> numberOfStudents;
	cout << "" << endl;

	students = new int[numberOfStudents];

	for(increment = 0 ; increment < numberOfStudents; increment++)
	{
		cout << "Please give me the score of student " << increment + 1 << "'s exam: "; 
		cin >> students[increment];
	}

	cout << "The scores received were:\n" << endl;

	for(increment = 0; increment < numberOfStudents;increment++)
		cout << students[increment] << endl;
		cout << " " << endl;

system("pause");
return 0;
}

What do you think?

Looks to me like you are mixing a couple concepts. For starters, you have at the top, your function prototypes... those say that getStudentCount is returning no value to the main function. However, when you actually code the function, you say that it is returning an int. Now, in the code for the getStudentCount function, you also use a pointer to allocate a new array on the heap (stuArray)... then you try to return this pointer to an int. However, your function is trying to just return an int.... not a pointer to one. I'm not sure why you also pass the students variable to the function, I can only image you want to use that to keep track of the student count for your loop in main... but in order to do that, it either needs to be a pointer, or a reference, so that the cin in getStudentCount can modify it. If you don't make getStudentCount return a pointer (which it doesn't so far, but tries to) then you will have a memory leak (your program allocates memory, and loses the pointer to it).

Also, it looks to me as though you are confusing some concepts. Global variables exists throughout the entire program... all functions, etc. However, a number of your variables are NOT global, they are local. You see, you declare stuArray in getStudentCount, and then try to use it after you jump back to main. You can't do that. Once the closing brace (curly bracket) is reached for the getStudentCount function (probably happens at the return line, but whatever), the stuArray variable is destroyed. Kaplooie, kaput. No more. Gone. So when the code jumps back to main, that variable is gone.... you can't use it. Attached is the way I would go about doing it.... with the annotated lines that were altered:

#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

int* getStudentCount(int &students);		// Modified  Note * and &
void getExamScores(int score);
void getLabScores(int labArray);
void calculatePointGrades(int );
void calculateLetterGrades(int);
void showGradeData(int);



int main(int argc, char **argv)                  // Modified
{
	int increment, score, students;
	int *stuArray;			// Modified
	
	stuArray = getStudentCount(students); // Modified

	for (increment = 0; increment < students; increment++) {
		cout << stuArray[increment] << endl;  // Modified

	}

	getExamScores(score);


// if(gradeAvg >= 90)
// cout<< "A";
// else if (gradeAvg >= 80)
// cout<< "B";
// else if (gradeAvg >= 70)
// cout<< "C";
// else if (gradeAvg >= 60)
// cout<< "D";
// else
// cout<< "F";

	cin.ignore(2);
        delete [] stuArray;            // Modified
	return 0;
}

int* getStudentCount(int &students)    // Modified
{
	int *stuArray;
	cout << "Enter the amount of students you wish to process\n" << endl;
	cin >> students;

	stuArray = new int[students];

	return stuArray;
}

void getExamScores(int score)
{
	cout << "Please enter in the scores for your student's Exam\n:" << endl;
	cin >> score;
}

This isn't really how I would do it.... I don't really like passing in buffer values to get filled by the function, but it works with your existing code attempt. Good luck.

Thanks comatose you have come through for me again! I did try the way you have it, but I did not include the '&' and the 'int*' for the function 'getStudentCount(int &students)' when I did the way I just explained, It would give me syntax errors stating that I had uninitialized variables everywhere. I'll Give it another shot and report back.

Ok comatose, I have edited my code to suit yours, but I have been trying for an hour to do the next process. The second code I posted shows me receiving a exam score from the user. I want to put that into the getExamScores function, but I am having trouble retrieving the new array type. The following code shows that I am trying to use a pointer as I did in the first function. Can you help?

#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

int* getStudentCount(int& students); //to get the user defined student count		
void getExamScores(int& increment);  //get the exam scores
void getLabScores(int labArray); // get the lab scores
void calculatePointGrades(int ); //calculate students total grade
void calculateLetterGrades(int); //assign  letter grade to the total 
void showGradeData(int);	 //show all students average
void intArrayAve();		
void doubleArrayAve();	



int main()                  
{
	int increment, students;
	int* stuArray;			// pointer for the array
	
	stuArray = getStudentCount(students); 
	getExamScores(increment);


// if(gradeAvg >= 90)
// cout<< "A";
// else if (gradeAvg >= 80)
// cout<< "B";
// else if (gradeAvg >= 70)
// cout<< "C";
// else if (gradeAvg >= 60)
// cout<< "D";
// else
// cout<< "F";

	cin.ignore(2);
    delete [] stuArray;            
	return 0;
}

int* getStudentCount(int& students)    
{
	int* stuArray;
	cout << "Enter the amount of students you wish to";  
               << "process\n"  << endl;
	cin >> students;

	stuArray = new int[students];

	return stuArray;
}

void getExamScores(int& students, int& increment)
{	
	int* stuArray;
	for (increment = 0; increment < students; increment++)
	{	cout << "Please give me the score of student ";  
                       << increment + 1 << "'s exam: "; 
		cout << stuArray[increment] << endl; 
	}
}

what do you think?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.