0

I have java background but I am new to C programming and this is my first HW assignment so forgive me if this is a simple fix. I need to ask a customer what their name is & what they want to buy. I began like this:

#include <stdio.h>
#include <stdlib.h>
#define TSHIRT  18.95
#define CHIPS   1.79
#define COKE    2.99
#define TAX     0.06
#define DEPOSIT 1.20

int main(void) {
printf("Hello customer! What shall I call you?");

char name[20];
scanf("%s",name);
printf("Okay %s, here is what we have to offer:",name);
return EXIT_SUCCESS;
}

When the program runs, it only shows very briefly on the console then disappears leaving the console blank. What is the reason why?

  • 1
    The program has nothing else to do. If you want him to stop add an input operation with getchar() for example(before the return statement). – caiomcg Sep 14 '16 at 00:20
  • Are you saying that it does not wait for you to type a name? – M.M Sep 14 '16 at 00:47
  • There are dozens of possible answers, all of them have been covered in depth before. Learn to use Google. http://stackoverflow.com/questions/1864029/how-do-you-keep-the-console-from-closing-after-the-program-is-done-in-c http://stackoverflow.com/questions/1173208/what-is-the-best-practice-for-combating-the-console-closing-issue – David Sep 14 '16 at 00:55

2 Answers2

3

You're returning at the end of your statement and I assume you're using Visual Studio which will terminate the console as the application has finished running. One thing you could do is add a break point before the return or the simpler thing is to fudge it with getch() e.g.

#include <stdio.h>
#include <stdlib.h>
#define TSHIRT  18.95
#define CHIPS   1.79
#define COKE    2.99
#define TAX     0.06
#define DEPOSIT 1.20

int main(void) {
printf("Hello customer! What shall I call you?");

char name[20];
scanf("%s",name);
printf("Okay %s, here is what we have to offer:",name);
getch();
return EXIT_SUCCESS;
}

If that doesn't work add the include conio.h, but I believe it works without it.

#include <conio.h>

Also, to help you avoid a buffer overflow you should use scanf like this:

scanf("%19s",name);

You don't want to scan more than your allocated buffer and you should use the buffer length minus one because scanf appends a null terminator to the end of the scan.

James Lockhart
  • 1,030
  • 11
  • 17
0

Make the following subtle changes:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>  // It contains information of getch() function which we used later in program
#define TSHIRT  18.95
#define CHIPS   1.79
#define COKE    2.99
#define TAX     0.06
#define DEPOSIT 1.20

int main(void) {
printf("Hello customer! What shall I call you?");

char name[20];
scanf("%s",name);
printf("Okay %s, here is what we have to offer:",name);
getch();   // It will wait for one char input from user
return EXIT_SUCCESS;
}

The getch() waits and holds the screen until you enter any single character. Hope it will be helpful!

Harshil Doshi
  • 3,497
  • 3
  • 14
  • 37