skip to Main Content

Im a first year CS student and currently enrolled in an intro to C class so I hope I don’t sound too ignorant lmao. We started the year using online compilers but ive been told to start the transition to VScode. I am on Mac and have so far on VScode I’ve installed coderunner, c/c++, and also clang through my terminal. I tried opening my notes through VScode and running it but it just gives me an indication that it’s running and no exit. A simple "hello world" code works as intended but with my notes I get nothing. I tried running the same code on the online compiler and it works and prompts me what it’s supposed to.

Here is the code:

#include <stdio.h>
struct gas{
        float distance;
        float gals;
        float mpg;
    };

struct gas f(struct gas g1);

int main()
{
    struct gas gg,gg_update; 

    printf("Enter distance: ");
    scanf("%f",&gg.distance);

    printf("Enter gals: ");
    scanf("%f",&gg.gals);

    gg_update=f(gg);

    printf("Distance: %f n",gg_update.distance); 
    printf("Gallons: %f n",gg_update.gals);
    printf("Miles per gallons: %f n",gg_update.mpg);
    return 0;
}

struct gas f(struct gas g1)
{
    g1.mpg=g1.distance / g1.gals;
    return g1; 
}

here is the expected input:

Enter distance: 200
Enter gals: 20

and the output:

Distance: 200.000000 
Gallons: 20.000000 
Miles per gallons: 10.000000 

Completely unrelated but not really id appreciate someone letting me know… why is that when I click just "run code" it prints out hello world but when I click "run C/C++ file" it tells me it is not a C file? Could that play a part in why im running into this issue?

Gonna be on here for awhile so if any further context is needed I can provide that. Also if anyone has the time to provide context as to what exactly each extension does id appreciate that too.

2

Answers


  1. In your original question, you included a screen snapshot. Yes, we always need to see code snippets as text-only MRE, not as an image (so we can reproduce the problem). But, in this case, the image offered a little clue as to where you were looking (I have added the red circle to your original screen snapshot):

    enter image description here

    You appear to be looking at the “Output” tab. But you likely want to run it in the terminal so that you can interact with the app.

    First, check the “Run in Terminal” setting (press +, to go to settings, search for “Run in Terminal”), and make sure that is checked “on” for Code-runner:

    enter image description here

    Now when you run the app (you might have to restart VSCode), go to the “Terminal” tab and you can interact with your app:

    enter image description here

    Login or Signup to reply.
  2. I think the answer by Rob would solve your problem. I would also recommend you to look into using the compiler directly instead of through an extension.

    GCC is a common and easy to use C compiler. You probably have it installed anyway but in case you don’t, use this command in your terminal to install it.

    brew install gcc
    

    A simple file compilation with GCC looks like this:

    gcc source_file_name.c -o output_file_name
    

    Here, your source file is 4A-1-2.c. (You should use underscores _ instead of dashes - or dots . in file names)

    You can use GCC to compile it like this:

    gcc "4A-1-2.c" -o 4A_1_2
    

    And then run the compiled program or executable like this

    ./4A_1_2
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search