0

I do not know what I did to my vscode but not it does not display anything, sometimes even a simple printf. The code runs and then terminates without results. I have installed gcc as the compiler and set it to run in terminal. Here is the code

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

void bubble_sort(int *deck, int size);

int main() {
    srand(time(NULL));

    char *suits[] = { "Hearts", "Clubs", "Diamonds", "Spades" };
    char *ranks[] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",          "Jack", "Queen", "King" };
    
    int deck[52];

    for (int i = 0; i < 52; i++) {
        deck[i] = i;
    }
    int index1 = 0, index2 = 0;


    for (int i = 0; i < 50; i++) {
        index1 = (rand() % 51);
        index2 = (rand() % 51);
        int temp = deck[index1];
        deck[index1] = deck[index2];
        deck[index2] = temp;
        
    }

    int count = 0;
    for (int i = 0; i < 52; i++) {
        printf("%s of %s\n", ranks[deck[i] % 13], suits[deck[i] / 13]);
    }

    bubble_sort(deck, 52);

    for (int i = 0; i < 52; i++) {
        printf("%s of %s\n", ranks[deck[i] % 13], suits[deck[i] / 13]);
    }

    
}

void bubble_sort(int *deck, int size) {
    int temp = 0;

    for (int i = 0; i < size; i++) {
        for (int j = 1; j < size; j++) {
            if (deck[j] > deck[j - 1]) {
                temp = deck[j];
                deck[j] = deck[j - 1];
                deck[j - 1] = temp;
            }
        }
    }

}

Result- it only displays the concatenated name

PS C:\Users\Macharia Mbugua\OneDrive\Desktop\cc++\.vscode\Array_Stuff> cd "c:\Users\Macharia Mbugua\OneDrive\Desktop\cc++\.vscode\Array_Stuff\" ; if ($?) { gcc ShowCards2.c -o ShowCards2 } ; if ($?) { .\ShowCards2 }
Joe Mbugua
PS C:\Users\Macharia Mbugua\OneDrive\Desktop\cc++\.vscode\Array_Stuff> cd "c:\Users\Macharia Mbugua\OneDrive\Desktop\cc++\.vscode\Array_Stuff\" ; if ($?) { gcc ShowCards2.c -o ShowCards2 } ; if ($?) { .\ShowCards2 }
Joe Mbugua
PS C:\Users\Macharia Mbugua\OneDrive\Desktop\cc++\.vscode\Array_Stuff>

I have tried restarting and reinstalling VScode but nothing works. The expected results should be a list of cards in random order then a list of sorted cards

dbush
  • 205,898
  • 23
  • 218
  • 273
  • This seems like a very good time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. More specifically how to run your program inside a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to catch any possible crashes. And if there's no crashes, then use the debugger to step through the code line by line while monitoring variables and their values. – Some programmer dude May 05 '23 at 21:43
  • On a different note, you don't use anything from the `` or `` header files. There's no need to include those headers. – Some programmer dude May 05 '23 at 21:46
  • 1
    Besides, is this really the code that fails you? Because when I copy-paste it and try it myself [I can't replicate your problem](https://godbolt.org/z/xz3Wbhbz6). Especially, I don't get that `Joe Mbugua` output you seem to have. Where does it come from? Are you building and running the correct code? – Some programmer dude May 05 '23 at 21:47
  • _it only displays the concatenated name_ Where is this code even concatenating a name? Are you getting your projects mixed up? – yano May 05 '23 at 21:52
  • "I have tried restarting and reinstalling VScode but nothing works." You are doing one unnecessary step. Uninstall VSCode *and do not install it again*. There are plenty of IDEs that work. Why fiddle with one that doesn't? – n. m. could be an AI May 05 '23 at 21:57
  • No I deleted the code for the concatenated name so as not to cause more confusion(now counter-productive). The main problem is that the code will not display the cards – Felix Mbugua May 05 '23 at 21:57
  • Ignoring the name part, nothing displays in VScode. – Felix Mbugua May 05 '23 at 21:59
  • 1
    Is the code you show us from the file `c:\Users\Macharia Mbugua\OneDrive\Desktop\cc++\.vscode\Array_Stuff\ShowCards2.c`? Did you remember to save the file before building and running? And please note that the coderunner plugin you're using to build and run will only build and run the active tab in the editor. Please [read the documentation](https://code.visualstudio.com/docs/cpp/config-mingw) for the actual C++ plugin, and how to configure it to build, run and *debug*. – Some programmer dude May 05 '23 at 22:03
  • 1
    Are you actually running your code? Your quoted command includes a `cd` command, a compilation step, and then finally running it. Could it be failing in one of the first two steps and so not running the code at all? Build it and run it as separate commands. – pmacfarlane May 05 '23 at 22:07
  • Stop. Don't mix code problems and some IDE problem. They are two different issues. As a test, simply compile your code from the command line in a terminal, run it. The posted code is fine (other than the unused `int count = 0;`, and unneeded headers). If all works when you build/run from the command line -- you have an IDE problem. An IDE is supposed to make things easier instead of harder. If that's not the case, forget the IDE for now. You don't need one for compiling projects with less than a dozen or so source files (and then `make` or `ninja` or `cmake` will do) – David C. Rankin May 05 '23 at 22:43
  • HInt: you can make you output more readable with `printf("%2d %s of %s\n", i+1, ranks[deck[i] % 13], suits[deck[i] / 13]);` and a `putchar('\n');` between the loops. – David C. Rankin May 05 '23 at 22:46
  • To repeat: The program you run seems to output `Joe Mbugua`. The code you show does not. Can you explain that? – Some programmer dude May 06 '23 at 06:27

0 Answers0