CPP printf giving no output on VS2012 - c++

I am using Microsoft Visual Studio 2012 Professional. I have a simple CPP code:
#include "stdio.h"
int main () {
int a = 0, b = 0, c = 0;
printf("\nEnter two numbers:\n");
scanf("%d %d", &a, &b);
c = a+b;
printf("\nc = %d", c);
return 0;
}
The problem I am facing is that the first printf statement is getting printed but the second one is not. The code is exiting without printing anything. However when I debug using breakpoints, the value of c is getting stored in it.

I'll take a wild guess and say you run this from your IDE.
It does print the output. But the program then ends. And it ends so fast, you cannot see the output. If you want your program to wait for you to see it, you will need to find a way to make it wait. For example with another scanf line.
As there is no standard, environment agnostic way to say "Press any key to continue", have a look here for some alternatives.

Add \n to the end of the last printf. It is implementation-defined whether the last line of output to stdout actually gets flushed , if it doesn't end in a newline.

When you use
scanf("%d %d", &a, &b);
then you should give 2 inputs separated by space or enter
scanf("%d,%d", &a, &b);
then you should give 2 input separated by comma(,).
and at last use
printf("Press any key to continue.."); getche(); instead of return;

Related

Problem when executing a simple C program in Eclipse IDE

I'm trying to run a simple C program in Eclipse and I am new to Eclipse.
When running the command I see output of the program for a very brief moment in the console, but instead for the program to wait for input, output disappears and and an empty console is shown instead. Is this a problem of the program or something related to the settings of the Eclipse IDE?
Edit: On the console menu there is a terminate button and when I pres it the output of the program shows but it doesn't wait for input and the program is terminated. Any help?
Image:
Here is the code
//The input is a series of numbers and the number X. Find
//how many times the number X is met in the series
#include<stdio.h>
int main(){
int m[20];
int n, X, cnt=0;
printf("Insert maximum length of the series (n<=20)\n");
scanf("n= %d",&n);
printf("Insert X the number you are looking for\n");
scanf("X= %d",&X);
while(n<1||n>20){
printf("Bad input, n should be between 1 and 20\n");
scanf("n= %d",&n);
}
for(int i = 0 ; i < 20 ; i++){
scanf(" %d", &m[i]);
if(m[i]==X)cnt++;
}
printf("X is found %d times\n", cnt);
return 0;
}
and here is the empty console:
Edit: I've tried rewriting the code using cout and cin but then the compiler in Eclipse complains on the line #include<iostream.h> saying that there is an unresolved inclusion. Sounds like a separate question to me.
The answer is here: click
Basically the output of the standard input output is buffered and setvbuf(stdout, NULL, _IONBF, 0); solves the problem.

Using Recursion To Print Output

#include<stdio.h>
#include<conio.h>
void main()
{
static int i=5;
clrscr();
if(--i)
{
printf("%d",i);
main();
}
getch();
}
When I am running this code, it is not giving any output and when I am removing getch() then after running when I am switching to output screen then it is showing ouput . Why?
I am using Turbo C++.
You would have seen output with debuger if you break on each iteration, but since you are obviously just executing your program what you see is the result of the last clrscr();. Since i is 0 you're not getting into the if, where the printing happens and you get right at the getch(). Hope that answers your question.
The printf function buffers output internally until either
the buffer is full
it's asked to print a newline (\n)
stdout is flushed with fflush()
You are not doing either of 2 or 3 and the buffer is surely bigger than 5 integers.
You probably want
printf("%d\n",i);
If you want the ints to be printed without a new line,
printf("%d ",i);
fflush(stdout);

Unable to find errors - Turbo C

I am making a simple program to add two numbers. I've done everything correct but somehow it's showing an error.
I am using TurboC for windows7 64bit (downloaded from filezilla)... I've also used devcpp but but there its showing error in using void main()... Why this is so? Why it is not working?
Also, can anybody suggest some good software for programming console-based for projects C, C++, etc.?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num1=0,num2=0;
//printing hello world
//printf("Hello World!");
printf("Enter number 1 : ");
scanf("%d",num1);
printf("Enter number 2 : ");
scanf("%d",num2);
int num3 = num1+num2;
printf("The sum of %d and %d is %d",num1,num2,num3);
getch();
}
The problem is with your scanf(). When accepting values, you must add the & before the variable. The unary & returns the address of the variable next to it, and scanf() then stores the value at that address. But note that you do not need to use & in printf() unless you actually want to print the address. In short, change your scanf() 's to
scanf("%d",&num1);
and
scanf("%d",&num2);
Here's your working code code
#include<stdio.h>
int main()
{
int num1=0,num2=0;
//printing hello world
//printf("Hello World!");
printf("Enter number 1 : ");
scanf("%d",&num1); // see here
printf("Enter number 2 : ");
scanf("%d",&num2); // and here
int num3 = num1+num2;
printf("The sum of %d and %d is %d",num1,num2,num3);
}
The error with void main() is that it is no longer accepted. On older versions like TurboC, you can use void main(), but the standard clearly states that we should not use void for main(), instead you should use int main().
Read this for reference
What should main() return in C and C++?
And, don't use <conio.h>. It's not supported in the standard. If you want to clear the screen, add the header file <stdlib.h> and use system("cls");
Regarding a replacement for getch(), you can just use getchar(). ( although in some programs, you will have to use two or more getchar()'s )
There's one thing you should know, and that is that both TurboC and DevC++ are outdated.
You should probably get Code Blocks.
You get the "Declaration not allowed here" error because prior to C99 ( your IDE TurboC runs on an older version than C99 ) , variables had to be declared at the beginning of a block. You can use Declaration not allowed here error in C as reference
I'm only focusing on the error:
printf("Enter number 1 : ");
scanf("%d",&num1); //use & for input
printf("Enter number 2 : ");
scanf("%d",&num2); //use & for input
When you use scanf(), you must provide the address of the variable you write to using &.
scanf("%d",num1);
should become:
scanf("%d",&num1); //add the & to refer to the address

C/C++ : Can I keep the cursor in the current line after pressing ENTER?

I would like to ask if there's any way to keep the cursor in the current line after pressing ENTER !!
for example ...
#include<stdio.h>
int main()
{
int d=0;
printf("Enter a number : ");
scanf("%d",&d);
if(d%2)printf(" is a Odd number\n");
else printf(" is a Even number\n");
return 0;
}
An example of output :
Enter a number : 10
is a Even number
... but what I need is something like that :
Enter a number : 10 is a Even number
I want to put "is a Even number" (or " is a Odd number") next to the number entered by user
The user is pressing enter, and this is being echoed back and starting a new line.
In order to avoid this, you'll need to turn off echo (and then read and echo individual characters except for newline). This is system-dependent, for example on Linux you can put the tty into raw/uncooked mode.
You may find a library such as GNU readline that does most of the work for you.
The simple answer is "you can't". There is no standard C++ functions to control this behaviour, or to read data without hitting enter at the end (in fact, the data hasn't really been "entered" until you hit enter, so the program won't see the data).
You can use non-standard functionality, such as additional libraries, such as a "curses" library or system dependent code, but we would then have to produce the code to read characters one at a time and merge it together using code that you write.
I would suggest that you use the "repeat the input in the output", and simply do something like this:
printf("%d is", d);
if (d%2)
printf("an odd number\n");
else
printf("an even number\n");
Set up raw keyboard mode and disable canonical mode.
That's almost, how linux manages not to show password chars in terminal.
Termio struct is the thing you should google for.
One link is :
http://asm.sourceforge.net/articles/rawkb.html
The Constants of the assembly are also available for a syscall ioctl.
This trick may help, if you have a vt100-style terminal: cursor movements.
\033 is ESC, ESC + [ + A is cursor up, ESC + [ + C is cursor right
int main()
{
int d=0;
printf("Enter a number : ");
fflush(stdout);
scanf("%d",&d);
printf("\033[A\033[18C%d is a an %s number\n", d, d%2 ? "odd" : "even");
return 0;
}
not with printf and scanf... have you tried with getc() and ungetc(char) ?
OR, try to play with printf("%c", (char)8); if I remember correctly that's a backspace
otherwise, you'll probably have to use some output lib such as ncurses

The output window closes after entering the input in C

I wrote program in C , but after compiling and running it the output console does not stay on after I enter anything. My program requires to enter distance and time.
My code is here:
#include <stdio.h>
int spd(int x , int y);
int main() {
int x,y;
printf("enter the distance first then time in their SI units :\n");
scanf("%d",&x);
scanf("%d",&y);
printf("the speed required is ",spd(x,y));
getch();
return 0;
}
int spd(int x , int y) {
return x/y;
}
It seems that the getch feeds on your input-buffer. When you enter your value in scanf(), the '\n' remains.
Try to put flush(stdin) after your scanf() or for now put getch () twice.
If you want to run a console program, you should run it from a console. You have written a console program -- run it from a console.
Also, you need to either flush standard out or write a newline before the getch call. Otherwise, you're waiting for a keypress before you've actually written anything.
Try including this header file : -
#include conio.h