Thread: If statement Homework Help

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    5

    If statement Homework Help

    Hello guys, I have a question about an If statement homework assignment. It is simple code designed to calculate the sum of 10 integers from stdin, and to printf the sum is over 100 or the sum is negative. Why is it when I input a letter instead of a number, the code can successfully run but with a crazy sum? Code located below, thanks for any help you can provide.

    Code:
    
    
    1. #include <stdio.h>
    2. int main ()
    3. {
    4. /* variable definition: */
    5. int value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,sum;
    6. /* Initialize sum */
    7. sum = 0;
    8. printf("Enter an Integer for value1\n");
    9. scanf("%d", &value1);
    10. printf("Enter an Integer for value2\n");
    11. scanf("%d", &value2);
    12. printf("Enter an Integer for value3\n");
    13. scanf("%d", &value3);
    14. printf("Enter an Integer for value4\n");
    15. scanf("%d", &value4);
    16. printf("Enter an Integer for value5\n");
    17. scanf("%d", &value5);
    18. printf("Enter an Integer for value6\n");
    19. scanf("%d", &value6);
    20. printf("Enter an Integer for value7\n");
    21. scanf("%d", &value7);
    22. printf("Enter an Integer for value8\n");
    23. scanf("%d", &value8);
    24. printf("Enter an Integer for value9\n");
    25. scanf("%d", &value9);
    26. printf("Enter an Integer for value10\n");
    27. scanf("%d", &value10);
    28. sum = value1 + value2 + value3 + value4 + value5 + value6 + value7 + value8 + value9 + value10;
    29. printf("Sum is %d\n " , sum );
    30. if (sum >100)
    31. printf("Sum is over 100\n");
    32. if (sum <0)
    33. printf("Sum is a negative value");
    34. return 0}
    35. }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Because you need to do something like
    Code:
    if ( scanf("%d",&var) == 1 ) {
      // success
    } else {
      // garbage input, so burn the rest of the current input line.
      int ch;
      while ( (ch=getchar()) != EOF && ch != '\n' );
    }
    If you don't, each successive scanf fails in the same way, and leaves each of your input variables in it's initial garbage state.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    5
    I am not looking to change the code, all that matters is that it runs successfully for the purpose of this assignment. I am simply wondering how the code successfully runs even if letters are input instead of numbers. Thanks for the assistance

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    And I'm telling you why it doesn't run "successfully".

    When presented with say "hello", each of those scanf calls is going to return IMMEDIATELY, do nothing with the input (which will remain untouched at "hello"), and almost certainly leave the valuex integers untouched with whatever garbage value is in there.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    5
    Ok, the output I am receiving whenever letters are input are a numerical value. For example, A+1+1+1+1 etc = 19009298183. Is this just a random number that will change every time or is there some default value given to letter characters? I have to do test cases based upon the code I choose, and the instructor prefers one of the cases to consist of unlikely inputs. I need to know if there is a way to predict what the end value will be in a given equation when letters are input when the code expects numbers.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    No, there is no way for your program in it's current state to ever print a sensible value when presented with bad input.
    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.

  7. #7
    Registered User
    Join Date
    Nov 2016
    Posts
    5
    Alrighty, thank you. For the record I know this program is coded horribly, but I am forced to do one programming class for my degree path so this is it. I have no interest or knowledge in coding. Thanks again

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I guess we're both wasting our time then - please tell your tutor.
    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.

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Just out of question - what degree would force a student to take a course in computer science that was mandatory to the success of it - but never touch the subject again?
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  2. Am I doing this homework problem correctly? (Switch Statement)
    By Cameron Taylor in forum C Programming
    Replies: 5
    Last Post: 06-17-2013, 08:04 PM
  3. Homework help! changing from a switch statement to if-else
    By Rachel Hyde in forum C Programming
    Replies: 3
    Last Post: 03-07-2013, 05:31 PM
  4. need homework help asap with if statement
    By automagp68 in forum C Programming
    Replies: 26
    Last Post: 01-27-2012, 02:19 PM
  5. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM

Tags for this Thread