View Full Version: couple of errors in some programs

C++ Learning Community > C++ Help > couple of errors in some programs


Title: couple of errors in some programs
Description: n00b questions


homeslice - January 22, 2007 02:20 AM (GMT)
Hey I have a couple of errors in both of my programs, I was wondering If anyone could help out (The error description in both of the programs is "Id returned 1 exit status"). Thanks

CODE
#include <iostream>
#include <cmath>

using namespace std;

void input (int& feet, double& inch);
/*prompts the user for a value for feet and a value for inches to go
through calculations into meters and centimeters*/

int meters (int feet, double inch);
/*Will calculate meters from the number of feet and inches, will chop off the
centimeters*/

double centimeters (int feet, double inch);
/*Will calculate centimeters from the number of feet and inches, will chop off
the meters*/

void output (int feet, double inch, int meter, double centimeters);
/*echoes the input and reads out the value of meters and centimeters*/

int main()
{
   int feet, meter, metersout;
   double inch, centimeter, centimetersout;
   char again;
             
   cout << "This program will prompt you for an integer value of feet "
        << "and a double value for inches (preferably to the nearest "
        << "tenth) and will output the equivilent value in meters and "
        << "centimeters\n\n\n";
   do
   {

             
         input(feet, inch);
         
         metersout = meters(feet, inch);
         
         centimetersout = centimeters(feet, inch);
         
         output(feet, inch, metersout, centimetersout);
         
         cout << "Enter \"Y\" if you would like to run the program again";
         
         cin >> again;
   
   }while (again == 'y' || again == 'Y');
   
   return 0;
}

/*prompts the user for a value for feet and a value for inches to go
through calculations into meters and centimeters*/
void input (int& feet, double& inch)
{
       using namespace std;
       
       cout << "Enter a value for feet please\n";
       cin >> feet;
       cout << "Enter a value for inches please\n";
       cin >> inch;
}

/*echoes the input and reads out the value of meters and centimeters*/
void output (int feet, double inch, int meters, double centimeters)
{
        using namespace std;
       
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(1);
       
        cout << "If you have " << feet << " feet and " << inch << "inches, "
             << "Then you have " << meters << " and " << centimeters
             << " centimeters \n\n";
}

/*Will calculate meters from the number of feet and inches, will chop off the
centimeters*/
int meters (int feet, double inch)
{
            int meter;
           
            feet = feet + (inch / 12);
           
            meter= feet / 0.3048;
           
            return meter;
}

/*Will calculate centimeters from the number of feet and inches, will chop off
the meters*/
double centimeters (int feet, double inch, int meters)
{
    double centimeter, meter;
     
   inch = (feet * 12 ) + inch;
   
   centimeter = inch * 2.54;
   //2.54 is the number of centimeters in an inch.
   
   centimeter = centimeter - (meter*100);
   
   return centimeter;
}


and the other program that is VERY similar (converts the opposite direction)

CODE
#include <iostream>
#include <cmath>

using namespace std;

void input2 (int& meter, double& centimeter);
/*prompts the user for a value for meters and a value for centimeters to go
through calculations into meters and centimeters*/

int foot (int meter, double centimeter);
/*Will calculate feet from the number of meters and centimeters, will chop off the
centimeters*/

double inches (int meter, double centimeter);
/*Will calculate inces from the number of meters and centimeters, will chop off
the meters*/

void output2 (int feet, double inch, int meter, double centimeter);
/*echoes the input and reads out the value of feet and inches*/

int main()
{
   int feet, meter, feetout;
   double inch, centimeter, inchout;
   char again;
             
   cout << "This program will prompt you for an integer value of meters "
        << "and a double value for centimeters (preferably to the nearest "
        << "tenth) and will output the equivilent value in feet and "
        << "inches\n\n\n";
   do
   {

             
         input2(meter, centimeter);
         
         feetout = foot(meter, centimeter);
         
         inchout = inches(meter, centimeter);
         
         output2(meter, centimeter, feetout, inchout);
         
         cout << "Enter \"Y\" if you would like to run the program again";
         
         cin >> again;
   
   }while (again == 'y' || again == 'Y');
   
   return 0;
}

/*prompts the user for a value for meters and a value for centimeters to go
through calculations into feet and inches*/
void input2 (int& meter, double& centimeter)
{
       using namespace std;
       
       cout << "Enter a value for meters please\n";
       cin >> meter;
       cout << "Enter a value for centimeters please\n";
       cin >> centimeter;
}

/*echoes the input and reads out the value of feet and inches*/
void output2 (int foot, double inches, int meter, double centimeter)
{
        using namespace std;
       
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(1);
       
        cout << "If you have " << meter << " meters and " << centimeter
             << "inches, Then you have " << foot << " feet and " << inches
             << " inches \n\n";
}

/*Will calculate feet from the number of meters and centimeters, w
ill chop off the inches portion*/
int foot (int meter, double centimeter)
{
            double foot;
           
            meter = meter + (centimeter / 100);
           
            foot= meter * 0.3048;
           
            return meter;
}

/*Will calculate inches from the number of meters and centimeters,
will chop off the feet portion*/
double inches (int meter, double centimeter, int foot)
{
    double inches;
     
   centimeter = (meter * 100) + centimeter;
   
   inches = centimeter / 2.54;
   //2.54 is the number of centimeters in an inch.
   
   inches = inches - (foot*12);
   
   return inches;
}


Thanks for any help.

Shonoby - January 22, 2007 04:11 AM (GMT)
Well, your problem is something that happens but might be of misconception.
Take for example your first program, you had initialized all your functions properly. But what threw you off was when u defined them. What you are misconstruing, is when u define a function. You have to give it the exact parameters if not then it is a different function. The function:
CODE

double centimeters (int feet, double inches);
...
double centimeters (int feet, double inch, double meter )
{
   double centimeter, double meter;
     
   inch = (feet * 12 ) + inch;
   
   centimeter = inch * 2.54;
   
   centimeter = centimeter - (meter*100);
   
   return centimeter;
}

This is might seem correct but actually is not. If u look at the function definition, the parameters are incorrect, u added an extra one which was 'double meter'. That is y the processor gave u a error instead of the compiler. When, the processor give u a error that means that one of your function was not defined among other things.
In your second program the exact same thing happened but in this part:
CODE

double inches (int meter, double centimeter);
...
double inches (int meter, double centimeter, int foot)
{
    double inches;
     
   centimeter = (meter * 100) + centimeter;
   
   inches = centimeter / 2.54;
   //2.54 is the number of centimeters in an inch.
   
   inches = inches - (foot*12);
   
   return inches;
}

That is self-explanatory, but if u dont know u added ' int feet ' in the parameters of the function definition.
There are also several other things that are incorrect, but in better terms, poor programming.
When you defined several functions u gave those functions a operation of 'using namespace std;'.
CODE

void input (int& feet, double& inch)
{
       using namespace std;  
       
       cout << "Enter a value for feet please\n";
       cin >> feet;
       cout << "Enter a value for inches please\n";
       cin >> inch;
}

You might think that, that is necessary but it is not, that type of operation should only be used once at the beginning of your code. You did that in the being but for some reason, you also did it in several function(?).
Another mistake that you did, that is also considered poor programming is. When you created the function prototype you also named the values. Which is not proper, this is what u did:
CODE

void input (int& feet, double& inch);
...
input( feet,inch)

But what you should have done is:
CODE

void input( int &, double& );
...
input( feet, inch )

You should not explicitly name the value in the prototype. Second of all y did u 'include <cmath>' there is not need for it. There are still other, coding problems, but not really problems but more poor programming. You should re-learn C++ or at least take a review on it because you are doing some very uncommon, poor programming. But since u still are learning ( so am i ) you should try to figure out what mistake u have made and fix them yourself.
Then afterwards you should visit this site:
Newbie Guide Here In This Forum

homeslice - January 22, 2007 05:44 AM (GMT)
Yeah I am pretty new to C++ haha. I've been "sort-of kind-of" learning it for the past 4 months in an elective at school. I did fix a majority of those problems between when I posted the problems and when I saw your post, the only thing I don't understand is this thing:

CODE
void input (int& feet, double& inch);
...
input( feet,inch)


to

CODE

void input( int &, double& );
...
input( feet, inch )


I don't listen to my teacher THAT much but I am pretty sure he told us to do this the other way, and he does take points off if you do things any other way so I'll leave it like that. It also says to declare functions the other way in the book we are using: Problem Solving With C++ by Walter Savitch.

edit: w00t i finally got it working, I just have a logic error hidden in here somewhere... (Oh yeah I did it with my (type& variable) way too, ohh yeah what now! =P)

CODE
#include <iostream>

using namespace std;

void input (int& in_feet, double& in_inch);
/*prompts the user for a value for feet and a value for inches to go
through calculations into meters and centimeters*/

int meters (int feet1, double inch1);
/*Will calculate meters from the number of feet and inches, will chop off the
centimeters*/

double centimeters (int feet2, double inch2);
/*Will calculate centimeters from the number of feet and inches, will chop off
the meters*/

void output (int out_feet, double out_inch, int out_meter,
    double out_centimeters);
/*echoes the input and reads out the value of meters and centimeters*/

int main()
{
   int feet, meter, metersout;
   double inch, centimeter, centimetersout;
   char again;
             
   cout << "This program will prompt you for an integer value of feet "
        << "and a double value for inches (preferably to the nearest "
        << "tenth) and will output the equivilent value in meters and "
        << "centimeters\n\n\n";
   do
   {

             
         input(feet, inch);
         
         metersout = meters(feet, inch);
         
         centimetersout = centimeters(feet, inch);
         
         output(feet, inch, metersout, centimetersout);
         
         cout << "Enter \"Y\" if you would like to run the program again";
         
         cin >> again;
   
   }while (again == 'y' || again == 'Y');
   
   return 0;
}

/*prompts the user for a value for feet and a value for inches to go
through calculations into meters and centimeters*/
void input (int& feet, double& inch)
{
       using namespace std;
       
       cout << "Enter a value for feet please\n";
       cin >> feet;
       cout << "Enter a value for inches please\n";
       cin >> inch;
}

/*echoes the input and reads out the value of meters and centimeters*/
void output (int feet, double inch, int meters, double centimeters)
{
        using namespace std;
       
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(1);
       
        cout << "If you have " << feet << " feet and " << inch << "inches, "
             << "Then you have " << meters << " and " << centimeters
             << " centimeters \n\n";
}

/*Will calculate meters from the number of feet and inches, will chop off the
centimeters*/
int meters (int feet1, double inch1)
{
            int meter;
           
            feet1 = feet1 + (inch1 / 12);
           
            meter= feet1 * 0.3048;
           
            return meter;
}

/*Will calculate centimeters from the number of feet and inches, will chop off
the meters*/
double centimeters (int feet2, double inch2)
{
   int mtemp;
   double cmtemp;
     
   inch2 = (feet2 * 12 ) + inch2;
   
   cmtemp = inch2 * 2.54;
   //2.54 is the number of centimeters in an inch.
   
   feet2 = feet2 + (inch2 / 12);
           
   mtemp= feet2 * 0.3048;
   //meters in a foot
   
   cmtemp = cmtemp - (mtemp * 100);
   
   return mtemp;
}
   


(Don't criticize the math/logic I'm in the middle of fixing that part now)

Shonoby - January 22, 2007 08:47 PM (GMT)
Ohh, you see it is that i did not know u were in school, that teaches you C++. But my book ( were i learned from and i have to teach myself ) teaches me to use only the value of the object. It also tells me not to explicitly name the value in the parameter list, if u look at most codes here, you will see that we do not explicitly name the value in the integer. This is how we do it:
CODE

void test( int );
...
int main()
{
...
int y;
test( y );
...
return 0;
}
...
void test( int yAlias )
{
yAlias + 2;
}

Well, in this code u see that i have created a PROTOTYPE of ' void test( int ); ' and not explicitly named it. The reason being, that is is only a model of the function, which is then referenced to( i.e. compiler goes to check if everything is correct ), when the function is invoked ( i.e. when it has been initialized at line 6 ).
This is done as one of the new functions that C++ introduced that C did not ( not completely sure on that ( dont quote me on it ) ).
This is better because this acts as a safe guard, to catch the a problem if it arises in compiling time instead of a run-time error. It also makes function overloadable as well.
But the most important reason that there is a function PROTOTYPE is so that you do not add any extra things to the function ( i.e. like what u did with your program ). Hence the name PROTOTYPE or also a TEMPLATE for the function definition.

Now on to the function initialization. This is were the function is given its parameter, note that the ' int y ' has the same value type ( int ) as the function parameters. Now, the compiler know that the function has been invoked, so it automatically goes to the function prototype, keeps that in mind, then goes to the definition. Checks to see if they match, if it does then makes sure the data inside the definition is correct. If the function prototype does not match the definition, it moves on, to find one that does, and if it does not, then it displays no error. But then the processor checks the system again to see of any of the function PROTOTYPES were not in check and then displays an error message.

Now onto the definition, in this you see that it's parameters are different then when it was invoked. But don't be fooled as u can see, the parameter of the definition say ' yAlias ' ( note it can have different names than what is given at initialization for better readability ). It is just how it sounds it is an alias or nickname of the parameter given at initialization.
Now this my sound good, but it actually passes the value, by ' pass by value '. Which makes a copy of the value and gives it to the function. But this makes all lot of unnecessary space, and memory which is created at run-time. but to avoid this u have to pass it by ' pass by reference ', which u should learn soon in your class i guess.


But anyways, you should just stick with what you feel is right. That the best thing about programming we can make our own style. With that in mind your teacher can teach or even programmer different than us. So, just keep programming.


Ohh, and about your code i have no idea what's wrong. When i run it with my compiler i get not errors. Also about the math is wrong but i am not gonna criticize u about that.

Zaqufant - January 24, 2007 02:14 AM (GMT)
This isn't critisizm, this is just a question. You used the style of commenting you use the "/* */". It just seems to me that it's easier to do the "//" kind of comments. I'm not telling you that you should change, nothing like that. I would just like to know how come you use that kind of comments. Not questioning your style now.




Hosted for free by InvisionFree