Thread: Program Shuts Down

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    7

    Program Shuts Down

    Hi All,

    I am writting a Currency Converter for a class and for some reason when I add my first value and hit enter the program fails... Any Thoughts? Thank You in advance.

    Code:
    #include <stdio.h>
    
    main()
    
    {
    
    int iOperand1 = 0;
    int iOperand2 = 0;
    int iResult = 0;
    
    
    /* Currency Rates*/
    printf("\n-=Currency Conversions=-\n"); 
    printf("\nSouth Korean Won = 922.403\n"); 
    printf("\nKuwait Dinar = 0.281700\n"); 
    printf("\nItalian Lire = 1418.51\n"); 
    printf("\nJamaica Dollar = 68.7264\n"); 
    printf("\nAustralian Dollar = 1.18906\n"); 
    
    /* Currency Converter*/
    printf("\nEnter US Dollar Amount");
    scanf("\n&#37;d", iOperand1);
    printf("\nEnter Currency to Exchange Rate");
    scanf("\n%.2d", &iOperand2);
    
    iResult = iOperand1 * iOperand2;
    
    printf("The Exchange Rate is %.2d\n", iResult);
    
    }
    Last edited by LaGood; 08-11-2007 at 06:14 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    Code:
    scanf("\n%d", &iOperand1);

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    Didn't see that. Thanks!

    Now when I hit enter after the first value the program just shuts down?

    Do I need to add a getch(); at the end of each scanf?

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    No, don't want to make the user type an extra enter every time they want your program to stop.

    Better to run the program from a command-prompt. I suspect you're running from an IDE of sorts, and the command window is closing before you can see the output. Open a terminal.

    EDIT: Ah, I didn't read the question very well. Still might apply though. Anyways here's one way to read user input.
    Code:
    char buffer[BUFSIZ];
    double money; /* Use double for floating-point values like money */
    fgets(buffer, BUFSIZ, stdin); /* safe way to read input from a user */
    fscanf(buffer, "&#37;f", &money); /* this can be modified for different inputs */
    Last edited by MacNilly; 08-11-2007 at 06:42 PM.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    I see what is happening, it does not like (.) decimals in the values... for example 1.00 or 5.22 Anyway to rectify that?
    Last edited by LaGood; 08-11-2007 at 06:43 PM.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    I got it.. cannot use integers with decimals... changed my intergers to floats.

    Thanks for all the help.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    > Do I need to add a getch(); at the end of each scanf?
    No, and nor do you need all those "\n" in there either. Whitespace (including newlines) is automatically skipped.

    Which compiler are you using?
    > scanf("\n&#37;d", iOperand1);
    Code:
    $ gcc -Wall foo.c
    foo.c:5: warning: return type defaults to `int'
    foo.c: In function `main':
    foo.c:22: warning: format argument is not a pointer (arg 2)
    foo.c:24: warning: unknown conversion type character `.' in format
    foo.c:24: warning: too many arguments for format
    foo.c:30: warning: control reaches end of non-void function
    Gcc can catch a lot of minor mistakes which often trip up newbies.
    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.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    I am using Dev-C++

  9. #9
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by LaGood View Post
    I see what is happening, it does not like (.) decimals in the values... for example 1.00 or 5.22 Anyway to rectify that?
    Google: "Manual printf"
    Google:"Manual scanf"

    I'm too lazy to search myself then post the links that pop up to you. If you really care, you'd do it yourself.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    > I am using Dev-C++
    Which is a port of the gcc compiler, so you can use that flag I suggested.

    Locate the project or compiler settings and add that flag, then recompile your program. Pay close attention to all the extra information which is printed.
    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.

  11. #11
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    No, his problem is he hasn't read the format specifiers for scanf (printf) correctly. Compiler errors will only confuse him further.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    True, but in a choice between specific diagnostics at compile time vs. random crash at run-time I know which I'd prefer.
    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.

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The confusion is there anyway. At least the compiler will explain to him when he makes these mistakes so he doesn't have to spend countless hours looking over code that has hidden bugs. To argue against raising the level of warnings/errors that the compiler will spit out in this case is ridiculous.

  14. #14
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    No, he can eliminate cryptic compiler errors by simply fixing a few function calls which use the wrong format, and he can do that by searching google. If he has any further "random crashes" or likewise bugs, I care not to correct those.

    Of course I do not argue against raising of levels of warnings and the like. I use:

    Code:
    GCC -Wall -pedantic -std=c99
    for my code, and I recommend the same for u rest, and I make sure I get NO feedback upon compile.
    Last edited by MacNilly; 08-16-2007 at 11:45 PM.

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You think this is cryptic?

    Code:
    syntaxprintf.c: In function `main':
    syntaxprintf.c:7: warning: double format, different type arg (arg 2)
    Code:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int x = 5;
    	
    	printf("&#37;f", x);
    	
    	return 0;
    }
    I disagree. It tells you exactly where the error occurred, that it's a printf() format issue, and which argument it can't swallow.

    Someone that can't figure out what the compiler is telling them with this error probably can't figure it out with google.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM