Thread: Calculation Help

  1. #16
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Various Errors

    Dear Reddtee

    I would like to make some observations about your code. So far I have not studied it in detail so I am not proposing that these observations will solve the problem. However they are errors that need to be addressed.

    In your printf() statements you start of with varied amounts of ?%s?. These indicate that after the double quotes you are going to print a string. These identifies of strings are not present. Indeed you have no reason for identifiers to be present. If you remove them, you should also remove the commas that are separating the lines of text to be printed. If you do not
    remove the commas only the last line prints. If my memory serves me: commas are a Visual Basic way of linking strings of text that you code over several lines. Your code would then be more usual. I cannot say with certainty that they are affecting the code.

    I compiled your code and got three warnings. In three of your case statements you have entered.
    getchar();
    ch = getchar();
    if (ch == 'n' || ch == 'N')
    break;
    The warnings say that the first of these lines has not effect. I remarked out (put /* in front of and */ at end of, to make the code unread) these lines and the three warnings stopped. I cannot say with certainty that this will solve the problem.

    I ran the programme and tried the q option to quit. Having pressed q and then return it did not quit. I then pressed a key not listed on screen and the programme quit.

    It might be worth pursuing this a little first. Many coders prefer to get the structure (moving between screens and functions) right first as this ensures that they are dealing correctly with data that may remain in the buffer. There are many input options and your compiler will tell you about each one and give you information about how they work. Some are : getc, getch,
    getchar, getche, scanf(%c, identifier). I think that some retain characters in the buffer and some do not.

    I concluded this partially because I ran the programme again and chose the P option. After entering the three numerical inputs the screen displayed the monthly payments and the choice of continuing. It did not wait for an answer and went through the P option again. I was stuck in a perpetual loop.

    Was this your experience as well ?

    Assuming it was then consider using ? fflush(stdin); ? in a strategic place to eliminate this problem.

    I realise that I have not attempted to answer the maths question. My first observation here is that you have declared the identifiers ?principal?, ?rate? and ?duration? as doubles. However when you are scanning them in you have used the format specifier for a float (f). It is likely that they never go into the identifiers. Please try ?lf?. Also when printing out.

    Good luck with whatever route you chose.

    Stephanos

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > In your printf() statements you start of with varied amounts of
    > ?%s?. These indicate that after the double quotes you are
    > going to print a string. These identifies of strings are not
    > present.

    Actually, they ARE present, I'll explain in a bit...

    > Indeed you have no reason for identifiers to be present. If you
    > remove them, you should also remove the commas that are
    > separating the lines of text to be printed.

    Correct, you could do this too.

    > If you do not remove the commas only the last line prints. If my
    > memory serves me: commas are a Visual Basic way of linking
    > strings of text that you code over several lines. Your code
    > would then be more usual. I cannot say with certainty that they
    > are affecting the code.

    Incorrect, and possibly correct. Two issues here. I'll explain now:

    printf( "%s %s", string1, string2 );

    This you'll agree is valid. It prints two strings, seperated by a space. Since that is correct, then this is also correct:

    printf("%s %s", "hello", "there" );

    The same rules apply. However, you can do this just the same:

    printf( "Hello" " " "world" );

    The reason this will work, though it isn't really "valid", is because C will concatenate most consecutive strings that aren't seperated by a comma, thus, turning it into:

    printf( "Hello world" );

    A comma in C, in a function call, denotes argument seperation.

    printf( "fun stuff %d", 4 );

    Thus, printf has 2 arguments, each seperated by a comma.

    > Assuming it was then consider using ? fflush(stdin); ? in a
    > strategic place to eliminate this problem.

    No. NEVER consider using this. This only works by "design" on Microsoft compilers from what I've seen. The behaviour is NOT ANSI standard. If you want to clear the input stream, you can, from the MSDN, rewind() an input stream. I think. It's either that or fseek() to the start. I can't recall which. I am uncertin if either of these methods is an ANSI standard, I haven't bothered looking, because I never need to worry about flushing my input streams.

    > My first observation here is that you have declared the
    > identifiers ?principal?, ?rate? and ?duration? as doubles.
    > However when you are scanning them in you have used the
    > format specifier for a float (f). It is likely that they never go into
    > the identifiers. Please try ?lf?. Also when printing out.

    This is not correct. Consider this following link. Let me quote:
    f The double argument is rounded and converted to
    decimal notation in the style [-]ddd.ddd, where the
    number of digits after the decimal-point character
    is equal to the precision specification. If the
    precision is missing, it is taken as 6; if the pre-
    cision is explicitly zero, no decimal-point charac-
    ter appears. If a decimal point appears, at least
    one digit appears before it.
    In other words, the "f" actually means "double".

    Hope that helps.

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

  3. #18
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    Dear Quzah & Reddtee

    I am very grateful for the extra information and corrections and hopefully there was not much confusion before this extra knowledge became shared. Thank you

    I did a step through of the code using Borland C++ v3 and when the format specifier was F the identifier did not take in the values entered. After I changed it to LF it did.

    Perhaps this is Borland specific.

    I am so impressed with the level of knowledge and politness that I will be posting a big problem of mine.

    Thanks again


    Stephanos

  4. #19
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    Quzah and Stephanos

    Thank you both for your posts and the great information. Quzah, the DEBUG info you sent me will be invaluable - I'm sure I will use it on a regular basis.

    I originally had a member reply and tell me to take out the 'l' from my %lf, so I did. I will try and put the 'l' back in to troubleshoot.

    Thanks again.

  5. #20
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    Inserting the 'l' in my scanf statements %lf did work. I can't explain it but the calculations worked fine after that. Maybe it is my compiler, I am currently using Borland 3.0.

    By using the DEBUG that was suggested, I was able to get all calculations working.

    Thank you to everyone who posted and educated me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Date calculation program
    By putty88 in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 07:24 AM
  2. Problem with modulo calculation
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 01-12-2009, 03:37 PM
  3. C# calculation problem
    By Diablo02 in forum C# Programming
    Replies: 13
    Last Post: 10-25-2007, 08:42 PM
  4. Help with calculation and loop
    By Betty in forum C Programming
    Replies: 7
    Last Post: 04-10-2006, 05:37 AM
  5. help with pay calculation program
    By shugstone in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2003, 09:38 AM