Thread: Need help with errors in C programming

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    5

    Need help with errors in C programming

    Here's what I'm trying to do, and the errors that I'm getting.

    it's a decent amount of code. Would like help

    1) Write a loop that will print all the whole numbers from 1 and 100. - works

    2) Write a loop that will add all the whole numbers from 1 and 100 and print the total. - works

    3) Write a loop that will add all the ODD whole numbers from 7 and 313 and print the total.

    getting 29690 as the answer

    4) Write a loop that will add all the EVEN whole numbers from -2 and -146 and print the total.
    getting 29690 as the answer (the same as the previous one)

    5) Write a loop that will add every 3rd number from 2000 and -60 and print the total.
    getting 695393 as the answer

    6) Write a loop that will prompt the user to enter a number from 1 to 100. The program will continue to loop until a number within that range is entered. After the loop, print out the square root of the number. Be sure to test the loop by entering numbers outside the range. - answer is correct

    7) Write a loop that will prompt the user to enter test scores from 0 to 100. The program will continue to loop until a -1 is entered. Sum all test scores entered that are in the range 0 to 100 (inclusive). After the loop, calculate the average and print out the letter grade. Assume a 10-point grading scale.
    - not returning the correct average or letter grade

    8) If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Write the code that will calculate and print the sum of all the multiples of 3 or 5 below 1000.
    - not returning the correct answer

    9) Write a loop that will prompt the user to enter an uppercase letter. The code should continue to loop until an uppercase letter is entered. After an uppercase letter is entered print out the letter in both uppercase and lowercase. You can't use the built-in tolower function.
    - not returning the correct answer (unsure of how to convert the uppercase to lowercase)

    Hangman. Make a constant in your code with a value from 1 to 100. Prompt the user to guess the number. If the user guesses correctly end the game and display a "Congratulation. You won." message. If the guess is too high print "Too high." If the guess is too low print "Too low." Give the user at most eight guesses. If the user hasn't guessed the number after eight guesses print a "You lose." message.

    - not going through all of the steps and just returning the first step regardless, also not stopping loop after 8 guesses or after guessing correctly

    Code:
    // ------------------------------------------------------------------------------------------
    // Includes
    // ------------------------------------------------------------------------------------------
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    
    
    // ------------------------------------------------------------------------------------------
    // Constants
    // ------------------------------------------------------------------------------------------
    const long lngARRAY_SIZE = 100;
    
    
    // ------------------------------------------------------------------------------------------
    // Prototypes
    // ------------------------------------------------------------------------------------------
    
    
    // ------------------------------------------------------------------------------------------
    // Name: main
    // Abstract: This is where the program starts
    // ------------------------------------------------------------------------------------------
    void main()
    {
    
    
        int intIndex = 0;
        int intTotal = 0;
        int intAverage = 0;
        const int constGradeRunningTotal = 0;
        char chrLetter = ' ';
        int intSquareRoot = 0;
        int intNumberChosen = 45;
        int intNumberEntered = 0;
        int intCount = 0;
    
    
        // --------------------------------------------------------------------------------
        // Problem #1 - Print all the whole numbers from 1 to 100.
        // --------------------------------------------------------------------------------
        printf("Problem #1 - Print all the whole numbers from 1 to 100.\n");
    
    
        for (intIndex = 1; intIndex <= 100; intIndex += 1)
        {
            printf("%d, ", intIndex);
    
    
            //move to a new line every 10 numbers
            if (intIndex % 10 == 0)
            {
                printf("\n");
            }
        }
    
    
        printf("\n");
        printf("\n");
    
    
        // --------------------------------------------------------------------------------
        // Problem #2 - Print Total of the sum of all numbers 1 to 100
        // --------------------------------------------------------------------------------
        printf("Problem #2 - Print the sum of all the whole numbers from 1 to 100.\n");
    
    
        for (intIndex = 1; intIndex <= 100; intIndex += 1)
        {
            intTotal += intIndex;
        }
    
    
        printf("The sum of all whole numbers 1 to 100 is %d, ", intTotal);
    
    
        printf("\n");
        printf("\n");
        // --------------------------------------------------------------------------------
        // Problem #3 - Print the sum of all ODD numbers 7 to 313
        // --------------------------------------------------------------------------------
        printf("Problem #3 - Print all the sum of all odd numbers from 7 to 313.\n");
    
    
        for (intIndex = 7; intIndex <= 313; intIndex += 2)
        {
            if (intIndex % 2 != 0)
            {
                intTotal += intIndex;
            }
        }
    
    
        printf("The sum of all whole odd numbers 7 to 313 is %i, ", intTotal);
    
    
        printf("\n");
        printf("\n");
        // --------------------------------------------------------------------------------
        // Problem #4 - Print the sum of all EVEN whole numbers -2 to -146
        // --------------------------------------------------------------------------------
        printf("Problem #4 - Print the sum of all even whole numbers from -2 to -146.\n");
    
    
        for (intIndex = -2; intIndex <= -146; intIndex += 2)
        {
            if (intIndex % 2 == 0)
            {
                intTotal += intIndex;
            }
        }
    
    
        printf("The sum of all whole even numbers -146 to -2 is %i, ", intTotal);
    
    
        printf("\n");
        printf("\n");
    
    
        // --------------------------------------------------------------------------------
        // Problem #5 - Print the sum of every 3rd number from -60 to 2000
        // --------------------------------------------------------------------------------
        printf("Problem #5 - Print the sum of every 3rd whole number from -60 to 2000.\n");
    
    
        for (intIndex = -60; intIndex <= 2000; intIndex += 3)
        {
            intTotal += intIndex;
        }
    
    
        printf("The sum of sum of every 3rd whole number from -60 to 2000 is %d, ", intTotal);
    
    
        printf("\n");
        printf("\n");
    
    
        // --------------------------------------------------------------------------------
        // Problem #6 - Print the square root of a number within the range of 1 to 100
        // --------------------------------------------------------------------------------
        printf("Problem #6 - Enter a number from 1 to 100:\n");
        scanf_s("%i", &intIndex);
    
    
        //is number between 1 and 100?
        if (intIndex >= 1 & intIndex <= 100)
        {
            intSquareRoot = intIndex * intIndex;
        }
        else
        {
            printf("The number mus be between 1 and 100:\n");
            scanf_s("%i", &intIndex);
        }
    
    
        printf("The square root of %i is %i\n", intIndex, intSquareRoot);
    
    
        printf("\n");
        printf("\n");
    
    
        // --------------------------------------------------------------------------------
        // Problem #7 - Print Average test Score and Letter Grade
        // --------------------------------------------------------------------------------
    
    
        do
        {
            printf("Problem #7 - Enter a test score between 0 and 100 (-1 to end):\n");
            scanf_s("%d", &intIndex);
    
    
            intCount = intCount + 1;
            intTotal = intTotal + intIndex;
            intAverage = intTotal / intCount;
    
    
        } while (intIndex != -1);
    
    
        if (intAverage <= 100 & intAverage >= 90)
        {
            chrLetter = 'A';
        }
        if (intAverage <= 89 & intAverage >= 80)
        {
            chrLetter = 'B';
        }
        if (intAverage <= 79 & intAverage >= 60)
        {
            chrLetter = 'C';
        }
        if (intAverage <= 79 & intAverage >= 70)
        {
            chrLetter = 'D';
        }
        else
        {
            chrLetter = 'F';
        }
    
    
        printf("The average test score was %d and the letter grade is a %c", intAverage, chrLetter);
    
    
        printf("\n");
        printf("\n");
    
    
        // --------------------------------------------------------------------------------
        // Problem #8 - Print the sum of all multiples of 3 or 5 below 1000
        // --------------------------------------------------------------------------------
        printf("Problem #8 - Print the sum of all multiples of 3 or 5 below 1000.\n");
    
    
        for (intIndex = 0; intIndex < 1000; intIndex ++)
        {
            if (intIndex % 3 == 0 || intIndex % 5 == 0)
            {
                intTotal += intIndex;
            }
        }
    
    
        printf("The sum of all multiples of 3 or 5 below 1000 is %i, ", intTotal);
        printf("\n");
        printf("\n");
        // --------------------------------------------------------------------------------
        // Problem #9 - Print the uppercase and lowercase letter of an UpperCase letter entered.
        // --------------------------------------------------------------------------------
        printf("Problem #9 - Enter an uppercase letter: \n");
        scanf_s("%c", &chrLetter);
    
    
        if (chrLetter < 'a' || chrLetter > 'z')
        {
            printf("Letter must be uppercase: ");
        }
        else if (chrLetter < 'A' || chrLetter > 'Z')
        {
            chrLetter = chrLetter;
        }
    
    
        printf("Here is the letter in uppercase %C and lowercase %c", &chrLetter, &chrLetter);
        printf("\n");
        printf("\n");
        //is the letter uppercase?
        //no, enter another letter
        //yes, print both the uppercase and lowercase letter
    
    
        // --------------------------------------------------------------------------------
        // Problem #10 - Have user guess number between 1 and 100
        // --------------------------------------------------------------------------------
    
    
        do          
        {
            printf("Problem #10 - Enter the chosen number between 1 and 100 (you have 8 guesses)\n");
            scanf_s("%d", &intNumberEntered);
    
    
            if (intNumberEntered < intNumberChosen)
            {
                printf("Too low!\n");
            }
            else if (intNumberEntered > intNumberChosen)
            {
                printf("Too high!\n");
            }
    
    
            //update count
            intCount + 1;  
    
    
            if (intCount == 8)
            {
                printf("You Lose!!!\n");
            }
        } while (intNumberEntered != intNumberChosen);
            printf("Congratulations, you won!!\n");
    
    
        system("pause");
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    At a glance, one issue is that you are not resetting intTotal to 0.

    Since all these tasks are separate, it would be best if you wrote a self-contained function for each of them.

    By the way, does the int in intIndex, intTotal, etc stand for "integer"? That's pointless since the type is already int. Just name them index, total, etc.
    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
    Jun 2020
    Posts
    6
    A couple things to start with. You don't reset the value of intTotal between problems, so problem 3 ends up printing the result from problem 2 plus all odd numbers from 7 to 313. Either reset intTotal to zero between problems, or make each problem a function so you don't need to worry about ensuring clean starting values for each of your variables.

    Also, in problem 4, your bounds need to be reordered:

    for (intIndex = -2; intIndex <= -146; intIndex += 2)

    intIndex is set to -2, but for the loop to continue it must be less than or equal to -146. -2 is greater than -146, so the loop never runs. This is why the sum is unchanged from problem 3 above. You should switch the start and end indices.

    See if you can spot similar issues in the remaining problems.

  4. #4
    Registered User
    Join Date
    Jun 2020
    Posts
    5
    Quote Originally Posted by mwh View Post
    A couple things to start with. You don't reset the value of intTotal between problems, so problem 3 ends up printing the result from problem 2 plus all odd numbers from 7 to 313. Either reset intTotal to zero between problems, or make each problem a function so you don't need to worry about ensuring clean starting values for each of your variables.

    Also, in problem 4, your bounds need to be reordered:

    for (intIndex = -2; intIndex <= -146; intIndex += 2)

    intIndex is set to -2, but for the loop to continue it must be less than or equal to -146. -2 is greater than -146, so the loop never runs. This is why the sum is unchanged from problem 3 above. You should switch the start and end indices.

    See if you can spot similar issues in the remaining problems.
    thank you! both replies were super helpful!
    that helped to fix most of my issues

    im still having difficulty with this one:
    Code:
        // --------------------------------------------------------------------------------    // Problem #7 - Print Average test Score and Letter Grade
        // --------------------------------------------------------------------------------
    
    
        intTotal = 0;
        intCount = 0;
    
    
        do
        {
            printf("Problem #7 - Enter a test score between 0 and 100 (-1 to end):\n");
            scanf_s("%d", &intIndex);
    
    
            intCount =  intCount + 1;
            intTotal = intTotal + intIndex;
    
    
        } while (intIndex != -1);
    
    
        intAverage = intTotal / intCount;
    
    
        if (intAverage <= 100 & intAverage >= 90)
        {
            chrLetter = 'A';
        }
        if (intAverage <= 89 & intAverage >= 80)
        {
            chrLetter = 'B';
        }
        if (intAverage <= 79 & intAverage >= 60)
        {
            chrLetter = 'C';
        }
        if (intAverage <= 79 & intAverage >= 70)
        {
            chrLetter = 'D';
        }
        else
        {
            chrLetter = 'F';
        }
    
    
        printf("The average test score was %d and the letter grade is a %c", intAverage, chrLetter);
    
    
        printf("\n");
        printf("\n");
    as it is not giving me the correct average

    and this one:
    Code:
        // --------------------------------------------------------------------------------    // Problem #9 - Print the uppercase and lowercase letter of an UpperCase letter entered.
        // --------------------------------------------------------------------------------
    
    
        do
        {
            printf("Problem #9 - Enter an uppercase letter: \n");
            scanf_s("%c", &chrLetter);
    
    
            if (chrLetter < 'A' & chrLetter > 'Z')
            {
                printf("Invalid Input");
            }
            else
            {
                chrLetter = chrLetter;
            }
    
    
        } while (chrLetter >= 'A' || chrLetter <= 'Z');
    
    
        printf("Here is the letter in uppercase %c and lowercase %c /n", chrLetter, tolower(chrLetter) );
    
    
        printf("\n");
        printf("\n");
    as it is recognizing uppercase letters as such nor is it recognizing numerals as invalid inputs or lowercase letters as invalid inputs

    and the last one
    Code:
        // --------------------------------------------------------------------------------    // Problem #10 - Have user guess number between 1 and 100
        // --------------------------------------------------------------------------------
        intCount = 0;
    
    
        do          
        {
            printf("Problem #10 - Enter the chosen number between 1 and 100 (you have 8 guesses)\n");
            scanf_s("%d", &intNumberEntered);
    
    
            if (intNumberEntered < intNumberChosen)
            {
                printf("Too low!\n");
            }
            else if (intNumberEntered > intNumberChosen)
            {
                printf("Too high!\n");
            }
    
    
            //update count
            intCount + 1;  
    
    
            if (intCount == 8)
            {
                printf("You lose! \n");
            }
    
    
        } while (intNumberEntered != intNumberChosen);
    
    
        if (intNumberEntered = intNumberChosen)
        {
            printf("Congratulations, you won!!\n");
        }
    it doesn't end the loop after 8 tries; it only ends the loop once the number is guessed correctly

  5. #5
    Registered User
    Join Date
    Jun 2020
    Posts
    6
    Problem 7 has a couple of issues.

    What happens to intTotal and intCount when you enter -1 to exit?

    & is the bitwise AND operator, which is not what you want. You should be using &&, the logical AND operator in this case.

    Finally, let's say intAverage is 90. The condition for 'A' is true, then 'B', 'C', and 'D' are false. Where does the program go when the condition for 'D' is false?

  6. #6
    Registered User
    Join Date
    Jun 2020
    Posts
    6
    Problem 9:

    The input is invalid if it is less than 'A' OR greater than 'Z', and you want your loop to keep going until you have valid input. Look carefully at the conditions involving chrLetter. Also, instead of duplicating the logic, I would consider either setting a flag when valid input is found or using break to exit the loop.

    You're also going to run into an issue when entering invalid inputs in which you are prompted twice. This is because scanf reads the \n when you hit enter as the next character. You can use getchar() instead, or prepend a space to your format string so that scanf consumes the whitespace before the character each time. Also watch for /n vs \n in your printf's.

    Problem 10:

    Try printing out intCount each loop or using a debugger to see what is going on. Once intCount gets to 8, what do you want to happen? Currently, there is nothing to cause the loop to terminate. Also, while it doesn't matter in this case, when waiting for a value to pass a threshold, I prefer to check if the value is greater than/equal to the threshold, as opposed to just equal to. That way, if intCount somehow got to 9, your loop would still exit as intended.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Typical errors in C/C++ programming
    By el_programmer in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2016, 03:36 PM
  2. Annoying programming errors
    By djm2 in forum C Programming
    Replies: 1
    Last Post: 04-14-2013, 08:17 AM
  3. C Programming Assignment Help!! (Cant Figure Out My Errors) :(
    By irishfeeney92 in forum C Programming
    Replies: 19
    Last Post: 04-28-2011, 02:14 PM
  4. programming errors
    By C_Enthuaist in forum C Programming
    Replies: 15
    Last Post: 07-02-2010, 01:32 PM
  5. Errors, with vectors, windows programming... etc
    By bobbelPoP in forum C++ Programming
    Replies: 10
    Last Post: 07-27-2008, 03:07 AM

Tags for this Thread