-
December 12th, 2012, 11:13 PM
#1
Redeclaration and definition problems
I'm having severe problems with my code. We are asked to take in the Vehicle name, tank size, and MPG. We are give the distance between the cities and are supposed to output the # of tankfuls required to make the ship. The errors I am getting are:
variable or field 'doOutput' declared vod
'int doOutput' redeclared as different kind of symbol
previous declaration of 'void doOutput
declaration of 'int doOutput'
conflicts with previous declaration 'void doOutput'
Then it tells me that Vehicle, MPG, and fillups are not declared on line 90 (were the function is supposed to be opened)
What is wrong? It seems to revolve around the redelcaration that is happening of doOutput. But why is it doing this?
void menu();
void getinput();
void doCalculations(char Vehicle[3][20], double tank[3], double mpg[3]);
void doOutput(char Vehicle[3][20],double mpg[3], double fillups[3]);
void cls();
#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome, please choose one of the following options" << '\n';
cout << '\n';
menu();
}
void menu()
{
int choice;
cout << "1. Enter the data!" << '\n';
cout << "2. Quit the Program!" << '\n';
cout << "Enter the choice you would like below: ";
cin >> choice;
switch (choice)
{
case 1:
getinput();
break;
case 2:
return;
break;
default:
cout << "DO IT AGAIN! I SAID PICK FROM THE CHOICES!!!" << '\n';
menu();
break;
}
}
void cls()
{
system("cls");
}
void getinput()
{
int counter;
char Vehicle[3][20];
double tank[3];
double mpg[3];
for (counter = 0; counter <3; counter++)
{
cout << "What is the Vehicle's Name: ";
cin >> Vehicle[counter];
cout << "What is the Vehicle's gas tank size: ";
cin >> tank[counter];
cout << "What is the Vehicle's Miles Per Gallon: ";
cin >> mpg[counter];
}
doCalculations(Vehicle,tank, mpg);
}
void doCalculations(char Vehicle[3][20], double tank[3], double mpg[3])
{
int counter;
double distance = 527.8;
double milespertank[3];
double fillups[3];
for (counter =0; counter <3; counter ++)
{
milespertank[counter] = (tank[counter]*mpg[counter]);
fillups[counter] = ((distance)/(milespertank)[counter]);
}
doOutput(Vehicle, fillups, mpg);
}
void doOutput(Vehicle[3][20],mpg[3], fillups[3])
{
char Vehicle[3][20];
double tank[3];
double mpg[3];
double distance = 527.8;
double milespertank[3];
double fillups[3];
int counter
cout << "The program calculates that: "
for (counter = 0; counter <3; counter++)
{
cout << "Vehicle Name: " << Vehicle[counter];
cout << "Vehicle MPG: " << mpg[counter];
cout << "# of Fillups: " << fillups[counter];
cout << '\n';
}
menu();
}
-
December 13th, 2012, 02:02 PM
#2
Re: Redeclaration and definition problems
Do you see a difference between the function prototype and the function itself ?
This is what the compiler is complaining about...
Code:
void doOutput(char Vehicle[3][20],double mpg[3], double fillups[3]);
void doOutput(Vehicle[3][20],mpg[3], fillups[3])
Jim
ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII
"The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.
"Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.
-
December 14th, 2012, 03:53 AM
#3
Re: Redeclaration and definition problems
Can we name the local variables of a function with the same names of parameters
we are passing to it ? How the program will work then ?
-
December 15th, 2012, 08:28 AM
#4
Re: Redeclaration and definition problems
Yes that's possible. The local names are just as the name say local.
Code:
#include <iostream>
int Something(int a)
{
return a;
}
int main()
{
int a = 1;
std::cout << "Something is " << Something(a) << std::endl;
return 0;
}
If this is what you questioned...
-
December 15th, 2012, 08:55 AM
#5
Re: Redeclaration and definition problems
Originally Posted by new_2012
Can we name the local variables of a function with the same names of parameters
we are passing to it ? How the program will work then ?
Please note, the variable name has a scope where it remains effective. If variable is declared as function parameter or local function variable, the scope is going to be limited by function body. Out of this scope the variable of the same name means absolutely different region of memory.
Back to the S_M_A's sample:
Code:
#include <iostream>
int Something(int a) // (2)
{
return a;
}
int main()
{
int a = 1; // (1)
std::cout << "Something is " << Something(a) << std::endl; // (3)
return 0;
}
Variable a (1) is a memory absolutely different from a (2), though the value from a (1) gets copied to a (2) when call occurs in (3).
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|