First Prototype gone bad...

This is a discussion on First Prototype gone bad... within the C++ Programming forums, part of the General Programming Boards category; Hey guys, so ive jsut started teaching my self C++ and so far ive been doing ok. Today i worked ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Central PA
    Posts
    1

    First Prototype gone bad...

    Hey guys, so ive jsut started teaching my self C++ and so far ive been doing ok. Today i worked on coding my own original prototype based of the example used. Code Blocker kicked back a lot of errors and i cant figure out why or what i am doing wrong. please help if you guys can.

    thanks guys. glad to be a part of the community. and hopefully this code is nice and clean enough for you guys. let me know if i should post differently next time.
    Josh

    Code:
    #include <iostream>
    
    using namespace std;
    int age ( int x, int y );
    
    int main()
    {
        int x;
        int y;
        cout<<"Please input the month you were born (number form): ";
        cin>> int x;
        cin.ignore();
        cout<<"Please input the year of your birth: ";
        cin>> int y;
        cin.ignore();
        cout<<"your age is:"<< age ( x, 2011 - y ) <<"\n";
        cin.get();
    
    }
    int age ( int x, int y );
    {
        return x, 2011 - y
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Sometimes clearer compiler messages really help.

    Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing. All rights reserved.
    MODE:strict errors C++ C++0x_extensions

    "ComeauTest.c", line 11: error: type name is not allowed
    cin>> int x;
    ^

    "ComeauTest.c", line 11: error: expected a ";" (perhaps on the previous statement)
    cin>> int x;
    ^

    "ComeauTest.c", line 14: error: type name is not allowed
    cin>> int y;
    ^

    "ComeauTest.c", line 14: error: expected a ";" (perhaps on the previous statement)
    cin>> int y;
    ^

    "ComeauTest.c", line 16: warning: variable "y" is used before its value is set
    cout<<"your age is:"<< age ( x, 2011 - y ) <<"\n";
    ^

    "ComeauTest.c", line 16: warning: variable "x" is used before its value is set
    cout<<"your age is:"<< age ( x, 2011 - y ) <<"\n";
    ^

    "ComeauTest.c", line 21: error: expected a declaration
    Wild guess: Should this be in a function block?
    {
    ^

    5 errors detected in the compilation of "ComeauTest.c".
    In general, it seems you're really confused about how to call a function. When you do call a function, you pass in values for the function to use, which it calls x and y.

    Since the function does 2011 - y, you should not need to do it when you call. Also, what is x really for? It's not doing much. The comma operator is just a sequence point (like semicolon), it doesn't have any mathmatical significance.

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    I'd say your confusion goes beyond not knowing how to call a function.

    Take a closer look at your examples, start small, and work your way up slowly. I'm talking adding 3-4 lines at a time, and compiling and making sure it works the way you expect before moving on.

    Delete all the code you've written. See if you can read in a variable and print it back out, and work your way up from there.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    I struggled with that (and apparently still do after revisiting!) but your problems seem to be:

    Code:
    cout<<"your age is:"<< age ( x, 2011 - y ) <<"\n";
    This should be written as
    Code:
    cout<<"your age is:"<< age ( x, y ) <<"\n";
    You're trying to define it twice, use the blueprints/prototype until you define it at the end.
    -----
    Code:
    int age ( int x, int y );
    {
        return x, 2011 - y
    }
    The int age (int x, int y ) should not have a ';' after it in this part (not the first time it comes up).
    -----
    Code:
    {
        return x, 2011 - y
    }
    There should be a ';' after the return part. Also, I don't know if it's necessary, but brackets certainly look nicer:
    Code:
    {
        return (x, 2011 - y);
    }
    -----
    For the sake of tidiness, you can enter the different variables on one line:
    Code:
        cout<<"Please input the month you were born (number form): ";
        cin>> int x;
        cin.ignore();
        cout<<"Please input the year of your birth: ";
        cin>> int y;
    Should be:
    Code:
        cout<<"Please input your DOB (mm yyyy): ";
        cin>> int x >> int y;
    -----
    I think that addresses everything, but I had a play around and came up with this (I'm sure it can be improved on, but it runs without those dreaded errors!)

    Code:
    #include <iostream>
    
    using namespace std;
    int age ( int x, int y, int z );
    
    int main()
    {
        int x;
        int y;
        int z;
        cout<<"Please input your date of birth (dd mm yyyy): ";
        cin>> x >> y >> z;
        cin.ignore();
        cout<<"your age is:"<< age ( x, y, z ) <<"\n";
        cin.get();
    }
    int age ( int x, int y, int z )
    {
        return ((30-x)/360)+((6 - y)/12) + (2011 - z);
    }
    I suppose I'd have to link it up to a database to get the exact date right with the varying days of the months, or use a function, or create one of my own (but with my skills at the moment, we're talking hours there...). Also, I don't know the variable to give a decimal instead of an integer, so it won't be as precise as I'd have liked. Hope this helps!

    Maybe someone can help me with my do...while loop question now? Stuck on tutorial number 5 (basics): switch cases
    Last edited by rwebb2305; 06-12-2011 at 07:49 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by rwebb2305
    Also, I don't know if it's necessary, but brackets certainly look nicer:
    I prefer to leave out the parentheses, but there's a bigger problem than just style: the comma used here is the comma operator. x is evaluated, then 2011 - y is evaluated, and then the result of 2011 - y is returned. If the aim is to compute the age in months, then obviously the correct computation must be substituted.

    Quote Originally Posted by rwebb2305
    For the sake of tidiness, you can enter the different variables on one line:
    Yes, but that's the wrong syntax. x and y must be declared first, then one can write:
    Code:
    cin >> x >> y;
    That said, x and y are poorly chosen names here. month and year would be more descriptive.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    @rwebb2305
    Don't forget when doing integer math any fractional part is dropped. So in your function ((30-x)/360) will evaluate to zero. As will (6 - y)/12). Which would leave (2011 - z) as the only part of the equation that produces an output.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prototype Help please !
    By tminh90 in forum C Programming
    Replies: 2
    Last Post: 04-01-2010, 08:28 AM
  2. prototype
    By chico1st in forum C Programming
    Replies: 3
    Last Post: 05-26-2008, 06:48 AM
  3. determinate() prototype
    By curlious in forum C++ Programming
    Replies: 0
    Last Post: 02-13-2005, 12:12 PM
  4. Prototype
    By Kelvin in forum C Programming
    Replies: 2
    Last Post: 07-12-2002, 07:06 AM
  5. What does this prototype mean?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 05-14-2002, 10:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21