I have a problem...

I am just starting to learn how to create functions and call them. I have to write a program that does what you will see in the main() part of the program but yeah. My big problem comes when you run this I get an error saying so and so function does not take 0 parameters. However when I do add parameters it says ")" is not supposed to be there. Helllpp!! Its due by midnight tonight lol. I dont want some one to do it for me just explain to me the correct answer.



#include <iostream>
#include <cmath>

using namespace std;

void drawTriangle(int height, char character)
{
cout << "Type in a character to use for the triangle.\n";
cin >> character;

cout << "Type in the height.\n";
cin >> height;

for(int i = height; i > 0; i--)
{
cout << endl;
for(int j = 0; j < i; j++)
cout << character;
}
}

void someNumbers(int dataEntered, int amtOfNum, double average, int sum, int min = 0, int max = 0)
{
cout << "Enter the amount of numbers you are going to enter.\n";
cin >> amtOfNum;


for(amtOfNum; amtOfNum > 0; amtOfNum--){
cout << "Enter the numbers.\n";
cin >> dataEntered;
sum = sum + dataEntered;


if (max < dataEntered)
max = dataEntered;

if (min > dataEntered)
min = dataEntered;
}

average = sum / amtOfNum;

cout << "Average = " << average << endl;
cout << "Sum = " << sum << endl;
cout << "Max = " << max << endl;
cout << "Min = " << min << endl;
}


void someNumbersPartDeux(int dataEntered, int amtOfNum, double average, int sum, int min = 0, int max = 0)
{

for(amtOfNum = 0; dataEntered > -9999; amtOfNum--){
cout << "Enter the numbers. Type -9999 to end.\n";
cin >> dataEntered;
sum = sum + dataEntered;
average = sum / amtOfNum;

if (dataEntered = -9999)
break;

if (max < dataEntered)
max = dataEntered;

if (min > dataEntered)
min = dataEntered;
}

cout << "Average = " << average << endl;
cout << "Sum = " << sum << endl;
cout << "Max = " << max << endl;
cout << "Min = " << min << endl;
}

void powers(double number, double exponent, double answer)
{
cout << "Enter a number.\n";
cin >> number;

cout << "Now enter an exponent to raise the number by.\n";
cin >> exponent;

answer = pow(number,exponent);

cout << number << " raised to power " << exponent << " equals " << answer << endl;
}

int main()
{

int numberEntered;

cout << "Welcome to the my program\n"
"I can do several things for you.\n"
"When you are ready to enter my program\n"
"Hit any key";

system("cls");

cout << "Please choose from:\n"
"1. draw an inverted triangle of your chosen height using your chosen letter.\n"
"2. enter some numbers and learn the sum, average, min and max of your inputs.\n"
"3. same 2. but with a different way to end inputting.\n"
"4. let me calculate a^b (a raised to the b) for whatever a and b you'd like.\n"
"5. quit this program.";

cin >> numberEntered;

switch (numberEntered) {
case 1:
drawTriangle();
break;
case 2:
someNumbers();
break;
case 3:
someNumberPartDeux();
break;
case 4:
powers();
break;
case 5:
cout << "Thank you.\n";
system("end");
}


}
Yea have clearly missed the class on function parameters vs variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void drawTriangle(int height, char character)
{
// Problem, your height and character are function parameters. I don't know why they are, but they shouldn't be.
// They should be local variables declared within the function.

cout << "Type in a character to use for the triangle.\n";
cin >> character;

cout << "Type in the height.\n";
cin >> height;

for(int i = height; i > 0; i--)
{
cout << endl;
for(int j = 0; j < i; j++)
cout << character;
}
}
Thanks alot man. I actually got the program to run but I had to move all the parameters to the inside of the function. One question, if the function was int instead of void, I would put parameters...? And I wouldnt put any for void...? I'm really foggy on it....
No, parameters are for a different purpose. The return type makes no difference for whether you want parameters or not.

This page should have all the info you need:
http://www.cplusplus.com/doc/tutorial/functions.html
Topic archived. No new replies allowed.