I am just learning c++ and, I am trying to create a function out of the lines that are surrounded by stars. I am at my wits end! Does anyone have a suggestion or reference that might help me figure this out.

Here is my code:

int a, b, c, x, y, z; // global variables

double examGrades[100]; // arrays


int getStudentNumber(int studentNumber); // prototypes


int main () // main program
{
z = getStudentNumber(z); // fn - input student qty.
cout << endl; 

//************************************************************************ 

cout << "Enter the EXAM grades for this class: " << endl;

for (y=0; y<=z; y++)
{
cin >> examGrades[y];
} 

//****************************************************************************

cin.ignore(2);
return 0;
}

int getStudentNumber(int studentNumber)
{ 
int students = 1;
cout << "Enter the number of students in the class: "<<endl;
cin >> students;
students = students - 1;
return students;
}

My first attempt:

void getExamGrades()

int main()
{
...code
getExamGrades();
...code
}

void getExamGrades()
{
cout << "Enter the EXAM grades for this class: " << endl;
for (y=0; y<=z; y++)
{
cin >> examGrades[y];
} 
}

My second attempt:

#include <iostream>			
#include <cmath>	
#include <iomanip>
#include <stdio.h>
#include <ctype.h>
#include <fstream>

using namespace std;

int a, b, c, x, y, z;                         // global variables

double examGrades[100];


int getStudentNumber(int studentNumber);      // prototypes
int getExamGrades(int eGrades, int sNum);


int main ()                                   // main program
{
		z = getStudentNumber(z);              // fn - input student qty.
		cout << endl;	
        getExamGrades();	
		examGrades[] = getExamGrades();

	    cin.ignore(2);
		return 0;
}

int getStudentNumber(int studentNumber)
{    
    int students = 1;
	cout << "Enter the number of students in the class: "<<endl;
	cin >> students;
	students = students - 1;
	return students;
}

int getExamGrades(int eGrades, int sNum)
{
    cout << "Enter the EXAM grades for this class: " << endl;
    for (y=0; y<=z; y++)
    {
    cin >> examGrades[y];
    }	
}

Any help would be appreciated.

Recommended Answers

All 2 Replies

There are 2 ways to do it. You can wrap the function in a loop and simply store the return of the function to an element of the array, or you can pass the array to the function as an argument and handle all of the input and storage directly in the function.

Here is a portion of a tutorial on arrays. Be sure to read the "Arrays as Parameters" section of it.

Option 1:

int inputFunction();

int main() {
  const int ARRAY_SIZE = 5;
  int myArray[ARRAY_SIZE] = {0};

  //...

  for (int i = 0; i < ARRAY_SIZE; ++i) {
    myArray[i] = inputFunction();
  }

  //...

}

int inputFunction() {
  int inputValue;

  //perform input operation...

  return inputValue;
}

Option 2:

void inputFunction(int[], const int);

int main() {
  const int ARRAY_SIZE = 5;
  int myArray[ARRAY_SIZE] = {0};

  //...

  inputFunction(myArray, ARRAY_SIZE);

  //...

}

void inputFunction(int []myArray, const int CAPACIY) {
  int userInput = 0;
  for (int i = 0; i < CAPACITY; ++i) {
    //prompt user for input
    //get user input
    myArray[i] = userInput;
  }
}

Your eGrades and sNum haven't been declared anywhere in the program.

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.