I need help with my homework assignment for school. I was able to get everything to work fine when I just put everything in int main() without using functions but when I tried to separate everything into functions the program is not running correctly and although I don't get an error compiling when I try to run the program it keeps exiting out. Can someone please help? Thanks!

// This program calls 3 functions: the 1st one has the user enter an ID and 3 test scores which range from 0-50 and checks to see
// if done correctly, if not then it asks the user again, the 2nd one takes the 3 test scores from the 1st function and then finds
// the average, and the 3rd one accepts the average and id and prints the id followed by a line of stars based on the
// whole # part of the average. The user can also repeat this as many times as they want.

#include "stdafx.h"
#include <iostream>
using namespace std;

void getIdScores(int &id, int &score1, int &score2, int &score3);
int calcAverage(int score1, int score2, int score3);
void printStars(int avg, int id);

int main()
{
	// declare variables
	int id, score1, score2, score3;

	// call function getIdScores
	getIdScores(id, score1, score2, score3);

	// call function calcAverage
	int calcAverage(int score1, int score2, int score3);

	// call function printStars
	void printStars(int avg, int id);

	return 0;
}

void getIdScores(int &id, int &score1, int &score2, int &score3)
{
	// loop if test scores are invalid
    while (true)
    {
	// get id #
	cout << "Please enter your ID#: ";
	cin >> id;

        // get 3 test scores
        cout << "Please enter 3 test scores (separated by spaces & b/w 0-50): ";
        cin >> score1 >> score2 >> score3;
    
        // test if it is in the right format
        if ((score1 > 0 && score1 < 50) ||(score2 > 0 && score2 < 50) || (score3 > 0 && score3 < 50)) break;
	cout << "Invalid test scores. Please try again." << endl;
    } // while
} // getIdScores

int calcAverage(int score1, int score2, int score3)
{
        // find average
	int avg = (score1 + score2 + score3) / 3;

	return avg;
} // average

void printStars(int avg, int id)
{
	// print id to screen
    cout << "The id is " << id;

	// loop to print out the number of stars of the average
	for (int i=0; i<avg; i++)
    {
		cout << "*";
    } // for
} // printStars

Recommended Answers

All 3 Replies

You might want to try compiling and working your way through the errors. These two lines will definitely cause issues.

// call function calcAverage
int calcAverage(int score1, int score2, int score3);

// call function printStars
void printStars(int avg, int id);

When calling a function, you don't have to but the type it returns before it.

For example, change line 23 to:

calcAverage(score1, score2, score3);

Hope this helps.

As ShawnCplus has mentioned, the calls to the functions are incorrect.
You aren't returning your average value from the calcAverage function into a variable so you won't be able to pass it into your printing function.

When you call the calcAverage function it should return into a variable:

int avg = calcAverage(int score1, int score2, int score3); 
//return value from this function is copied into the avg variable..

This will allow you to pass the avg variable into your print function. You might think about drafting up a simplified version and reading up on returning variables from functions.
Hope that helps..

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.