Need Advice 2

This is a discussion on Need Advice 2 within the C++ Programming forums, part of the General Programming Boards category; I have rewritten the code. Now I am getting the following error message:error C2239: unexpected token '{' following declaration of ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    12

    Need Advice 2

    I have rewritten the code. Now I am getting the following error message:error C2239: unexpected token '{' following declaration of 'main'. Please help.

    I am writing a function that will take time as three integer arguments (for hours,minutes, and seconds), and returns the number of seconds since the last time the clock "struck 12". I will then use this function to calculate the amount of time in seconds between two times, both of which are within on 12-hour cycle of the clock. Any advice would be appreciated.

    Code:
    #include <iostream>
    #define  SecondsPerHour    3600
    #define  SecondsPerMinute  60
    #define  MinutesPerHour    60
    
    int      main 
    {
    
            int     hh,                   // hours    
                    mm,                   // minutes 
                    ss,                   // seconds 
    
                    begin_sec,  end_sec,  // times converted   
    		sec_elapsed;          // difference between times  
    						
            char    colon1, colon2;       
    	
    	// read start time 
            printf( "Please enter a start time (HH:MM:SS) --> " );
            scanf ( "%d%c%d%c%d", &hh, &colon1, &mm, &colon2, &ss);
    	
    	// convert start time to seconds by calling on function 
    	begin_sec = Time_in_Seconds(hh,mm,ss); 
    	
    	// read end time 		
    	printf( "Please enter an end time  (HH:MM:SS) --> " );
            scanf ( "%d%c%d%c%d", &hh, &colon1, &mm, &colon2, &ss);
    	
    	// calls on function that will convert entered time to seconds 
    		
    	end_sec = Time_in_Seconds(hh, mm, ss);
    	
    	// calculate time difference 
    	sec_elapsed = end_sec - begin_sec;
    		
    	
    return (0);
    }
    Please use [code][/code]Tags

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You forgot the parameter list:
    Code:
    int main ( void )
    {
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Change this line:

    int main

    to this:

    int main()

    And, change this line:

    return (0);

    to this:

    return 0;

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    148
    Originally posted by 7stud

    And, change this line:

    return (0);

    to this:

    return 0;
    What's wrong with the braces? Nothing.
    It's even legal to write something like this
    Code:
    int (((i)));

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Thanks for the insight. I'll try to remember that the next time I need to declare an int variable.
    Last edited by 7stud; 06-07-2003 at 04:28 AM.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    12
    I have changed all the suggestions you have made. And I am still getting the same error messages.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    12
    #include <iostream>
    #define SecondsPerHour 3600
    #define SecondsPerMinute 60
    #define MinutesPerHour 60

    int main ()
    {


    int hh, // hours
    mm, // minutes
    ss, // seconds

    begin_sec, end_sec, // times converted
    sec_elapsed; // difference between times




    std::cout<< "Please enter a start time (HH:MM:SS): ";//start time
    std::cin >>"%d%d%d", &hh, &mm,&ss;


    std::cout<< "Please enter an end time(HH:MM:SS): ";// read end time
    std::cin>>"%d%d%d", &hh,&mm,&ss;

    sec_elapsed = end_sec - begin_sec;// calculate time difference


    // This function calculates the number of seconds when given a time in Hours:Minutes:Seconds */

    int Time_in_Seconds( int hh, int mm, int ss);

    int time_in_sec;


    time_in_sec = (hh * SecondsPerHour) +
    (mm * SecondsPerMinute)+(ss);// converts standard time into seconds

    begin_sec = Time_in_Seconds(hh,mm,ss); // convert start time to seconds
    end_sec = Time_in_Seconds(hh, mm, ss); // calls on function that will convert entered time to seconds

    return ( time_in_sec );// sends converted time to main to be processed
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Just to elaborate on what Salem said:

    You can't define a function inside another function. Your structure should look like this:

    Code:
    int my_function(int number)
    {
        ....
        ....
    }
    
    
    int main()
    {
       .....
       int result = my_function(3);
       .....
       return 0;
    }
    Also, please thoroughly read this:

    << !! Posting Code? Read this First !! >>
    Last edited by 7stud; 06-07-2003 at 01:30 PM.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    12

    Question

    I hate to be such a pain. I have tried the suggestions made and my program compiles but when it is executed it will have: Please enter a start time <HH:MM:SS> -->(I enter the start time)
    then the same line is repeated but asks for the end time-->Press any key to continue. All this is on the same line for the end time. When I press a key the program exits. Any suggestions.

    #include <iostream>
    #define SecondsPerHour 3600
    #define SecondsPerMinute 60
    #define MinutesPerHour 60

    int Time_in_Seconds( int hh, int mm, int ss)
    {
    int time_in_sec;


    time_in_sec = (hh * SecondsPerHour) +
    (mm * SecondsPerMinute)+(ss);// converts standard time into seconds

    return ( time_in_sec );// sends converted time to main to be processed


    }
    int main ()
    {
    int hh, // hours
    mm, // minutes
    ss, // seconds

    begin_sec, end_sec, // times converted
    sec_elapsed; // difference between times


    printf( "Please enter a start time (HH:MM:SS) --> " );// read start time
    scanf ( "%d%d%d", &hh, &mm, &ss);


    begin_sec = Time_in_Seconds(hh,mm,ss); // converts the start time to seconds by calling on function


    printf( "Please enter an end time (HH:MM:SS) --> " );// read end time
    scanf ( "%d%d%d", &hh, &mm, &ss);


    end_sec = Time_in_Seconds(hh, mm, ss);// calls on function that will convert entered time to seconds


    sec_elapsed = end_sec - begin_sec; // calculates the time difference


    return 0;
    }

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The '\n' character will print a new line.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    12
    Where do I need to put this at

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Just put it in the printf wherever you want the newline to start. i.e.

    printf("The next line will be on a new line.\nThis is on a new line.");
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    then the same line is repeated but asks for the end time-->Press any key to continue. All this is on the same line for the end time. When I press a key the program exits.
    What do you expect your program to do? You ask the user for two pieces of information, you perform some calculations, and then your program ends(that's what the "Press any key to continue" message means). If you want your program to do something else, then you have to program it to do something else.

    I'm guessing you want to ouput some information based on what the user entered, but you don't have any lines of code that output any information to the screen after you read in the input from the user. Computers are dumb, they can't read your mind and decide what to output for you.
    Last edited by 7stud; 06-07-2003 at 10:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on C Programming with MSVC++ 2008
    By IT_Guy in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2009, 03:23 AM
  2. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 02:46 PM
  3. Resume advice
    By Zughiaq in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-15-2005, 02:16 PM
  4. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 06:38 PM
  5. Partitioning advice?
    By mart_man00 in forum Tech Board
    Replies: 6
    Last Post: 01-10-2003, 09:53 PM

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