Program works under C++ but not under C

This is a discussion on Program works under C++ but not under C within the C Programming forums, part of the General Programming Boards category; This program is able to compile and run succesfully under C++, but not C. The program gives a AccessViolation error ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Program works under C++ but not under C

    This program is able to compile and run succesfully under C++, but not C. The program gives a AccessViolation error message on the first line using NULL. Is there some type of headfile I'm missing? I thought iostream.h allowed me to use null.
    I'm using Borland C++ builder ver 5 and 6.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <process.h>
    #include <string.h>
    #include <stdlib.h>
    #include <iostream.h>
    
    int main()
    {
       char  starting_time[10], ending_time[10];
       int   starting_hour = 0, ending_hour = 0, starting_minute = 0,
             ending_minute = 0, hour, minute;
    
       printf("Enter the starting time: ");
       scanf("%c", &starting_time);
       printf("Enter the ending time: ");
       scanf("%c", &ending_time);
    
       starting_hour = atoi( strtok( starting_time, ":" ) );
       starting_minute = atoi( strtok( NULL, ":" ) );
       ending_hour = atoi( strtok( ending_time, ":" ) );
       ending_minute = atoi( strtok( NULL, ":" ) );
    
       hour = ending_hour - starting_hour;
       minute = ending_minute - starting_minute;
    
       if ( hour < 0 )
       {
          hour = hour + 24;
       }
       if ( minute < 0 )
       {
          minute = minute + 60;
          hour = hour - 1;
       }
    
       printf("Elapsed time is: %d : %d", hour, minute);
    
       getch();
       return 0;
    }

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You're including headers that only exist in C++. Use the standard C functions (and incidentally, according to the current standard, that code doesn't actually compile under C++ - use namespaces)

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Code:
       printf("Enter the starting time: ");
       scanf("%c", &starting_time);
       printf("Enter the ending time: ");
       scanf("%c", &ending_time);
    What is the type of &starting_time ? What type does scanf() expect with the %c modifier?

    edit: whoopsie cancel that. But let that be a lesson: saying
    &array confuses people! Say &array[0] or just array if thats what you mean.
    Last edited by moi; 10-12-2004 at 10:13 PM.
    hello, internet!

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    PHP Code:
    #include <iostream.h> 



    Not required..

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    "Array name is pointers in C"

    First class of array in your school.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Iv'e removed the iostream.h but I have no idea what your'e trying to say with your second post. Being a little bit more specific would help.
    And I forgot to mention, when it compiled under C++, it did have a few other header files like pragma hdrstop and such.

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    #pragma hdrstop is not a header file. A header file is a source code file that is added to your code by the preprocessor, when it encounters the directive "include" (preprocessor directives all start with '#'. #pragma hdrstop tells the preprocessor that you're done including header files. It's not really necessary, and not really common. I've never seen it outside of my Borland user manual. Not even in lists of preprocessor directives online. Another common directive you'll see is define, which is used for macros (#define MAX 10 would go through your source code before compilation and replace every occurence of MAX with 10, literally.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    Your code is broken - the fact that it works in one inplementation and not another is just dumb luck.

    > scanf("%c", &starting_time);
    This does NOT append a \0 to whatever you type in
    Besides, it reads only 1 char, not a string

    > starting_hour = atoi( strtok( starting_time, ":" ) );
    This assumes many times that the input does have a \0

    I really think you should be replacing those scanf calls with fgets calls.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Roaring_Tiger
    "Array name is pointers in C"

    First class of array in your school.
    No.

    Code:
    #include <stdio.h>
     
     int main() {
       char test1[20];
       char *test2;
     
       printf("pointer:%i, array:%i\n", sizeof(test1), sizeof(test2));
       return 0;
     }
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program works on Windows XP and 2000, not on 98 or ME
    By MidnightlyCoder in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 03:36 PM
  2. My program works but the numbers are not correct.
    By romeoz in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2003, 11:02 PM
  3. Replies: 3
    Last Post: 01-14-2003, 10:34 PM
  4. Enistine program
    By Mac_the vamp in forum C Programming
    Replies: 2
    Last Post: 11-11-2002, 10:56 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

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