I'm working on a program for fun but I can't figure out my first problem, also I'm aware the program doesn't end.

#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

//all prototypes 
void studentName(string&);

//all global variables

//main 
void main()
{
	do{
	string Name;
	studentName(Name);
	cout << "\nStudent Name: " << Name << endl;
	}while(true);
		
	getch();
}

//all functions
void studentName(string &studName)
{
	string Name;
	cout <<"Enter student's name: ";
	cin.ignore();
	getline(cin, Name);
}

You never set the reference that you passed. It should be this instead

//all functions
void studentName(string &studName)
{
	cout <<"Enter student's name: ";
	cin.ignore();
	getline(cin, studName); // you want to set this value that you are passing to the function as a reference
}

youre problem is

#
string Name;
studentName(Name);

you attempting to use Name variable without initializing it when you passing it to function. Also you should change void main to int main

I'm getting another problem when its run. The problem is the first letter is replaced with a blank/removed when displayed.

Remove this line from your code

cin.ignore();

it will work, but once again you have to check your loop inside main its look like a infinite loop so change it also.
I hope you understand.
Best Of Luck

Another question?
I'm going to be using 6 different arrays for all the available classes to enter "test scores" into. Now the # of test scores is not a constant variable, how can I design a code with the option to enter 1 or several test scores.

Is the number of test scores something that you can prompt the user for? If so, use a dynamic array:

std::cin >> numscores;
int * scorearray = new int[numscores];

Two errors atm: error C2664 'mathScores': cannot convert parameter 1 from 'int' to 'int[]'
and the second error is:
Intellisense argument of type "int" is incompatible with parameter of type "int*"

#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

//all prototypes 
void studentName(string&);
int mathScores(int matScores[], int SIZE);

//all global variables
const int SIZE = 7;
int matScores[SIZE];

//main 
void main()
{
	do{
	string studName;
	studentName(studName);
	cout << "\nStudent Name: " << studName << endl;
	mathScores(matScores[SIZE], SIZE);
	}while(true);
		
	getch();
}

//all functions
void studentName(string &studName)
{
	cout <<"Enter student's name: ";
	getline(cin, studName);
}

int mathScores(int matScores[], int SIZE)
{
	int total = 0;
	int i;
	for (int i=0; i<=SIZE; i++)
	{
		cout << "Math test score #" << i + 1 << ": ";
		cin >> matScores[i];
	}
	return matScores[i];
}

Line 21, just pass in matScores not matScores (which incidently, matScores refers to the (non-existent) element one past the end of the array).

Also, main should always return an int, according to the standard. void main may "work" in your compiler, but it will likely not work on a compiler that abides by the standard.

On line 43, you're only returning a single element of the array. You are changing matScores by pointer anyway, so your function doesn't need to return anything.

Edited 5 Years Ago by jonsca: n/a

Line 21, just pass in matScores not matScores (which incidently, matScores refers to the (non-existent) element one past the end of the array).

Also, main should always return an int, according to the standard. void main may "work" in your compiler, but it will likely not work on a compiler that abides by the standard.

On line 43, you're only returning a single element of the array. You are changing matScores by pointer anyway, so your function doesn't need to return anything.

So normally arrays don't return a value?

I'm not sure what you are asking.

The situation is, if you are taking the time to load up your array in this function, you'd be returning one element (which isn't a valid element anyway, firstly because your for loop goes up to and including SIZE, and secondly, because the loop is going to increment i one last time before testing it) which is two past the end.

If you are asking if you can return a whole array, you really can't. You can have your function return a pointer to an array, but it's much easier just to pass in your array as a parameter (as you are doing) and use the changed array in main after you have called the function.

void fillarray(int arr[],int size)
{
    for (int i = 0;i<size;i++)
    {
        arr[i] = i;
    }
}

int main()
{
   const int size = 10;
   int testarr[size];
   
 
   fillarray(testarr,size);
   //now testarr has "changed"
   for(int i = 0;i<size;i++)
   {
       std::cout<<testarr[i]<<" ";
   }
   std::cout<<std::endl;
}
This article has been dead for over six months. Start a new discussion instead.