#scanf() not working

12 messages · Page 1 of 1 (latest)

glacial patrol
#

I am starting to learn C but have come to a roadblock when it comes to using the scanf() function. I am creating a program which produces the weight and volume of a box given the dimensions of said box. My program works completely fine when I assign dimensions (and avoid the scanf() function) but if I try to have the user input the dimensions using scanf() nothing is returned, not even printf() functions before those scanf() functions.

Seems like it would be an easy fix but im still very unsure on how to go about fixing this issue!

verbal mothBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

glacial patrol
#

#include <stdio.h>

int main(void)
{
printf("test"); //tests to see if line is being read

int height, length, width, volume, weight;

printf("Enter height of box: ");
scanf("%d", &height);
printf("Enter length of box: ");
scanf("%d", &length);
printf("Enter width of box: ");
scanf("%d", &width);

volume = height*length*width;
weight = (volume +165)/166;

printf("Volume: %d\n", volume);
printf("Dimensional weight: %d\n", weight);

return 0;

}

mint notch
#

first off - please don't use coderunner.
second - the program's output should be under the terminal tab. the output tab is (confusingly) for the output of your extensions

glacial patrol
#

okay wait how would i run it through the terminal? sorry im super new to coding

mint notch
#

you better start looking into compilation and/or build systems. for now you may compile it with
gcc your_file_name.c -o your_exe_name -Wall -Wextra -Wpedantic
and run it with
./your_exe_name

(if you don't want to supply an executable name gcc your_file_name.c -Wall -Wextra -Wpedantic. this will result in your exe named a.out)

verbal mothBOT
#
How to Enable Compiler Warnings and Analyzers

Enabling compiler warnings is extremely important to ensure that your code is correct. Countless mistakes can be caught by the compiler before you ever run your code, such as missing return statements, unused variables, etc.

Static analyzers run additional tests on your code, which are much more expensive. They can spot more complex mistakes, such as dereferencing null pointers.

Warnings for GCC and clang
Warnings for MSVC
  • recommended: /W4
  • warnings as errors: /WX
  • disble warnings: /wd
  • analysis: /analyze
Passing Warning Flags to the Compiler on the Command Line
g++ -Wall -Werror -o program main.cpp
Passing Warning Flags indirectly using CMake
# GCC/clang-specific, needs compiler detection
target_compile_options(program PRIVATE -Wall -Werror)
mint notch
#

this provide some explanation regarding those flags i wrote

glacial patrol
#

i'll look into those. thank you

#

!solved

verbal mothBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

mint notch
#

np