Thread: Help for a beginner

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

    Help for a beginner

    Hi everyone, I am currently learning C code and come a cropper.

    I want to write a code where 5 integers are stored in an array and the user has to input any of them to show a message on the screen saying “correct value”

    Could anyone help?

    Cheers

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,282
    Can you declare an array?

    Can you print a prompt?

    Can you input an integer?

    In short, show us how far you can get by yourself.
    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
    Oct 2020
    Posts
    5
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    int IntS[] = {1,2,3,4};
    int IntI[4], i;
    
    
      while (1)
      {
                printf("Enter Integer: \n");
    
    
            for (i=0;i<3;i++)
            {
                scanf("%d", &IntI[i]);
    
    
                if (IntS[i]!= IntI[i])
                    {
                     printf("Wrong Integer\n\n");
                    }
    
    
                else
                    {
                     printf("Correct Integer \n");
                    }
            }
      }
    }
    Last edited by Jordan99; 10-16-2020 at 03:36 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    You didn't show the array declaration.

    But perhaps more importantly, have you compiled your code and tried to run it? You need to take things slowly: write some code, make sure it compiles, and if feasible, run it and enter test input and observe the test output to see if you're on the right track. You shouldn't be writing a bunch of code all at once when you don't yet have the grasp of how it works.
    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

  5. #5
    Registered User
    Join Date
    Oct 2020
    Posts
    5
    Apologies, i have changed it.

    Yes the code runs correctly but it only compares the entered integer with the integer in the array in order.

    i wanted to know how to change the code so it recognises any integer at any time.

    Thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    Okay, great. One of the things you should learn sooner or later is how to format your code to make it readable. One important aspect of this is to use indentation to denote scopes, like what is the body of a function, or a loop, or an if statement. For example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int IntS[] = {1, 2, 3, 4};
        int IntI[4], i;
    
        while (1)
        {
            printf("Enter Integer: \n");
    
            for (i = 0; i < 3; i++)
            {
                scanf("%d", &IntI[i]);
                if (IntS[i] != IntI[i])
                {
                    printf("Wrong Integer\n\n");
                }
                else
                {
                    printf("Correct Integer \n");
                }
            }
        }
    }
    As you can see, the code within the body of the main function is indented by one level. Then when we enter the while loop, it is indented by yet another level, and then goes down by one level when we're out of the loop. This way, it is easier to trace what is nested in what, which makes it easier to reason about the code because you have less to worry about whether say, the for loop is inside the while loop or does it come after the while loop. (Of course, typo errors concerning indentation itself can still lead to related mistakes in interpreting the code, but they also tend to be easier to spot with otherwise good indentation.)

    Next, you have two arrays, but according to what you described, you only need one array, and it should have 5 elements, not 4 elements. Furthermore, when you loop over the array, you should loop over all the elements of the array, not only 3 of them. One way to avoid such mismatch is to compute the number of elements in the array and then use that constant, e.g.,
    Code:
    int numbers[] = {1, 2, 3, 4, 5};
    const size_t num_numbers = sizeof(numbers) / sizeof(numbers[0]);
    // ...
    for (size_t i = 0; i < num_numbers; i++)
    {
        // use numbers[i]
    }
    Consequently, when you want to read an integer from the user, you only need one integer, not an array of them, and you only need to read this integer once, not in a loop (unless you're looping to give the user several tries, but that is something you should only do after you have successfully gotten the code to work with a single try).

    The for loop itself should be search the array of numbers to check if the number the user entered is equal to any one of them. If it is, that's when you announce that the value entered is correct and break from the loop. If none are, that's when you tell the user that value entered is not correct, i.e., you only need to do this once, not on each iteration.
    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

  7. #7
    Registered User
    Join Date
    Oct 2020
    Posts
    5
    Hi.

    Thanks for the help!

    This is the code i have come up with but it still acts the same as before.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int numbers[] = {1,2,3,4,5};
        const size_t num_numbers = sizeof(numbers) / sizeof(numbers[0]);
        int numberin, i;
    
    
        while (1)
        {
           printf ("Enter Integer:");
    
    
           for (size_t i = 0; i < num_numbers; i++)
           {
                scanf("%d", &numberin);
                
                    if (numbers[i] != numberin)
                    {
                        printf("Wrong Integer\n\n");
                    }
                    else
                    {
                        printf("Correct Integer \n");
                    }
            }
        }
    }
    I think it could be to do with this section but not 100%

    Code:
                if (numbers[i] != numberin)
                {
                    printf("Wrong Integer\n\n");
                }
                else
                {
                    printf("Correct Integer \n");
    Last edited by Jordan99; 10-19-2020 at 01:58 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    You can refer to my previous post:

    The for loop itself should be search the array of numbers to check if the number the user entered is equal to any one of them. If it is, that's when you announce that the value entered is correct and break from the loop. If none are, that's when you tell the user that value entered is not correct, i.e., you only need to do this once, not on each iteration.
    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

  9. #9
    Registered User
    Join Date
    Oct 2020
    Posts
    5
    Hey, sorry to be a pain this is my first coding practice.

    This is the code I have come up with.

    it still doesn't work.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        int numbers[] = {1, 2, 3, 4, 5};
        int entnumbers;
        const size_t num_numbers = sizeof(numbers) / sizeof(numbers[0]);
    
    
        printf("Please enter number");
        scanf("%d", entnumbers);
    
    
            for (size_t i = 0; i < num_numbers; i++)
            {
                for (entnumbers = numbers; entnumbers != numbers;)
                printf("Correct Integer");
    
    
            }
    }
    any ideas?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    1. How does it not work? You need to be absolutely clear what your code is supposed to do and why you concluded it does not work.

    2. Trace through your code step by step. This is with reference to #1. That is, at each step you record the current value of the variables and the output thus far so you can truly understand what your code is doing.

    3. Do you understand everything that I wrote in previous posts? I outlined the algorithm in words and I didn't frivolously mention keywords like "check if the number the user entered is equal to any one of them" and "break from the loop", but you didn't use these in your most recent code.
    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

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    3,920
    Hint: "break" is an C keyword.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Question -- From Absolute Beginner's Guide to C
    By Dghelerter in forum C Programming
    Replies: 5
    Last Post: 12-26-2013, 01:30 PM
  2. A little help for a beginner.
    By ixapie in forum C Programming
    Replies: 3
    Last Post: 10-16-2012, 08:29 AM
  3. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  4. beginner ?
    By braincrash in forum C Programming
    Replies: 2
    Last Post: 02-18-2003, 03:33 AM

Tags for this Thread