-1

Why Does this not work.....?

Intent: Wanted to have it so the user picks a Tables to practise and then the sum works of there choice and the array of digits. I was assuming that it would pick a random digit out of the array?

#include <stdio.h>
int main(){
int Sum [12] = {1,2,3,4,5,6,7,8,9,10,11,12};
int Choice ;
printf ("Pick Your Time's Table\n");
printf ("|2|3|4|5|6|7|8|9|10|11|12|\n");
printf ("===========================\n");
scanf ("%d",Choice);

printf ("%d x %d",Choice,Sum);


return 0;
}
Dead_Ling0
  • 171
  • 1
  • 1
  • 13
  • 2
    Your pun is inexcusable. – Nerrolken Jun 16 '15 at 23:28
  • So to be clear, you want them to enter a number, `choice` from 1 to 12, and that corresponds to practicing their 8s table, or 4s table. You want to print randomly, if they selected 8 for instance, `8 x n` where n is some number from 1 to 12? I think that's what you're asking, but I'm not 100% sure, so I want to clarify. – Dan Jun 16 '15 at 23:31
  • 3
    `scanf` requires a pointer to `Choice`. –  Jun 16 '15 at 23:32
  • This is correct Dan that's what I'm trying to figure out. with little success new to coding ( as in baby steps) and just trying to mess about so thought would make my little brother a time's tables program :) – Dead_Ling0 Jun 16 '15 at 23:36
  • There's a bogus `'` after `Time` – M.M Jun 16 '15 at 23:37
  • Thanks matt !!!! any idea's on the code?;) – Dead_Ling0 Jun 16 '15 at 23:39
  • 2
    @marmaa not really, you'd be better off reading a book or introdoctory tutorial (or even reading other SO threads); at this stage it looks like you're just guessing code and hoping it works. We could hand you an answer to this question but then you'll still be lost as soon as you try to do anything else to the program. – M.M Jun 16 '15 at 23:45
  • @Matt Could you suggest any books I did a really basic course online and sort of understand variables arrays and pointers at a really simplistic level – Dead_Ling0 Jun 16 '15 at 23:47
  • 1
    @marmaa K&R is always a good start especially for arrays and scanf as they start off in that area. [See here](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) for a list – M.M Jun 16 '15 at 23:48
  • @Matt Thanks will check now :) – Dead_Ling0 Jun 16 '15 at 23:52
  • Could Not Find K&R ???? – Dead_Ling0 Jun 16 '15 at 23:58

3 Answers3

2

First, as mentionned by Evert, scanf() require a pointer to Choice.

Secondly, you can't print Sum like that, it's an array.

You should instead use

#include <stdio.h>
int main(){
int Sum [12] = {1,2,3,4,5,6,7,8,9,10,11,12};
int Choice ;
printf ("Pick Your Time's Table\n");
printf ("|2|3|4|5|6|7|8|9|10|11|12|\n");
printf ("===========================\n");
scanf ("%d",&Choice);
int i;
for ( i = 0; i < 12; i++){
  printf("%d X %d = %d\n",Choice,Sum[i],Choice*Sum[i]);
}

return 0;
}

It will loop througth the array and print the result of the table.

If you want it to output a random integer in the array, you must use the rand() function with the modulo operator like so :

int i = rand() % 12; //return numbers from 0 to 11
printf("%d X %d = %d\n",Choice,Sum[i],Choice*Sum[i]);
AdminXVII
  • 1,319
  • 11
  • 22
2

Ok, so you have some issues here.Really this is not a problem you need arrays for. What you do need is to generate random numbers from 1 to 12. Here is a sample program I wrote based on yours that you can modify to do what you want.

#include <stdio.h>
#include <cstdlib>

int main()
{
    //declare variables we will use later
    int choice = 1;
    int randomNumber;

    //print prompt for user
    printf("Pick Your Times Table\n");
    printf("|2|3|4|5|6|7|8|9|10|11|12|\n");
    printf("===========================\n");
    printf("Enter 0 to quit.\n");

    while (choice != 0)
    {
        //read in user input
        scanf_s("%d", &choice);

        //gets a random number from 1 and 12
        randomNumber = rand() % 12 + 1;

        printf("%d x %d = ?\n", choice, randomNumber);
        system("pause"); //pauses so the answer isnt shown right away.
        //print the result of the multiplication problem
        printf("= %d\n", choice * randomNumber);

        //get user input again
        printf("Pick Your Times Table\n");
        printf("|2|3|4|5|6|7|8|9|10|11|12|\n");
        printf("===========================\n");
        printf("Enter 0 to quit.\n");
     }

    return 0;
}

In the beginning it's very difficult to write code from nothing, so I hope having this as an example to modify helps you out. Let me know if you have any more questions about it that the comments in the code don't answer.

Dan
  • 660
  • 6
  • 15
  • Immensely helpful still throwing some errors but it's getting there maybe i should try do a less complicated project to begin with? – Dead_Ling0 Jun 17 '15 at 00:29
  • 1
    I ran the code I posted. It isn't compiling for you? And yeah, sometimes its better to start off with projects that don't have user input. It can be easier. Like try some simple stuff like calculating the volume of a rectangle or a sphere. It's also good to practice printing out patterns of `*`s using loops. I think you can do this project though. Remember, the most important part of programming is planning what you want to do first, not just writing code! It's a hard lesson, but you'll find it's worth it. – Dan Jun 17 '15 at 00:45
  • Hey Dan, I was wondering if i could add you as a friend and have a chat about some coding :)? – Dead_Ling0 Jun 17 '15 at 11:23
  • Yeah absolutely! Happy to help. email to spudguntechie at gmail I don't know how PMs work on SO, if they exist. – Dan Jun 17 '15 at 16:40
  • Cheers buddy just messaged you perhaps I can help you with something though it won't be code related obviously ^^ – Dead_Ling0 Jun 17 '15 at 17:30
0

The scanf() function needs the address of the variable choice, not its contents.

change this statement: scanf("%d", Choice);

to this: scanf("%d", &Choice);

OwlsCIS
  • 61
  • 4