Struct Help

This is a discussion on Struct Help within the C++ Programming forums, part of the General Programming Boards category; Ive been given a task to create a program that will create a clock that can be incremented at users ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    25

    Struct Help

    Ive been given a task to create a program that will create a clock that can be incremented at users request. Ive always had a few problems when functions are involvec. So far in my program i just want to do a function that will add 1 to the value for seconds and i want the changes to occur in main too. Can someone show me where im going wrong?

    #include <iostream.h>
    #include <cstring.h>
    struct ClockType
    {
    int iHours;
    int iMinutes;
    int iSeconds;
    }; //End Struct
    ////////////////////////////////////////////////////////////////////////////////
    //Function Prototypes
    AddTime();
    ////////////////////////////////////////////////////////////////////////////////
    void main(void)
    {
    int iYes;

    ClockType sClock;

    cout << "Enter the time" <<endl;
    cout << "What is the hour? ";
    cin >> sClock.iHours;
    cout << "Enter the minute " <<endl;
    cin >> sClock.iMinutes;
    cout << "Enter the seconds " <<endl;
    cin >> sClock.iSeconds;

    cout <<"The time is " << sClock.iHours <<":"
    << sClock.iMinutes << ":" << sClock.iSeconds <<endl;
    cout <<"Press 1 to change time";
    cin >> iYes;

    if (iYes == 1);
    {

    AddTime(int ClockType sClock);
    {
    sClock.iSeconds = sClock.iSeconds +1;

    return sClock;
    }
    else
    {
    cout << "Time not changed"
    }
    }


    }
    }//End main

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Struct Help

    I'ver marked the parts that are wrong, explanations after the code:
    Code:
    #include <iostream.h>
    #include <cstring.h>
    struct ClockType
    {
       int iHours;
       int iMinutes;
       int iSeconds;
    }; //End Struct
    
    ////////////////////////////////////////////////////////////////////////////////
    //Function Prototypes
    AddTime();
    ////////////////////////////////////////////////////////////////////////////////
    
    void main(void)   (1)
    {
       int iYes;
       ClockType sClock;
    
       cout << "Enter the time" <<endl;
       cout << "What is the hour? ";
       cin >> sClock.iHours;
       cout << "Enter the minute " <<endl;
       cin >> sClock.iMinutes;
       cout << "Enter the seconds " <<endl;
       cin >> sClock.iSeconds;
    
       cout <<"The time is " << sClock.iHours <<":"
            << sClock.iMinutes << ":" << sClock.iSeconds <<endl;
       cout <<"Press 1 to change time";
       cin >> iYes;
    
       if (iYes == 1);   (2)
       {
    
          AddTime(int ClockType sClock);   (3)
          {                                (4)
             sClock.iSeconds = sClock.iSeconds +1;
             return sClock;
          }
       else
       {
          cout << "Time not changed"
       }
    }
    }
    }  (5)
    1) void main() should be int main(). It is possible to do void, but the correct way is to use int.

    2) There should be no ; at the end.

    3) int ClockType as a datatype? Should probably just be ClockType if this is the definition and/or it should just be sClock if this is the function call.

    4) Define this function outside main instead. Also, you've missed one }. And there is no need to return sClock (You haven't even specified a return value).

    5) You have two misplaced } here. If you use a hiearchical display these are easier to spot.

    PS: In the future, please use CODE tags .
    Last edited by Magos; 04-18-2002 at 02:58 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    Cheers for the help but ive still got a problem im being told i have an expression syntax which appears to be from the function call This is the code i have now. Im getting very stressed by this.


    #include <iostream.h>
    #include <cstring.h>
    struct ClockType
    {
    int iHours;
    int iMinutes;
    int iSeconds;
    }; //End Struct

    ////////////////////////////////////////////////////////////////////////////////
    //Function Prototypes
    AddTime();
    ////////////////////////////////////////////////////////////////////////////////

    int main()
    {
    int iYes;

    ClockType sClock;

    cout << "Enter the time" <<endl;
    cout << "What is the hour? ";
    cin >> sClock.iHours;
    cout << "Enter the minute " <<endl;
    cin >> sClock.iMinutes;
    cout << "Enter the seconds " <<endl;
    cin >> sClock.iSeconds;

    cout <<"The time is " << sClock.iHours <<":"
    << sClock.iMinutes << ":" << sClock.iSeconds <<endl;
    cout <<"Press 1 to change time";
    cin >> iYes;

    if (iYes == 1)
    {

    AddTime(int sClock);//Function Call
    {
    sClock.iSeconds = sClock.iSeconds +1;

    return sClock.iSeconds;
    }
    }



    }//End main

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if (iYes == 1); 
    { 
        AddTime(int ClockType sClock); 
        {
            sClock.iSeconds = sClock.iSeconds +1; 
            return sClock; 
        }
    Look very carefully at this code. Are you sure this is what you want? Let me tell you what it does, then you can see if you want it:


    1) If iYes is equal to 1, then the block executes.
    2) The function 'AddTime( )' is called.
    2a) This function call is incorrect. It should be called:

    AddTime( timeToAddIETheTimeToPassToThisFunction );

    2b) 'int' and 'ClockType' are here for what reason exactly?
    C) The following block of code attempts to execute:

    {
    sClock.iSeconds = sClock.iSeconds +1;
    return sClock;
    }

    See that? That in itself is a valid block of code. Think very carefully as to what happens here:

    1) You execute a function call to AddTime( );
    2) This block of code, which is NOT the body of 'AddTime' executes.

    Got that? I think what you want is to define the function 'AddTime' BEFORE you ever make the 'main' function. Right after your structure definition.

    As such, it should probably look something like this:
    Code:
    ClockType AddTime( ClockType sClock )
    {
        sClock.iSecond++;
        return sClock;
    }
    That is what you want. Then instead of that call to 'AddTime' below, replace everything I quoted above with the following:
    Code:
    if( iYes == 1 )
    {
        AddTime( sClock );
    }
    That is what you want. At least that appears to be what you want.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    I think the thing im not getting is how a function is defined, such as what goes in the brackets and why. And when it is called what goes in the brackets and why. These problems are only magnified when im trying to use structs with it. I think i changed the code as you suggested this is what ive got now.

    if( iYes == 1 );
    {
    AddTime( sClock );
    }

    ////////////////////////////////////////
    ClockType AddTime( ClockType sClock );
    {
    sClock.iSeconds++;
    return sClock;
    }
    ////////////////////////////////////////



    }//End main

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    More problems with this code include the fact that this is C++ posted on the C board.

    > if( iYes == 1 );
    Mmm, that ; at the end of the line does something, but not what you want it to.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    As an asside i know i have put the function within the main there, ive moved it outside now but im still getting my extra parameter in call to addtime() error

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    Im quite sure i was doing C. What part of it is c++?

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    All of it

    #include <iostream.h>
    #include <cstring.h>
    These are C++ headers

    //Function Prototypes
    These are C++ comments

    cout << "What is the hour? ";
    cin >> sClock.iHours;
    These are C++ stream I/O


    > my extra parameter in call to addtime() error
    Because you prototype it with no parameters, but try and use it with one parameter, and also define it with one parameter

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Ryan_P
    Im quite sure i was doing C. What part of it is c++?
    cout << "Oh, I donno. Perhaps this line?" << endl;

    printf("Well what about me?\n");

    cout << "No, you're C." << endl;

    printf("Ah. Ok. So the same goes for those headers?\n");

    cout << "Yeah, they're C++ headers.";

    printf("Gotcha.\n");

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    Bah, well thats not entirely my fault, my lecturers told me i was basically doing C, in future then ill post in C++ board.

  12. #12
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Ryan_P
    I think the thing im not getting is how a function is defined, such as what goes in the brackets and why. And when it is called what goes in the brackets and why. These problems are only magnified when im trying to use structs with it. I think i changed the code as you suggested this is what ive got now.

    if( iYes == 1 );
    {
    AddTime( sClock );
    }

    ////////////////////////////////////////
    ClockType AddTime( ClockType sClock );
    {
    sClock.iSeconds++;
    return sClock;
    }
    ////////////////////////////////////////



    }//End main
    You should place it outside main, like this:
    Code:
    ClockType AddTime( ClockType sClock )
    {
        sClock.iSeconds++;
        return sClock;
    }
    
    int main()
    {
    
       ...
    
       if( iYes == 1 );
       {
          sClock=AddTime( sClock );  //Remember to return the result
       }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  3. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 10:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM

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