1
#include<stdio.h>
#include <stdlib.h>

int triArea(x, y) {
    int baseTimesHeight = x * y;
    int area = baseTimesHeight / 2;
    return area;
}

int main() {
    int x = 0;
    int y = 0;

    printf("Enter an integer value for the length: \n");
    scanf("%d", &x);

    printf("Enter an integer value for the height: \n");

    int area1 = triArea(x ,y);
    printf("The area of the triangle is: %d\n", area1);

    printf("Hello world!\n");
    return 0;
}

this is the code that i wrote for a simple calculation (I am VERY new to programming in c and low level languages) however no matter the program if it has a scanf it will not output any other code. Anyone have idea as to why this happens? I'd like both a solution and explanation if possible!:)

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • 1
    You don't have a `scanf` for the `y` value. And what exactly do you mean by "won't output anything whatsoever"? Does it crash, do you see the first `printf()` prompt only? – Ken Y-N Sep 13 '21 at 09:05
  • 1
    When the program runs, what input are you providing to it? – Yun Sep 13 '21 at 09:05
  • Please [edit] your question and show the input and actual output and the expected output. I get the output `The area of the triangle is: 0` (among other output lines) which is what I expect because `y` is 0. – Bodo Sep 13 '21 at 09:06
  • 3
    `int triArea(x, y)` Does your compiler issue some warning about that line? Implicit `int` type is an ancient feature and should not be used in any program from last 4 decades – Gerhardh Sep 13 '21 at 09:08
  • Have you tried to step through the code statement by statement with a debugger? – Some programmer dude Sep 13 '21 at 09:08
  • 1
    Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 19 '21 at 04:19

1 Answers1

1

You forgot the scanf for y:

...
printf("Enter an integer value for the height: \n");
scanf("%d", &y);  // <<<<< you forgot this line

and int triArea(x, y) should be int triArea(int x, int y). Any decent compiler should issue a warning for this.

Bonus: why do you call your variable x, and y? Call them length and height instead. Code readability is very important, and correct naming of variables and functions is very important.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • I'm just using VSC with a c complier i installed a while ago (unsure what compiler) and the c/c++ compliers from the marketplace, it does not raise any warnings whatsoever. Also in the actually code block I ran I had both the scanf for x and y, it did not output anything at all, including the prints. I tried this with other code, like ones that just printed a line (this worked fine) then no matter where i put a scanf, it does not output anything. I think i'll try and move and learn vim and use a different compiler! – Joshua Mellon Sep 13 '21 at 11:10
  • @JoshuaMellon I don't recommend to use `vim` unless you really want to learn using an old editor with an unusual user interface. You can use any text editor. If you have problems whenever you add a `scanf` you should concentrate on this fact in your question. I suggest to simplify your program by removing everything that is not necessary to reproguce your problem. Show a version that works fine including input (if any) and output, and a version with a minimal change that leads to your problem, again with input and output. Add all details about OS and compiler to your question. – Bodo Sep 13 '21 at 11:49
  • @JoshuaMellon: Maybe the compiler you are using is ok, but you only have to enable the warnings. I recommend gcc or clang. See [this question](https://stackoverflow.com/q/57842756/12149471) for details on why enabling warnings is important and how to do that. – Andreas Wenzel Sep 13 '21 at 12:20
  • @JoshuaMellon While this answer helps you solve one problem, it doesn't explain your statement that your program "won't output anything whatsoever". Did you really mean that when you run it there's really no output at all? For your next question please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Sep 13 '21 at 14:36
  • There is absolutely NO output, the only output i get is when i terminate the code and that's just how long the code was running for. – Joshua Mellon Sep 13 '21 at 15:13
  • 1
    @JoshuaMellon If you call `fflush(stdout);` after every `printf` call, does that show the output then? – Some programmer dude Sep 13 '21 at 17:13
  • @Someprogrammerdude shouldn't the `\n`s force an output buffer flush? – Jabberwocky Sep 13 '21 at 17:17
  • 1
    @Jabberwocky `stdout` is line-buffered only if connected to a terminal. Otherwise it's fully buffered. Some environments uses pipes to capture output from the programs they're running, so `stdout` isn't line-buffered and needs to be flushed explicitly. – Some programmer dude Sep 13 '21 at 18:21
  • Sorry to everyone who wasted their time responding to me, turns out for no aparrent reason that VSC doesn't take inputs from c in their console. It works with other languages but refuses to with c, not sure why. – Joshua Mellon Sep 14 '21 at 07:00