Xcode not showing output - c

I was instructed to write a game of dice in C language in which the computer and the user act as competing sides of the game. First, a random number is generated by the computer, and then the string "g" command input by the user is accepted to generate the user's random number (simulate to roll a dice once), and compare their values. If the random number obtained by the user is less than that obtained by the computer, output "Sorry, you loss!", if the results is otherwise, output "Congratulations, you won!".
The code:
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand((unsigned)time(NULL));
int com_inp = (rand() % (6 - 1 + 1)) + 1;
int user_inp=0;
char ch;
scanf("%c",&ch);
if(ch=='g')
user_inp = (rand() % (6 - 1 + 1)) + 1;
if(user_inp<com_inp)
printf("\n Sorry, you lost!");
else
printf("\n Congratulations, you won!");
printf("\n Computer dice = %d \t User dice = %d",com_inp,user_inp);
return 0;
}
After running on Xcode v12.2, no output is shown. The console is blank.
However, the output is shown if the program is run on Dev-C++(Windows).
Any help would be appreciated.

I don't see anything printed before the scanf. I am not familiar with Dev-C++ on Windows, but in the Xcode console there is no prompt to show you that it is waiting for input, so you might want to put something like:
printf("Enter a number between 1 and 6\n>");
before your scanf.
You may also be running into some line-buffering issues. Try adding \n at then end of each line you are printing.
if(user_inp<com_inp)
printf("\n Sorry, you lost!\n");
else
printf("\n Congratulations, you won!\n");
printf("\n Computer dice = %d \t User dice = %d\n",com_inp,user_inp);

Related

Difference in output between Command Prompt looking terminal and VSC Terminal

I recently started learning C and have just started using VSC. I was previously using CodeBlocks. While testing out a simple array program, I realised that there is a difference in output. For reference, the array program code is shown below.
#include <stdio.h>
int main() {
int grades[10];
int count = 10;
long sum;
float average;
printf("\nPlease enter the 10 grades: \n");
//printf("%i", count);
for(int i = 0; i < count; i++) {
printf("%2u> ", i+1);
scanf("%d", &grades[i]);
sum += grades[i];
};
average = (float)sum/count;
printf("The average of the 10 grades are %.2f", average);
return 0;
}
I am able to run the for loop but the code after the for loop does not appear in the CMD like terminal and the program just quits after the for loop. However, the code after the for loop does appear in the VSC terminal. I am able to enter the ten values.
The VSC Terminal Output:
However, the CMD looking terminal did not manage to print the code after the for loop and instead exits the program.
Could it be because of this error: "The preLaunchTask 'C/C++:gcc.exe build active file' terminated with exit code -1." everytime I click 'Run > Start Debugging'.
Sorry for this messy format but thank you in advance for helping!

My Visual Studio Code won't run my code properly

I can't properly run/debug my code in VS Code using the C language. I've installed C/C++ package on VSC, Mingw & applied the path for Mingw. All my files are running .c format as well.
Only the last part of my code keeps crashing in VSC, when I run this same code on website compilers, it works!
Here is my code:
#include <stdio.h>
int main(void) {
int num1;
int num2;
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter another number: ");
scanf("%d", &num2);
printf("Answer: %d ", num1 + num2);
return 0;
}
That last printf is where VSC just shuts down the output window, so I never get to see the end result of my code. Anyone have any solutions to fix this? It'd be greatly appreciated!
When you run your console program from Visual Studio, it opens a terminal window, runs the program and the terminal window closes automatically when the program exits. This is a classic problem with the Microsoft Windows platform that they do not seem to care about despite millions of newbie programmers like you experiencing the same problem.
If you open the terminal window yourself, by running the CMD command from the start menu, you will be able to run your program manually after changing the current directory to that of the program binary.
To prevent the terminal window from closing immediately when running directly from Visual Studio, you should add 2 getchar(); statements before returning from main() to wait for user input and get a chance to see the output. Just reading a single byte with getchar() will not suffice because it will just read the pending newline entered by the user in response to the second prompt.
Also note that it is preferable to output a trailing newline to ensure the output is properly flushed on some legacy systems:
printf("Answer: %d\n", num1 + num2);
Here is a modified program you can test:
#include <stdio.h>
int main(void) {
int num1 = 0, num2 = 0;
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter another number: ");
scanf("%d", &num2);
printf("Answer: %d\n", num1 + num2);
getchar(); // read the pending newline
getchar(); // read at least another byte from the user.
return 0;
}
run you program from the console:
In the search box type cmd
cd \path_to_your_executable
run your program.

C language :Scanning input from Eclipse console [duplicate]

This question already has answers here:
C/C++ printf() before scanf() issue
(2 answers)
Closed 4 years ago.
I am a java programmer and started learning C recently. I was going through headfirst C and wrote one of the example programs in Eclipse (with CDT). Here is the program
#include <stdlib.h>
#include <stdio.h>
int main()
{
char card_name[3];
puts("Enter the card_name: ");
scanf("%2s",card_name);
int val = 0;
if(card_name[0] == 'K'){
val = 10;
}else if (card_name[0] == 'Q'){
val = 10;
}else if(card_name[0] == 'J'){
val = 10;
}else if(card_name[0] == 'A'){
val = 11;
}else{
val = atoi(card_name);
}
printf("The card value is: %i\n",val);
return 0;
}
When I run it on eclipse, the line containing puts should be executed and then the user should enter in eclipse console and then the scanf line should execute.
But it doesn't happen that way, when I run it, it just expects the user to enter first on the eclipse console, then execute the puts line and finally the scanf line. I couldn't understand this behavior. Can someone help me on this ?
Depending on how the buffering of the output is configured, you might nee to be very explicit about when to actually make the output.
Usually a newline at the end of text is sufficient, i.e. there must be something special in your environment, I suspect the Eclipse console, but that is just guessing.
Especially when attempting to do prompt and reading within one line, it is wise to use a
/* ... */
puts("Enter the card_name: ");
fflush(stdout);
/* ... */
at exactly the point where you need the output you have already send.
In your case the buffering causes this sequence:
put the prompt in output buffer
environment is configured not to automatically output immediatly
(this is where I propose to flush in order to get output)
scanf is executed
user input
your program does not do anythign visible, though executing completly
put the final message into output
ending the program causes final output of anything buffered
I.e. the scanf is not executed before the first puts, it is only the visible effect which gets delayed.

Simple C program executed in the terminal with prompt alogside the final output

I have written a simple "hello, world" program from here http://www.cprogramming.com/tutorial/c/lesson1.html cprogramming: lesson 1, the output is shown then the terminal prompt shows up in the following one. When executing another program:
#include <stdio.h>
main()
{
int age;
printf("How old are you?");
scanf("%d", &age);
if (age <= 20)
{
printf("You are still young");
}
else if (age >= 20)
printf("You are not that young anymore!");
else if (age >= 30)
printf("Hello young man!\n");
getchar();
return 0;
}
The output is shown in the same line as the terminal prompt in Gnome Terminal 3.6.1 on Ubuntu 13.10. I just don't know if this a code issue or is it related to the terminal only.
Alignment can be done using escape sequences.
In your case u need to use \n for new line
Add a newline character after each of your printf's
example:
int age;
printf("How old are you?\n");
scanf("%d", &age);

Using putchar and printf together in the same C Program?

I've written a small C program where I wanted to display the numeric ASCII value that corresponds to certain key presses.
My code follows. The problem is, after running the program, it accepts input, but doesn't do anything else. It doesn't even reach the first printf statement. I can't figure out what the issue is - is there a problem with mixing the getchar() function with the printf() function in the same program?
#include <stdio.h>
int main() {
const int numKeys = 256;
int keys[numKeys];
int i;
for (i = 0; i < numKeys; i++) {
keys[i] = 0;
}
printf("\n Start pressing some keys!\n\n");
int c;
while ((c = getchar()) != EOF) {
printf(" CAPTURED: %d\n", c);
keys[c]++;
}
printf("\n\n ** RESULTS ** \n\n");
for (i = 0; i < numKeys; i++) {
if (keys[i] != 0) {
printf(" Key with value %d was called %d times.", i, keys[i]);
}
}
}
I should clarify that I have a Windows XP Pro machine, with Cygwin installed. I use Cygwin for my development space, so I wonder if there is something different when running this type of program in that environment.
I found the problem. I think you want to use
while ((c = getchar()) != EOF && c != '\n')
Instead if you want to have it print the results after the person hits enter/return.
problem 1 : getting to printf(" CAPTURED: %d\n", c); without having to press the Enter key
solution : is by using getche() in while loop.
problem 2 : getting to 'printf("\n\n ** RESULTS ** \n\n");' or essentially breaking while loop?
solution : you cannot. you will never get EOF as long as you read from keyboard.
workaround : close stdin or use a escape character other than EOF.
EDIT : workaround2 :
->use getchar() itself. but to print those entered char u need to press Enter key. now on windows ctrl+z gives EOF but this should be the **FIRST** input on the line after you press Enter key. well this is not a good solution.
if you want a "Press key display times pressed scenario. there is just no simple way(AFAIK)"
I believe that the first printf statement gets executed, but due to buffering is not displayed on the screen immediately. Use fflush(stdout) to send the contents of the buffer to the screen. Ie:
printf("\n Start pressing some keys!\n\n");
fflush(stdout);

Resources