Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have some homework about a "hangman" game and initially I need to show the rules on the console for only 5 seconds after which it must dissappear and the game will start. How can I achieve this in C?
For example:
"You can only try 5 times"
After 5 seconds this should dissappear and the game will start.
I am using the DEV-C console and have researched the time.h library but the part I'm most stuck at is how to make the text dissappear.
You could do something like this using sleep if you don't need to do anything during those 5 seconds:
int main()
{
printf("You can only try 5 times");
sleep(5);
// Start game
return 0;
}
For dissappearing text you alluded to in comments you can use carriage returns \r:
printf("\rI will overwrite the previous text!");
But this only works if you're overwriting with a string longer than what's already printed. You can print a blank line first to 'erase' it to get around this.
Finally, as most output streams are buffered, your text may not print without the newline \n character straight away, to get around this you can use fflush(stdout) so your final implementation might look something like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("\rYou can only try 5 times");
fflush(stdout);
sleep(5);
printf("\r ");
fflush(stdout);
printf("\rThe game will now begin.");
fflush(stdout);
return 0;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I don't know why, but my code is not taking input.... where did i do mistake???
After running it just prints this:
Type your input (press enter to save and exit).🙂️
Done, your file is saved successfully🤩️
My code is:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fpp;
char Entry;
char sid;
fpp = fopen("sid","w");
if (fpp == NULL)
{
printf("Sorry🙁️ file not created\n");
exit(0);
}
printf("Type your input (press enter to save and exit).🙂️\n");
while (1)
{
putc(Entry,fpp);
if(Entry =='\n')
break;
}
printf("Done, your file is saved succesfully🤩️\n");
fclose(fpp);
return 0;
}
yes, guys I used scanf() instead of putc(). My online tutor said me to write this...
while((ch=getchar())!='\n')
{
putc(Entry,fpp);
}
and I used that.... but now I used this code and it worked.
while (1)
{
scanf("%c",&Entry);
if(Entry =='\n')
break;
}
In order to get this out of the list of unanswered questions I compile an answer from comments.
Credits to the commenters: MikeCAT, 0x5453, Vlad, Daniel Farrell.
Other commenters provided wise input, which I however do not see as immeditate parts of the solution.
Your question is not very specific about what puzzles you about the shown output so here is my guess, along with explanation:
Why does any input you provide not have any influence on program behavior?
Because you do never take any input. It seems that you intend to read input with putc(Entry,fpp);, but as 0x5453 comments
putc is used for output, not effective input.
I.e. with no input reading functions called there is no input.
Why does Done, your file is saved successfully🤩️ occur at all? It should only occur after '\n' input.
Because you have an unconditional break; in your endless loop.
That might be non-obvious, but as Daniel Farrell commetns:
... the semicolon between if(...) and break essentially makes the if a no-op... Thus break is executed the first time the loop runs.
I.e. unconditional immediate break, end of loop, output. End of program.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
When I used the switch statement in the C language and ran it,the output contains two linebreak after it .
I don't know why and just want to get the end result.
Please tell me why and how to deal with it.
Thank you very much !
#include<stdio.h>
int main(){
int i;
printf("Please enter an integer:");
scanf("%d",&i);
switch(i){
case 1:printf("%d---A\n",i); break;
case 2:printf("%d---B\n",i); break;
case 3:printf("%d---C\n",i); break;
case 4:printf("%d---D\n",i); break;
default : printf("%d---E\n",i); break;
}
return 0;
}
when I input 1 will show:
The following code does not output a newline:
#include <stdio.h>
int main(void)
{
printf("Hello world");
}
Now, if my command shell did not start a new line, the prompt would be
Hello worldC:\TEMP>
So the shell starts a new line, and the console looks like this:
Hello world
C:\TEMP>
But if I output a newline in my program, there will be blank line:
Hello world
C:\TEMP>
The command shell does not know if the final output of the program was a newline or not, without exploring the console output. So for simplicity, it just starts a new line. It's not a bug. If you don't want a blank line, don't output a newline.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm working on my study for passing class. I've done everything, but my foor loop doesn't work well, what should i do for fixing it?
inside of 4th for loop, first loop (a) starts from 9, second loop (b) starts from 3. Why?
Code:
#include <stdio.h>
#include <conio.h>
int main () {
int i,j,a,b;
for(a=0; a<=10; a++){
for(b=0; b<=10; b++){
for(i=0; i<=3; i++){
for(j=0; j<=3; j++){
printf("a: %d -- b: %d -- i: %d -- j: %d \n",a,b,i,j);
}//--j for--
}//--i for--
}// --b for--
}// --a for--
getch ();
return 0;
}
Output:
Your program is fine, but it prints 1936 lines of output very quickly, so you only see the last 25 or so and scrolling back, you cannot get back to the beginning of the output because the Windows terminal seems to have very limited backscroll capability, maybe 150 lines.
You can try and increase the backscroll capability in the Windows settings pages (I have not used Windows for many years, I do not know where to do that nor if it can be done).
There are other methods to convince yourself that the code is fine:
redirect the output to a file and load that file: open a terminal window and type myprogram > output.txt in the terminal. You agree going to need to hit the keyboard because of the getch() at the end of the main function. As a matter of fact, you could remove this line altogether. As mentioned by manetsus, redirection can be forced from within the program by calling freopen("output.txt", "w", stdout); before the first for loop.
pipe the output through more. Again open a terminal window and type myprogram | less. The call to getch() is counterproductive for this too.
move the getch() to the end of the second loop (just before }// --b for--). The program will produce 16 lines at a time and wait for a key.
You are seeing the output in the console and every console provides a limited buffer memory.
So, the previous outputs were generated, but they are truncated in the consoled you showed.
You can get the full output in a file named output.txt by adding the following line as the first line of your main() function:
freopen("output.txt","w",stdout);
Or, from linux console, you can write the following command as #BLUEPIXY said in the comment:
./a.out > out.txt
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
For example, I run my program like:
program.exe < text.txt
I want the program to read from file text.txt. How do I begin doing this?
Ok, as this is a textfile, you probably want to read it line by line, so
char buf[1024];
while (fgets(buf, 1024, stdin))
{
/* do whatever you need with the line that's in buf here */
}
Note your code doesn't know about the file, it just reads from standard input. With <, you tell your environment (CMD on windows, a shell like e.g. bash on *nix) to open that file for you and provide it to your program as the standard input instead of the default, the controlling terminal, which would normally just read from the keyboard.
Hint: 1024 is kind of a random pick, most text files don't have lines exceeding 1kb. You might want to modify it to better suit your expected input.
another way to do what you are looking for is
#include <stdio.h>
int main (void) {
int c;
while ((c = fgetc(stdin)) != EOF) fputc(c, stdout);
return 0;
}
some more help here
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
#include <stdio.h>
int main()
{
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if (a>=b)
{
if(a>=c)
printf("Largest number = %.2f",a);
else
printf("Largest number = %.2f",c);
}
else
{
if(b>=c)
printf("Largest number = %.2f",b);
else printf("Largest number = %.2f",c);
}
return 0;
}
when I compile. the code will scan for the 3 numbers but wont do anything afterwards. even if i put {} around every if and else statements, it wont change.
As Shubham suggested, try putting something at the end of the program that prevents the windows command line interface from closing instantly.
A getchar() from stdio.h is more appropriate than getch() from conio, because it's in the standard library.
If you run the program from a command line interface and not by double clicking the icon or hitting the run-button in the IDE your program runs fine without the getchar(), as you would expect.
You can also check if your IDE supports an option to leave the command line interface open after the program has terminated.
Another option is setting a breakpoint on the last line of your main() function.
Try putting a getch() before the return 0 statement. Don't forget to #include <conio.h>.
What is happening is that the program displays the result and closes immediately.
EDIT:
Assuming you are on windows
you have include one more header file like conio.h . you may aware of this one....sorry to remember you this basic header file for output window.......then before your return 0; line just write getch();
Sample like:
#include <stdio.h>
#include <conio.h>
int main()
{
/* your code to check conditions for which number is greater among these numbers */
getch();
return 0;
}