| Off Topic > Off Topic |
| Programming Megathread |
| << < (124/241) > >> |
| Ravencroft·:
So I'm trying to make a program that calculates the average of five numbers by sending them to a function called SUM to calculate the sum, then return to MAIN to calculate the average. I'm having trouble figuring out how to return to MAIN though. Here's what I have so far: http://codepaste.net/me8fpa This is my first program using functions so point out any other syntax issues there might be. |
| Headcrab Zombie:
First off, return isn't a function, so you don't do return(SUM); It's just return SUM; Then you need a call to that function after the inputs: float sum = SUM(a,b,c,d,e); Also, you're missing an ending curly brace at the end of main |
| Ravencroft·:
--- Quote from: Headcrab Zombie on May 03, 2016, 11:59:22 AM ---First off, return isn't a function, so you don't do return(SUM); It's just return SUM; Then you need a call to that function after the inputs: float sum = SUM(a,b,c,d,e); Also, you're missing an ending curly brace at the end of main --- End quote --- That's odd. My lecture notes say to have parentheses around the return variable. There's even parentheses on it on the example code snippets. And yeah I know I'm missing a brace. I tried removing it thinking the main function had to be wrapped around the sum function. So how exactly would I continue with the main function after returning? Do I put the code in the first main function brackets or start a new main function after the sum function(talking about the part where the average is calculated and output to the user)? |
| Pecon:
--- Quote from: Ravencroft· on May 03, 2016, 12:44:35 PM ---That's odd. My lecture notes say to have parentheses around the return variable. There's even parentheses on it on the example code snippets. And yeah I know I'm missing a brace. I tried removing it thinking the main function had to be wrapped around the sum function. So how exactly would I continue with the main function after returning? Do I put the code in the first main function brackets or start a new main function after the sum function(talking about the part where the average is calculated and output to the user)? --- End quote --- The main issue is that you've left the SUM function inside the main function, when it's supposed to be outside the main function. Like so: --- Code: ---#include <stuff> using namespace std; float SUM(float a, float b, float c, float d, float e) { return a + b + c + d + e; } int main(void) { // Your code defining values and collecting their data float answer = SUM(a, b, c, d, e); cout << "The answer is " << float << endl; } --- End code --- |
| TableSalt:
--- Quote from: Ravencroft· on April 18, 2016, 09:02:12 PM --- --- End quote --- hello did you say my name |
| Navigation |
| Message Index |
| Next page |
| Previous page |