0

I have two problems with the code, the first one being that the program wants me to enter my number twice and the second one being that the program closes down immediately after it has finished its process. I have tried to use the getchar() statement to stop it doing so but it doesn't seem to work.

#include <stdio.h>

int square(int);        /*function prototype*/

main()
{
    int x;              /*defining the function*/
    printf("Enter your number\n");
    scanf_s("%d \n", &x);       /*reading the users input*/
    printf("Your new answer is %d \n", square(x)); /*calling the function*/
    getchar();
    getchar();

}

int square(y) /*actual function*/
{
    return y * y;

}
CoolDude
  • 83
  • 1
  • 1
  • 9

2 Answers2

1

Fix the issue by changing

scanf_s("%d \n", &x);

to

scanf_s("%d", &x);

The problem was that a whitespace character (space, newline etc) in the format string of scanf insructs scanf to scan and discard any number of whitespace characters, if any, until the first non-whitespace character.


As for the problem with getchar(), replace the first getchar() with:

int c;
while((c = getchar()) != '\n' && c != EOF);

This will scan and discard everything until a \n or EOF.


Also, change

main()

to

int main(void)

and

int square(y)

to

int square(int y)
Spikatrix
  • 19,378
  • 7
  • 34
  • 77
  • If you work in C under Linux, you do not need `int main(void)`. You can use `void main(void)` or simply `main(){..}`. – Simply Me Feb 07 '16 at 12:31
  • 1
    @SimplyMe As per the latest standard, the valid forms of `main` are `int main(void)` and `int main(int argc, char** argv)` – Spikatrix Feb 07 '16 at 12:33
  • 1
    @SimplyMe Linux or not, `void main()` is not standard C. `main` has a return type of `int`.http://stackoverflow.com/q/204476/2072269, http://stackoverflow.com/a/4207223/2072269 – muru Feb 07 '16 at 12:33
  • @CoolGuy, I asume you talk about the C11. And yes, that is true. – Simply Me Feb 07 '16 at 12:36
  • @muru, yes, but I assume you never worked under the linux platform? There are little differences and the `gcc` allows the simple `main` and adds the requirements depending on what SO version you have. Running on older versions would be nice to conform to the C99 version, instead of the C11, to be the same with the standard that was available in that time. – Simply Me Feb 07 '16 at 12:38
  • 1
    @SimplyMe Have a look at my profile. :) GCC works fine on Windows too. And note that `int main(...)` is accepted by older versions just fine. So a cautious use would always be `int`, not `void`. – muru Feb 07 '16 at 12:40
  • 1
    @SimplyMe AFAIK, C89, C99 and C11 permits only `int main(void)` and `int main(int argc, char** argv)`. Otherwise, one should check if the implementation supports another signature of `main`. BTW, GCC will refuse to compile if you use `-pedantic-errors`. – Spikatrix Feb 07 '16 at 12:43
  • @muru, I didn't say I use void. Right the opposite, I use int. But on his example he had no int so I didn't complicate it for him. – Simply Me Feb 07 '16 at 13:12
0

I would recommend using scanf("%d", &x); to read your number. Your problem is that your argument looks like this:"%d \n" so the program expects you to enter your number AND \n. This way, you say how you want your x to look, and in your case, it expects it to be a numeric value, a space and end of line.

As for the closing one, use getch();. For this function, you need to include the conio.h like you did with stdio, meaningly: #include <conio.h>.

Simply Me
  • 1,519
  • 9
  • 22
  • Thanks that solved it all. I will use the `conio.h` library from now on. I will also find out what the differences are between the two libraries. Thanks a lot for answering my question, much appreciated – CoolDude Feb 07 '16 at 12:27
  • 2
    @bahjat Note that `conio.h` is non-standard and is not available on all platforms. – Spikatrix Feb 07 '16 at 12:32