Thread: Why is my code acting this way? (Very simple)

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    44

    Why is my code acting this way? (Very simple)

    For (Score <= 100) It will count the passing grades correctly. But it will also count failing grades as passing, why is it doing that when in the if else statement I put (Score<70) count = count +0; ?Why is my code acting this way? (Very simple)-notcorectcountingpassing-jpgWhy is my code acting this way? (Very simple)-notworkingfailinggrades-jpg
    When I change the first if statement to (score <=100 && score >= 70) Now all the values that I enter in only change the counter to 1?
    Why is my code acting this way? (Very simple)-workingpassinggrades-jpg

    Help please!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please post your code in [code][/code] bbcode tags. What is your test input, expected output, and actual output?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    44
    Quote Originally Posted by laserlight View Post
    Please post your code in [code][/code] bbcode tags. What is your test input, expected output, and actual output?
    Hey laserlight!
    The question is why is this part of my code not working correctly " (score <= 100 && score >=70 ) "
    Im trying to enter in values 88, 90 , and 65. It counts 1 passing score not 2.
    Why is my code acting this way? (Very simple)-notcorectcountingpassing-jpg
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Ask user test scores, calc how many pass (70%+), and percentage of passing scores/not passing scores
    70 - 100 passing, 0-69 failing, any other values invalid, -1 to quit*/
    main() {
        int score, passing, failing, count = 0;
        double percentPass;
    
    
        printf("Enter in the test scores (-1 to quit)");
        scanf("%i", &score);
    
    
        while (score != -1){
            printf("Enter in the test scores (-1 to quit");
            scanf("%i", &score);
    
    
            if (score <= 100 && score >=70 ) {
                count = count +1;
            }
            else {
                if (score < 70) {
                    count = count + 0;
                    
                }
                else {
                    printf("You entered an invalid test score\n");
    
    
                }
                
    
    
    
    
    
    
    
    
            }
    
    
    
    
        }
        
        printf("There were %i passing scores\n", count);


    Last edited by Rednally; 02-25-2016 at 01:41 AM.

  4. #4
    Registered User
    Join Date
    Feb 2016
    Location
    Noida
    Posts
    5
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Ask user test scores, calc how many pass (70%+), and percentage of passing scores/not passing scores
    70 - 100 passing, 0-69 failing, any other values invalid, -1 to quit*/
    main() {
        int score, passing, failing = 0, count = 0;
        double percentPass;
    
    
     /*   printf("Enter in the test scores (-1 to quit)");
        scanf("%d", &score); */
    
        while (score != -1){
            printf("Enter in the test scores (-1 to quit)");
            scanf("%d", &score);
    
            if (score <= 100 && score >=70 ) {
                count++ ;
            }
            else {
                if (score < 70 && score > 1) {
                    failing++ ;
                }else if (score == -1){
                        printf("\n****************************************************\n");
                }
                else {
                    printf("You entered an invalid test score\n");
                }
            }
        }
    
        printf("There were %d passing scores and %d failing scores \n", count , failing);
        printf("****************************************************\n \n");
    }

    This will work fine
    Last edited by Suraj_Tiwari; 02-25-2016 at 03:24 AM.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Suraj_Tiwari View Post
    This will work fine
    Maybe it will, but now you've given this person the answer, and nothing has been learned. This forum does not simply provide answers. We answer specific questions, with the intent of leading people toward a solution. Giving someone the answer, without making them figure it out, teaches nothing. This is especially important for professional developers. We may someday have to work with people who have just been given the answers, and they will need their hand held for their entire career as a result.

    In short, don't do it again.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Echo Elkvis.

    Quote Originally Posted by Suraj_Tiwari View Post
    This will work fine
    Apparently we have different opinions on what "will work fine" means.

    • "score" is checked before it is given a valid value (the initial input lines are commented out, so they do not count) - this is undefined behavior.
    • Lack of handling invalid input - try "accidentally" entering a letter instead and see what happens.
    • According to the comments, 0 is a valid score, but your code treats it as invalid.
    • "main()" needs an explicit return type, and should not have an empty parameter list.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    @Rednally:

    The question is why is this part of my code not working correctly " (score <= 100 && score >=70 ) "
    Im trying to enter in values 88, 90 , and 65. It counts 1 passing score not 2.
    Trace through your code, and the problem should become apparent:

    Line 11: You read a number from the user
    Line 14: You enter the loop
    Line 16: You read another number from the user (without ever using the previous value!)

    This is why a "do-while" loop may be better for this kind of logic - then you wouldn't need to pre-assign a value to the input variable before the loop.

  8. #8
    Registered User
    Join Date
    Feb 2016
    Location
    Noida
    Posts
    5
    Sorry, But i am new. I will never do this again.

  9. #9
    Registered User
    Join Date
    Feb 2016
    Posts
    44
    Thanks @Elkvis this was actually a HW question for last week that I got a 0 on because I couldnt figure it out, now Im just trying to teach myself how to do it properly.
    Quote Originally Posted by Matticus View Post
    @Rednally:



    Trace through your code, and the problem should become apparent:

    Line 11: You read a number from the user
    Line 14: You enter the loop
    Line 16: You read another number from the user (without ever using the previous value!)

    This is why a "do-while" loop may be better for this kind of logic - then you wouldn't need to pre-assign a value to the input variable before the loop.
    @matticus

    I have not learned "do-while" loops yet, Im just so confused as to why (score <= 100 && score >=70 ) isnt working properly!! Do I need to have my else if statement match in the same format? Am I not allowed to use the variable score two times in a if expression?

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The "if()" is working fine. The problem, as I said, is that you're overwriting the first value of "score" (read in before the loop) when you enter the loop, so that value is never processed.

    Try:

    1. Initializing "score" to some value (like 0)
    2. Remove the print/scan lines before the loop

    Then run your code again.

  11. #11
    Registered User
    Join Date
    Feb 2016
    Posts
    44
    Quote Originally Posted by Matticus View Post
    The "if()" is working fine. The problem, as I said, is that you're overwriting the first value of "score" (read in before the loop) when you enter the loop, so that value is never processed.

    Try:

    1. Initializing "score" to some value (like 0)
    2. Remove the print/scan lines before the loop

    Then run your code again.
    In all the examples of sentinal loops that I have learned so far there is ALWAYS the "priming" set of printf/scanf, how come I wouldnt need the first printf/scanf?

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Rednally View Post
    In all the examples of sentinal loops that I have learned so far there is ALWAYS the "priming" set of printf/scanf, how come I wouldnt need the first printf/scanf?
    Because the first thing that happens in your loop is you read a new value, overwriting the previous value (meaning the first value you entered is lost, and never checked).

    That is why I recommended you initialize ("prime", as you say) the value of "score" when eliminating that first printf/scanf.

    This is why I think a "do-while" loop is better, since you can enter the loop, prompt, read, check, and process - all before the loop condition is checked. And a "do-while" is not that much different from a "while".
    Last edited by Matticus; 02-25-2016 at 12:25 PM.

  13. #13
    Registered User
    Join Date
    Feb 2016
    Posts
    44
    My professor gave me this. Thoughts?

    Code:
    while(score !=  -1)
    {
        if(score<0 || score >100)
    
        printf("It is an invalid score");
    
           else
    
            {
    
                countValid = countValid + 1;
    
                if (score< 70)
    
                {
    
                    printf("Failing grade!");
    
                }
                else
    
                {
    
                    countPass = countPass +1;
    
                }
    
            }
    }
    percentPass = 100 * countPass/countValid;printf("Passing grades are %i percent\n\n", percentPass);

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The scanf call appears to be missing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    As laserlight says, that code is not reading user input anywhere. Even if it was reading input inside the loop, it would still require redundant code, as there would be lines to prompt and read both before and within the loop.

    But if you're okay with this, then try integrating the provided code into a "main()" function and test it yourself, to see if it works as you expect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-21-2014, 02:51 PM
  2. code acting a bit strange
    By Labmouse in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 12:59 PM
  3. mmsystem.h acting up
    By Frantic- in forum Windows Programming
    Replies: 2
    Last Post: 06-17-2005, 08:38 PM
  4. getch() acting up...
    By Govtcheez in forum C Programming
    Replies: 9
    Last Post: 08-18-2001, 12:56 PM