0
votes

I'm trying to write a code which adds a value - say X - to a user entered int, and continues to allow the user to add further int values to 'X+all previously entered int values' until a value of 21 or greater is reached - where the code exits the loop. I thought it would be a simple case of value=value+X but I'm going wrong somewhere...

void additionFunction()
{
     int j=0;
     int tot;

     //tot=cat.firstCard+cat.secondCard; is the actual tot value but for simplicity;

     tot=5;

     for(j=0; j+tot<22; j=j+tot)
     {
         printf("Enter next card\n");
         scanf("%d",&j);
     }
}

I know this is a very simple question but I'm stuck as to what else to try. It was originally implemented in a if,do,switch loop in the shortened form;

if(cat.firstCard!=11 && cat.secondCard!=11)
{
    do
    {
    switch(tot+j>=4 && tot+j<=8)
    {
        printf("Hit\nEnter next card\n");
        scanf("%d",&j);
        break;
    }
    switch(tot+j==9 && (cat.dealersCard==2 || (cat.dealersCard>=7 && cat.dealersCard<=11)))
    {
        printf("Hit\nEnter next card\n");
        scanf("%d",&j);
        break;
    }
    ...
    switch(tot+j>=17 && tot+j<=21)
    {
        printf("Stand\n");
        j=50;
        break;
    }
    switch(tot+j>21)
    {
        printf("Bust\n");
        j=50;
        break;
    }
    }while(j!=50);
}
else  //etc.

This method didn't work either. I know this is simple but I can't find the answer anywhere. It would be great if you could answer for the second case but any answer is appreciated!

only use stdio.h, no global variables

2
In the first code, you need to increment tot, not j, don't you? Or you need to read into tot instead of j, but then the names, to the extent they are at all meaningful, are wrong. You really need to check the return value from scanf() too, so you can spot that the user typed K (for King) instead of 9. - Jonathan Leffler
That's one weird way of writing switch :) - Unmanned Player
This is the way I was taught, what’s the usual way? - Asher Leask
This is not the way I was taught. It was a late night... - Asher Leask

2 Answers

3
votes

You need to increment tot rather than j. I think it could be clearer like this:

void additionFunction(void)
{
    int tot = 5;

    while (tot < 22)
    {
        int j;
        printf("Enter next card\n");
        if (scanf("%d", &j) != 1)
            break;
        tot += j;
        printf("Read: %d - total = %d\n", j, tot);
     }
}

I've kept your variable names, but they could be improved (tot ⟶ total; j ⟶ value, perhaps). I added the second printf() to identify what's going on better (but there's room to improve that, too, though a debugger could also be used to see the information). I suspect you will need to do more work in the loop, or return a value from the function, but this is OK as an MCVE (Minimal, Complete, Verifiable Example).

3
votes

The problem is you are resetting j every time in the loop with user's input with this line - scanf("%d",&j);

This should fix it, by assigning input into another variable and add it to j.

 int j=0,input,tot=5;

 for(j=0; j+tot<22; j=j+input)
 {
     printf("j=%d tot=%d\n",j,tot);
     printf("Enter next card\n");
     scanf("%d",&input);
 }