| | |
Need help with functions
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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!
C++ Syntax (Toggle Plain Text)
// 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
You might want to try compiling and working your way through the errors. These two lines will definitely cause issues.
c++ Syntax (Toggle Plain Text)
// call function calcAverage int calcAverage(int score1, int score2, int score3); // call function printStars void printStars(int avg, int id);
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
•
•
Join Date: Mar 2008
Posts: 1,425
Reputation:
Solved Threads: 115
When calling a function, you don't have to but the type it returns before it.
For example, change line 23 to:
Hope this helps.
For example, change line 23 to:
C++ Syntax (Toggle Plain Text)
calcAverage(score1, score2, score3);
Hope this helps.
I need pageviews! most fun profile ever :)
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:
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..
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:
C++ Syntax (Toggle Plain Text)
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..
![]() |
Similar Threads
- "Parallel" execution of functions (C++)
- VB's Left, Right, Mid Functions in C++? (C++)
- Double Linked Lists and Functions required (C++)
- How to write FNVAL functions (Java)
- I dont see any difference between these 2 functions, DO YOU? (C)
- access Digital Camera Functions (C++)
Other Threads in the C++ Forum
- Previous Thread: curl and xerces-c
- Next Thread: Void functions for c++
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






