1

When I run the program on a Cygwin terminal, I am not able to achieve the following output:

38 $92360.88 llama

Notepad++ Cygwin Terminal Program Execution

// input.c -- when to use &
#include <stdio.h>
int main(void)
{
    int age;            // variable
    float assets;       // variable
    char pet[30];       // string
    
    printf("Enter your age, assets, and favorite pet.\n");
    scanf("%d %f", &age, &assets);      // use & here
    scanf("%s", pet);                   // no & for char array
    printf("%d $%.2f %s\n", age, assets, pet);
    
    return 0;
}

I am not sure what the most effective way to include my code is here without it looking a little choppy. My apologies in advance.

Thank you Brendan

Toto
  • 86,179
  • 61
  • 85
  • 118
Brendantr
  • 13
  • 4
  • No photos of the code please. Post as formatted text in the question body. https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question – Eugene Sh. May 12 '22 at 18:34
  • Welcome! Copy/pasting of the text of the exact code is the best method, a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), which is the shortest *complete* code that shows the problem. – Weather Vane May 12 '22 at 18:40
  • 1
    The code looks OK and it should produce the expected output as per input shown on your Cygwin session. The only issue I can think of is that you are not running the executable compiled from this code. – Eugene Sh. May 12 '22 at 18:42
  • 1
    @EugeneSh. Thank you. I discovered that I had two copies of the input.c file. I correct the one I kept saving and updating was located in the wrong location. The incorrect file, which I thought was the updated one was the wrong code, but I kept thinking I was updating the proper file. Thank you, I fixed the issue! – Brendantr May 12 '22 at 18:48
  • 1
    The code is fine, the problem is somewhere else, when I say fine I'm not including the lack of width specifier for `scanf`, `%29s` or ideally the use of `fgets`: – anastaciu May 12 '22 at 18:48
  • @WeatherVane, thank you for the suggestions. I am glad to get connected with this website and hopefully make good use of it during my journey of learning how to program. Best... – Brendantr May 12 '22 at 18:48
  • I am glad you solved the issue, I was about to comment this - Perhaps you need `fflush(stdout)` after printing. Related: [C90 printf with \n or \r\n not working in cygwin; but fflush(stdout); works fine. why?](https://stackoverflow.com/questions/17984660/c90-printf-with-n-or-r-n-not-working-in-cygwin-but-fflushstdout-works-fine) – Weather Vane May 12 '22 at 18:49
  • @anastaciu, thanks for the response. I found that the issue was I had two files with the same name. I was editing and saving one file, but it was in the wrong location. I deleted the old file and replaced it with the correct version. Thanks for the help. – Brendantr May 12 '22 at 18:50
  • The code should work for the input you specify, but if the "pet" in your input file has a sufficiently long name, it will crash your program. You certainly won't get this code through any code-review. – user1934428 May 13 '22 at 06:56

0 Answers0