Finding the largest of 3 numbers [closed] - c

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;
}

Related

Why is writing and saving strings and integers in a file not work? [closed]

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 last month.
Improve this question
I need help with this code. You see, it is taking inputs likes strings and integers and saves them in two arrays . Those two should be written into a file with the name "Lagerverwaltung.text". However it just prints a 0 and nothing else into the file.
Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char artnr[50],menge[50],me[50],neu[50],date[50];
int zahl, calcam, id, sub,amount;
int greatlen = 0;
int result = 0;
char str[50][50][50];
int mengen[10];
int a = 1;
int s = 0;
while(a > 0){
FILE* fp;
fp = fopen("Lagerverwaltung.txt", "w");
printf("Geben sie eine Zahl ein:");
scanf("%d", &zahl);
if(zahl == 1){
printf("Geben sie ein:\nArtikelnr.:");
scanf("%s",&artnr);
strcpy(str[s][0],artnr);
printf("Menge:");
scanf("%d",&mengen[greatlen]);
printf("Mengeneinheit:");
scanf("%s",&me);
strcpy(str[s][1],me);
printf("Datum:");
scanf("%s",&date);
strcpy(str[s][2],date);
}
fputs(str[greatlen][0], fp);
fprintf(fp, "%d", mengen[greatlen]);
fputs(str[greatlen][1], fp);
fputs(str[greatlen][2],fp);
fclose(fp);
s =s+1;
greatlen = greatlen +1;
}
return 0;
}
There should be a line of integers and strings written into a file.
fopen with "w" parameter opens the file and discards existing content. That means that in each loop iteration you discard whatever you have written previously. Since a never goes to 0, the only option to end the program is to abort it, and you'll be doing that while it waits for input, which is after it has already discarded any file content but before it has written new content.
Possible fixes:
open the file with "a" to append to it
open the file before the loop and close it after the loop (while providing a way to exit the loop).
Also, fix the string-scanning lines like
scanf("%s",&artnr);
that should be
scanf("%s", artnr);
and every decent compiler would warn you about it.

can anyone help me? My code is not taking input [closed]

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.

Why withot newline charecter printf doesnot work? [closed]

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 4 years ago.
Improve this question
I was asked to write a program to blink the given word without using clrscr function in c and I am tring this following code when I add \n in the printf then only it works fine and when I remove \n it doesnt display anything why this is so?
#include <stdio.h>
#include <unistd.h>
int main()
{
int j=10;
while (j--)
{
printf("BLINK");
sleep(1);
system ("clear");
sleep(1);
}
return 0;
}
Buffering.
Output to stdout (which is where printf writes) is by default line buffered. That means the text written to stdout is put into a buffer only, and is not flushed (actually written to the device) unless either the buffer is full, or a newline is written.
Information about this should be in any good book, tutorial or class.
You can solve your problem by adding a flush of stdout.
#include <stdio.h>
#include <unistd.h>
int main()
{
int j=10;
while (j--)
{
printf("BLINK");
fflush(stdout);
sleep(1);
system ("clear");
sleep(1);
}
return 0;
}
But system("clear") will probably make code reviewers cry blood, so maybe consider something like backspace escape sequence.
#include <stdio.h>
#include <unistd.h>
int main()
{
int j=10;
while (j--)
{
printf("BLINK");
fflush(stdout);
sleep(1);
printf("\b\b\b\b\b \b\b\b\b\b");
fflush(stdout);
sleep(1);
}
return 0;
}

Making a text dissappear after x second [C] [closed]

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;
}

Why does the for loop showing last part of the output? [closed]

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

Resources