I am using Code::Blocks for programming and yeah i am a beginner but everytime i write a program it pauses in IDE but does not pause while executing directly.
What is the possible reason ? Can anyone explain me ?
My code goes as follows :
#include <stdio.h>
void main()
{
float length,breadth,Area;
printf("Enter the value of length and breadth \n\n");
scanf("%f %f",&length,&breadth);
printf("You entered length=%f and breadth=%f \n\n",length,breadth);
Area= length * breadth;
printf("The area of the rectangle is %f\n\n",Area);
return 0;
}
You have to tell your program to wait for input at the end otherwise it will execute, do exactly what you wrote in your code and exit. The "good" way would be to execute it from a terminal (cmd if you are on windows)
#include <stdio.h>
int main()
{
float length,breadth,Area;
printf("Enter the value of length and breadth \n\n");
scanf("%f %f",&length,&breadth);
getchar(); // catch the \n from stdin
printf("You entered length=%f and breadth=%f \n\n",length,breadth);
Area= length * breadth;
printf("The area of the rectangle is %f\n\n",Area);
getchar(); // wait for a key
return 0;
}
Why do you need a getchar() after your scanf()?
When you enter your numbers you finish it with a press of enter. Let's see what you are reading: a float whitespaces and another float. The \n is not consumed by scanf(), but left in the input buffer (stdin). The next time you use a function that reads from stdin, the first sign this function sees is a \n (Enter). To remove this \n from the input buffer you have to call getchar() after scanf() which reads 1 character from the input buffer. I'm sure you will encounter this behaviour more often in future.
As a really, REALLY bad practise, you can just use a getch call to stop the excecution (or any function that generates a small pause, getch is not a standard function).
A program is not supposed to pause after execution and this is a feature added by the IDE. If you want execution to pause and wait for some input you should instruct it to do so. For instance if you are on windows you can add a line:
system("pause");
Right before return 0;. This is not advisable, but may help you for debugging in some cases. Also the standard requires that your main function is int, not void. So you better get used to writing int main instead of void main.
You need to use getch(); at the end of program (Before return statement)
Getch holds down the output screen.
Related
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,c;
printf("enter the 2 numbers: ");
scanf("%d %d",&a,&b);
c=a+b;
printf("the sum is : %d ",c);
return(0);
}
this is a simple program to add 2 numbers.
my program would let me input the value..but it would not print the sum ,nor would it print the next line.
it would run till the scanf() and as i press enter, it would jst exit the program.
can you please tell me whats wrong. I am a beginner programmer...
There are two things you should think of here.
End printouts with a newline character, because stdout is often line buffered. Do printf("the sum is : %d \n",c); instead. Or call fflush(stdout); expliticly after the printout. This will ensure everything gets printed.
Add some input code in the end. Like an extra scanf("%d", &a); This is basically a small hack to prevent the window from closing before you can see the final output. Another alternative is to add sleep(3); to sleep for 3 seconds. A third alternative here is to see if there are some settings that controls the closing of the window in your IDE.
Your program works correctly, but it exits right after printing the output, giving you no time to look at it.
Consider adding some input before return(0);, such as 2 getchar(); calls. You need 2, because the first character read will be the \n that you typed after the numbers.
I am programming in C and i have a problem when i run a program in the cmd terminal. here is the code i use:
#include <stdio.h>
int main() {
int num;
printf("enter a number: ");
scanf("%i\n", &num);
for(int n = 1; n < num + 1; n++){
printf("%i\n", n);
}
return 0;
}
Generally, everything works like it should, exept for one thing.
when I enter a number, nothing happens. there is no output, until I write anything and press Enter, and only then the number appear.
this is a screenshot of what it looks like.
here is enter the number (and press enter) but nothing happens: http://prntscr.com/deum9a
and this is how it looks like after i entered something random nad all the numbers popped up: http://prntscr.com/deumyn
if anyone knows how to fix this, please tell me (:
Remove the \n from scanf()
scanf("%i", &num);
When you have a whitespace character in the format string, scanf() will ignore any number of whtiespaces you input and thus the ENTER you do doesn't terminate the input reading. Basically, you'll be forced to input a non whitespace character again in order complete the scanf() call.
Generally, scanf() is considered bad for input reading. So, considering using fgets() and parsing the input using sscanf().
See: Why does everyone say not to use scanf? What should I use instead?
I'm trying to write a program that lets the user insert integer values into an array until they enter a specific key to stop, say ENTER, or X.
int array_numb[100];
char quit = 'x';
printf("Enter as many values into the array as you want, pressing x will end the loop\n");
while(1==1){
int i = 0;
scanf("%i",&array_numb[i]);
i++;
array_numb[i] = quit;
}
I know it's wrong but this is my thought process. Any ideas? thanks for the help
Enter as many values into the array as you want while having int array_numb[100];, at some point you will be in trouble. What if the user enters more than 100 numbers before entring the sentinel value? You'll be overruning the allocated memory, isn't it?
You should check the return value of scanf() to ensure proper input. A mismatch in the supplied format specifier and the type of input will cause scanf() to fail. Check the man page for details.
You need to have a break statement inside while(1) loop to break out. Otherwise, you need to have a conditional statement in the controlling expression for while() loop to terminate the loop.
Note: IMHO, the best way to stop scanning user input is to comapre the return value of scanf() against EOF, which can be produced by pressing CTRL+D on linux and CTRL+Z in windows.
You can check this answer for your reference.
The same thing can be achieved as shown below:
scanf() can fail and check the return value. When a character is scanned scanf() fails
int i=0;
printf("Enter as many values into the array as you want, pressing x will end the loop\n");
while(i<100 && scanf("%d",&array_numb[i])==1)
{
i++;
}
scanf, with %i, will fail to scan a number when invalid data, such as a character is inserted and will return 0(in your case) if it fails.
Just check the return value of scanf. If it is zero, use
if(getchar()=='x') //or if(getchar()==quit)
break;
Note: Clear/flush the standard input stream(stdin) using
int c;
while((c=getchar())!='\n' && c!=EOF);
after the loop as well as after the break; .
You could learn something and use signals:
http://man7.org/linux/man-pages/man7/signal.7.html
Register SIGINT, override it, let it check which key was pressed, then let it change a variable. In your loop check if this variable is 0 or 1.
Signals have top priority, signal code will be executed no matter the loop.
Example: http://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code/
It's overkill for this case but a fun thing to know. Signals are the way how an OS is managing events, like process kills, keyboard input etc. Be warned, how to use this is OS specific and has not much to do with C itself.
So basically I have a a program in C, whose point is to look organized.
I'd like my program's cursor not to move after I confirm my Scanf statement.
Heres example part of my program and its output:
#include <stdio.h>
int main()
{
char input[10];
int voltage=220;
printf("What do you want to know\n"); //no need to correct me on this part please
scanf("%s", input);
if (strcmp("voltage",input)==0)
printf(" = %d\n", voltage);
/* Imagine other string comparisons (resistance, current) here*/
return 0;
}
So this program for input "voltage" would output something like this:
voltage //what I just inputed via scanf
= 220 // \n because I pressed enter after inputing.
What I wanted is:
voltage = 220
I dont know Is there reverse \n function that I dont know about.
There is no standard way in C to read input without pressing Enter. You will have to go for platform specific functions.
There are lots of questions here in StackOverflow that cover this, such as Capture characters from standard input without waiting for enter to be pressed
Sorry for asking such a simple question, I'm still learning C and going through the basics first.
I'm creating a character counting program and yet when I execute the program and try to input "h" for example and then press enter a new line appears and nothing is outputted onto that line?
Code:
#include <stdio.h>
/* Copy input and count characters 2nd version */
main() {
double cc;
for(cc = 0; getchar() != EOF; ++cc);
printf("%.0f\n", cc);
}
Once you have finished entering characters, you have to signal the end of input stream by pressing Ctrl-D. Otherwise your program will continue waiting for more input.
P.S. Why are you using a double variable for the counter? An integer type would be more appropriate.
Maybe (I'm not sure what exactly do you want) you have an extra ; after the for(), which mean an empty statement. So your program will run the empty statement (in other words, do nothing) until the end of input (if the input is terminal, you may need a CTRL+D), and then print (once) the number of characters.
If you want your program to print the counter after every char in the input, remove that ;, so the printf will be in the loop.
Include this line at the end you will get output:
return 0;