Update previous line instead print new line - c

Here is a simple program in c. It take two integers and add them. In this code, I want to update previous line by new line instead making a new one, Can any body help me on this topic.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
printf("Enter two integer to add\r");
scanf("%d%d",&a,&b);
a=a+b;
printf("Your value is -- %d",a);
return 0;
}
I have use \r instead of \n, to take cursor back to the start. I have input data in program console which writing from start. But the next line "Your value is.." printed as new line, I want to remove the first line and then input value and print next line "Your value..". How do I do that? Please help me

You cannot move up in the terminal like that, unless using some complex lib as curses.
You can use the "clear screen" trick, maybe that would achieve what you want.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
system("clear"); // or "cls" on windows
printf("Enter two integer to add\r");
scanf("%d%d",&a,&b);
system("clear"); // or "cls" on windows
a=a+b;
printf("Your value is -- %d",a);
return 0;
}

Related

scanf() is not working for getting a sentence

I am learning the language C by myself and with the help of internet.
I came across an exercise, and I was able to read in everything with integers and double, but allowing the user to type in a full sentence and store it in a variable has given me hard time. Can someone explain how I can get a sentence from the user, and store it in a variable. I have tried many things, such as [%^\n] with scanf, and also fget but I am having some trouble. For some reason, it is not working.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i = 4;
double d = 4.0;
char s[] = "Orange ";
// Declare second integer, double, and String variables.
int secondInt;
double justDouble;
char variable[500];
// Read and save an integer, double, and String to your variables.
scanf("%d", &secondInt);
scanf("%lf", &justDouble);
scanf("%[^ \n]", variable);
// Print the sum of both integer variables on a new line.
printf("%i\n ", i + secondInt);
// Print the sum of the double variables on a new line.
printf("%.1lf\n ", d + justDouble);
// Concatenate and print the String variables on a new line
printf("%s ", s);
printf("%s ", variable);
// The 's' variable above should be printed first.
return 0;
}
This should do the trick by using fgets() in general
#include <stdlib.h>
#include <stdio.h>
int main()
{
// variable to store the message
char msg[100];
// prompting the user to enter the message
printf("Pls enter a msg: ");
// using fgets() to retrieve a whole sentence from the user
fgets(msg, 100, stdin);
// printing the message to stdout
printf("%s", msg);
}
You can learn more about fgets here:
https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm
Let me know if anything is not clear so I can improve my answer
To work with scanf, you need to make sure that everything entered gets read.
In your example, you first expect an integer, and then a double. What does the user type to 'finish' entering the integer? Probably a <RET> (or a blank) - and now you need to scanf these too! Or they will 'clog' the input stream.
For example, your second scanf could be scanf(" %lf"... - note the blank before the % sign, it will read (and discard) any number of whitespace (which is <RET>, <TAB>, <space>).
scanf is very powerful, but needs a lot of detail understanding to be used correctly. Most people don't get it, and therefore claim "it's old and bad and shouldn't be used".
In professional software, it is generally avoided; not because it's not capable, but because the chance is too high that is used wrong, or that it is encountered by a developer that changes it and messes it up.

Eclipse IDE always ask for input first regardless of actual code order

I'm currently studying beginner C programming using Eclipse IDE.
Below is a very basic program that suppose to print out a line first, take in some input, and then print out a second line. But my Eclipse always ask me for the input first before printing out the two lines together afterwards.
This is extremely frustrating because it is something so simple and doesn't work. What am I doing wrong?
#include <stdio.h>
int main(void){
int aNumber;
printf("first line\n");
scanf("%d", &aNumber);
printf("second line with %d", aNumber);
return 0;
}
Had the same problem once.
Solved it by flushing stdout.
#include <stdio.h>
int main(void){
int aNumber;
printf("first line\n");
fflush(stdout); // Prints stdout content
scanf("%d", &aNumber);
printf("second line with %d", aNumber);
return 0;
}

why doesn'y this code work?

This question is regarding the SPOJ tutorial problem :
Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.
I want to run the program without if -else statements but the program doesn't work. Can someone please tell me what am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
while (i != 42)
{
putchar(i);
i = getchar();
}
exit(0);
}
Here is a quick and dirty way of getting the result that you want.
Since you want the user to enter number (which is composed of multiple characters), you will want to use scanf() instead of getchar().
scanf() documentation : http://www.cplusplus.com/reference/cstdio/scanf/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0;
while (i != 42)
{
scanf("%d", &i);
printf("You have entered : %d\n", i);
}
printf("You have successfuly entered 42!\n");
exit(0);
}
Hope this helps.

How to count loop execution with local variables and user defined function? C Language

Please help as I am a beginner here. In this program, main asks the user to enter an integer. If the integer is positive, the loop will continue, and ask to enter another integer. The user-defined function named "myfunction" displays in output how many times the do-while loop has been called. I used variable loopCount++ to increment this count each time. This program works correctly, but I have just been given a new challenge: do this without using any global variables. Turns out I have one global var... int loopCount. I have no idea how to accomplish this using local variables but I sure do want to know. PLEASE help!
#include <stdio.h>
#include <stdlib.h>
int loopCount;
int main()
{
int number;
do{
printf("Enter a number: ");
scanf("%i", &number);
loopCount++;
myfunction();
}while(number > 0);
exit(0);
}
int myfunction(){
printf("The loop has been called %d times\n\n", loopCount);
}
Just make loopCount a variable that is local to main, and pass it to myfunction as an argument, like so:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
int loopCount = 1;
do{
printf("Enter a number: ");
scanf("%i", &number);
myfunction(loopCount++);
}while(number > 0);
exit(0);
}
int myfunction(count){
printf("The loop has been called %d times\n\n", count);
}
It's also probably worth pointing out that this code could use better error handling - if you're not sure what I mean, try typing a letter at the prompt (instead of a number).

Why doesn't my output show up until the program exits?

I have a simple program from a C programming book, and it's supposed to ask for two integers and then add them together and show the sum. I'm able to enter the two numbers, but the output doesn't show up until the very end of the program.
#include <stdlib.h>
#include <stdio.h>
/* Addition Program*/
main()
{
int integer1, integer2, sum;
printf("Enter first integer\n");
scanf("%d", &integer1);
printf("Enter second integer\n");
scanf("%d", &integer2);
sum = integer1 + integer2;
printf("Sum is %d\n", sum);
return 0;
}
The output looks like this:
2
6
Enter first integer
Enter second integer
Sum is 8
Any help would be greatly appreciated, thanks!
It is possible that the output is not being flushed automatically. You can add fflush(stdout) after each printf() and see if that helps.
Which environment are you using to build and run this program?
Further to the above, printf will only automatically flush it's buffer if it reaches a newline.
If you are running on windows, a newline is \r\n instead of \n.
Alternatively you can do:
fflush(stdout);
Another alternative is to turn off buffering by calling:
setbuf(stdout, NULL);
EDIT:
Just found this similar(but not the same) question:
Why does printf not flush after the call unless a newline is in the format string?

Resources