Stumped student

I am working on a project for an intro to C class. Part of the project involves prompting for 6 numbers. These numbers are to be read into an array. Here is what I came up with:


#include


void main()

{

int picks[6];


printf("Please enter 6 different numbers:
");



scanf("%d%d%d%d%d%d
", picks);


}

My thinking was that this would take the 6 entered numbers and read them into the array "picks". When I test this code, I get an illegal operation warning so obviously I'm doing something incorrectly. Can someone point out to me what I am doing wrong? Thanks in advance for any suggestions.


Comments

  • Hi Broas!


    #include

    main()

    {

    int arr[6],i;

    printf("Enter 6 integers
    ");

    for(i=0;i<6;i++)<br>
    {

    printf("%d : ",i+1);

    scanf("%d",&arr[i]);

    }

    for(i=0;i<6;i++)<br>
    printf("%d
    ",arr[i]);

    }


    Good Luck!


  • : I am working on a project for an intro to C class. Part of the project involves prompting for 6 numbers. These numbers are to be read into an array. Here is what I came up with:


    : #include


    : void main()

    : {

    : int picks[6];


    : printf("Please enter 6 different numbers:
    ");

    :

    : scanf("%d%d%d%d%d%d
    ", picks);


    : }

    : My thinking was that this would take the 6 entered numbers and read them into the array "picks". When I test this code, I get an illegal operation warning so obviously I'm doing something incorrectly. Can someone point out to me what I am doing wrong? Thanks in advance for any suggestions.


    Remember one thing: Whenever your dealing with a series of items (i.e. numbers) you will most likely need to use a looping structure of some sort.

    Here is what I would do in your situation.


    #include iostream.h


    const int numValues = 6;


    void main() {


    int picks[numValues];


    cout << 'Please enter ' << numValues << ' different numbers:' << '
    ';<br>
    for (int i = 0; i < numValues; i++)

    cin >> picks[i];


    }


    I'm used to C++ syntax, however, I think you can see want I mean by using a looping structure to do the work.


    Also, notice that "numValues" has been declared as a constant.

    This allows you to associate a name to the amount of numbers you want to input. (Easier to read/understand program code)

    In addition, it enables you to change the number of input values later on without drastically modifying your code. All you need to do is change the value of "numValues" and your done.

    I recommend adopting this programming style in futur work.


    You can see that with the help of a for loop, your program will keep inputing numbers until it reaches the maximum amount of values you had specified in the beginning. (whether it be 6 or 60)

    The counter 'i' is used to test the condition and to insert values into your array in their proper position.


    I hope that this information will help you along in your programming course.

    It only gets better... :)

    Have fun.





  • : scanf("%d%d%d%d%d%d
    ", picks);


    scanf requires, for each of the scancodes in the input string, a unique address in the parameter list for each entry. Examine the following code:


    int x;

    double f;


    scanf("%d%lf",&x,&f);


    This, of course, reads in an integer and a double.


    In your example, you've only provided one parameter. When the program goes to read data into the second field, it looks on the processor stack for the address of some variable to stuff the user input into. What this means is that, not finding a real address, it is pulling some random bits out of computer memory for the address of where to put the user data, and then putting the user input at that random address.


    scanf requires individual addresses as parameters.


    It is interesting to note that by giving the array name without an index like you did, an address *was* generated for the first spot in the array (the start of the array), so probably the very first number that you entered into the console got properly placed. :)




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories