Computing.Net > Forums > Programming > Error message C++

Error message C++

Reply to Message Icon

Original Message
Name: feierstarter1
Date: March 18, 2006 at 22:47:23 Pacific
Subject: Error message C++
OS: XPP
CPU/Ram: Mobile 1,9, 1GB
Model/Manufacturer: Compaq
Comment:

Hi there,
it is homeworkthe calculation!!!
im trying to use functions (not homework)
i would like do it without arrays (not homework)

I need a little bit help with the error message
error C2448: '<Unknown>' : function-style initializer appears to be a function definition

the message appears if i compile and the compiler stops by line 99 and 117 (functions)

and i need some good ideas to make the code really good (ANSI), maybe easier and shorter than now!

how can i use float average and not double?


Thanks for your help
here the code:

//C++ program that reads a series of integer numbers (between 0 and 100), calculates and prints
//sum of first and last numbers
//average of first three numbers
//product of last two numbers
//largest of all numbers
//greatest common divisor(gcd) of last two numbers
//factorial of last two numbers
//grade for all numbers (H:85-100, D:75-84, C65-74, P:50-64, F:0-49)


//Constant and type definitions are included in header file.

#include <iostream.h> // header file for input/output stream
#include <stdlib.h> // header file for standard library
#include <math.h> // header file for mathmatic

int first_number;
int second_number;
int third_number;
int fourth_number;
int last_number;

// function prototypes
int sum;
double average;
double product;
int largest;
double gcd;
int factorial;
int grade;

------
// function main begins program execution
int main()
{

cout << "Welcome to my program";
cout << "This program calculate and print the following things: ";
cout << "The sum of first and last numbers";
cout << "The average of first three numbers";
cout << "The product of last two numbers";
cout << "The largest of all numbers";
cout << "The greatest common divisor(gcd) of last two numbers";
cout << "The factorial of last two numbers";
cout << "and";
cout << "The grade for all numbers (H:85-100, D:75-84, C65-74, P:50-64, F:0-49)";
cout << endl << endl;
cout;
cout << endl << endl;
cout << "Enter first integer: ";
cin >> first_number;
cout << "Enter second integer: ";
cin >> second_number;
cout << "Enter third integer: ";
cin >> third_number;
cout << "Enter fourth integer: ";
cin >> fourth_number;
cout << "Enter last (first 3 digits of your ID-number) integer: ";
cin >> last_number;


// numbers one and last are arguments to the sum function call
cout << "The sum of the first and last numbers " << first_number << ", " << last_number << "is: " << sum << endl;


// numbers 1 to 3 are arguments to the average function call
cout << "The average of the first three numbers " << first_number << ", " << second_number << ", " << third_number <<" is: " << average << endl;


// numbers of last two numbers are arguments to the product function call
cout << "The product of the last two numbers " << fourth_number << ", " << last_number << "is: " << product << endl;


// numbers 1 to last are arguments to the largest function call
cout << "The largest number " << first_number << ", " << second_number << ", " << third_number << ", " << fourth_number << ", " << last_number << "is: " << largest << endl;


// last two numbers are arguments to the gcd function call
cout << "The product of the last two numbers " << fourth_number << ", " << last_number << "is: " << gcd << endl;


// last two numbers are arguments to factorial function call
cout << "The factorial of the last number " << last_number << "is: " << factorial << endl;


// all numbers are arguments to grade function call
cout << "The largest number " << first_number << ", " << second_number << ", " << third_number << ", " << fourth_number << ", " << last_number << "is: " << grade << endl;


return 0; // indicates successful termination

} // end main


---------
// function average of first three numbers definition;

double average (first_number, second_number, third_number)
{

average =(first_number + second_number + third_number + 0.0) / 3; //calculates the average of first three numbers numbers

return average;

} // end function average

----------
// funtion product of last two numbers definition;

// function largest number definition;

int largest (first_number, second_number, third_number, fourth_number, last_number)
{
int large = first_number; // assume first_number is largest

if (second_number > large) // if sec_number is larger, assign sec_number to max
large = second_number;

if (third_number > large) // if third_number is larger, assign third_number to max
large = third_number;

if (fourth_number > large) // if fourth_number is larger, assign fourth_number to max
large = fourth_number;

if (last_number > large) // if last_number is larger, assign last_number to max
large = last_number;

return large; // large is largest value

} // end function largest

/*
-----------
//function to calculate factorial

int factorial(int last_number)
{
if (last_number > 1) {
return n * factorial(n - 1);
}
else {
return 1;
}
}

-------------
//function to calculate grade of all numbers

if (numericGrade <= 100) {
letterGrade = 'H';

} else if (numericGrade >= 84) {
letterGrade = 'D';

} else if (numericGrade <= 74) {
letterGrade = 'C';

} else if (numbericGrade <= 64) {
letterGrade = 'P';

} else if (numericGrade <= 49){
letterGrade = 'F';
}
}

*/



Report Offensive Message For Removal


Response Number 1
Name: geohoffman49431
Date: March 19, 2006 at 10:53:38 Pacific
Reply: (edit)

when you create a function you need to put the parameter types in the function prototype:


instead of :

double average(num1,num2)
{
..
}

you need to do:

double average(double num1, double num2)
{
...
}



Report Offensive Follow Up For Removal

Response Number 2
Name: Fozzie
Date: March 19, 2006 at 10:56:43 Pacific
Reply: (edit)

Your function prototypes are incorrect.

If you type in

double average;

you are creating a variable of type double named average.

Your code would be less confusing if you don't use the same name for both variables and functions.

I suggest renaming your average function. Something like:

double GetAverage(int 1stnum, int 2ndnum...)
{
double average; //declare locally is better
// your
// code
// here
return average;
}
(That would be the functions' DEFINITION)


The PROTOTYPE for that function would be

double GetAverage(int 1stnum, int 2ndnum, int 3rdnum);


Report Offensive Follow Up For Removal

Response Number 3
Name: feierstarter1
Date: March 20, 2006 at 04:39:49 Pacific
Reply: (edit)

Thanks for your help!!!!
im just a beginner, thanks
i tried to change some points.
here the new version

why is the second function not working??

How can i make sure nobody enter a letter or a number smaller than 0 or bigger than 100???
I would like show a errormessage for that case.How can i search for these points or where can i find??


//C++ program that reads a series of integer numbers (between 0 and 100), calculates and prints
//sum of first and last numbers
//average of first three numbers
//product of last two numbers
//largest of all numbers
//greatest common divisor(gcd) of last two numbers
//factorial of last two numbers
//grade for all numbers (H:85-100, D:75-84, C65-74, P:50-64, F:0-49)


//Constant and type definitions are included in header file.

#include <iostream.h> // header file for input/output stream
#include <stdlib.h> // header file for standard library
#include <math.h> // header file for mathmatic

int first_number;
int second_number;
int third_number;
int fourth_number;
int last_number;

// function prototypes

double Getaverage (int first_number, int second_number, int third_number);

int GetLargest (int first_number, int second_number, int third_number, int fourth_number, int last_number);

------
// function main begins program execution
int main()
{

cout << "Welcome to my program" << endl;
cout << "This program calculate and print the following things: " << endl;
cout << "The sum of first and last numbers" << endl;
cout << "The average of first three numbers" << endl;
cout << "The product of last two numbers" <<endl;
cout << "The largest of all numbers" << endl;
cout << "The greatest common divisor(gcd) of last two numbers" << endl;
cout << "The factorial of last two numbers" << endl;
cout << "and" << endl;
cout << "The grade for all numbers (H:85-100, D:75-84, C65-74, P:50-64, F:0-49)" << endl;
cout << endl << endl;
cout << endl;
cout << endl << endl;
cout << "Enter first integer: ";
cin >> first_number;
cout << "Enter second integer: ";
cin >> second_number;
cout << "Enter third integer: ";
cin >> third_number;
cout << "Enter fourth integer: ";
cin >> fourth_number;
cout << "Enter last (first 3 digits of your ID-number) integer: ";
cin >> last_number;
cout << endl;

return 0; // indicates successful termination

} // end main


// function GetAverage of first three numbers definition;

double GetAverage (int first_number, int second_number, int third_number);
{

double average; // declare variable local

average =(first_number + second_number + third_number + 0.0) / 3; //calculates the average of first three numbers

// numbers 1 to 3 are arguments to the average function call
cout << "The average of the first three numbers " << first_number << ", " << second_number << ", " << third_number <<" is: " << average << endl;
//cout << average (first_number, second_number, third_number);

return average;

} // end function average

----------

// function largest number definition;

int GetLargest (int first_number, int second_number, int third_number, int fourth_number, int last_number);
{

int large = first_number; // assume first_number is largest

if (second_number > large) // if sec_number is larger, assign sec_number to max
large = second_number;

if (third_number > large) // if third_number is larger, assign third_number to max
large = third_number;

if (fourth_number > large) // if fourth_number is larger, assign fourth_number to max
large = fourth_number;

if (last_number > large) // if last_number is larger, assign last_number to max
large = last_number;

//numbers 1 to last are arguments to the largest function call
cout << "The largest number " << first_number << ", " << second_number << ", " << third_number << ", " << fourth_number << ", " << last_number << " is: " << large << endl;


return large; // large is largest value

} // end function largest


Report Offensive Follow Up For Removal

Response Number 4
Name: geohoffman49431
Date: March 20, 2006 at 10:32:15 Pacific
Reply: (edit)

you could do something like this:

first number = -1;

while (first_number > 0 || first_number < 100)
{
cout << "Enter first integer: ";
cin >> first_number;
}

If this is for a homework project then I would just assume the professor will enter correct values. So unless he tells you to do some error checking I would not assume you have to.

As for checking to make sure they dont enter a letter this is what I would do - read the input as a string of characters. Format the string and remove all non integer characters, leaving only 0-9 and the '-' characters. Then use the atoi() function to convert it to an integer or write your own function to convert.

A function prototype is used for one reason. When you call a function the compiler expects to see the function declared above the function call like this:


void func()
{
}

int main()
{
func();
return 0;
}


if you want to put func() below the call to func() then you have to add a function prototype like this:

void func();

int main()
{
func();
return 0;
}

void func()
{
}

So when the compiler tries to find the definition of func() above main it finds the prototype "void func();" which tells it that the actual definition is bellow the function call.

When you declare your functions you added ';' at the end making it a prototype.

return_type function_name(parameter_list);
{
function_body
}

so the compiler sees this as a function prototype followed by a block of code that has nothing to do with the function prototype. You might as well have written :


{
function_body
}

return_type function_name(parameter_list);


because the compiler will not associate the block of code with the prototype. I think you had your function declarations right in the first version you posted except for not puting the data types in the parameter list. You probably just copied and pasted the suggested syntax and got the extra ';' .


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Error message C++

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge