Why printf not giving output in a windows form C++ application - winforms

I created a simple windows form application C++ project in MS VS 2010. Then I intent to print to console:
// FtoC.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
#include <stdio.h>
#include<conio.h>
using namespace FtoC;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
printf (" printing to console");
// Enabling Windows XP visual effects before any controls are created
//Application::EnableVisualStyles();
//Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
//Application::Run(gcnew Form1());
getch();
return 0;
}
As you can see I have commented everything except my printf statement.
It compiles without any error, but no output is coming. Why it is so?
I modified the code as shown below:
#include "stdafx.h"
#include <Wincon.h>
#include "Form1.h"
#include <stdio.h>
#include<conio.h>
using namespace FtoC;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
BOOL chk = AllocConsole();
if(chk)
{
freopen("CONOUT$", "w", stdout);
printf (" printing to console");
}
else
{
throw new SomeException();
}
getch();
return 0;
}
But now I am getting lots of errors in wingdi.h file, such as:
Error 270 error C1003: error count exceeds 100; stopping compilation C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\wingdi.h 750 1 FtoC
Error 159 error C2065: 'MAX_PATH' : undeclared identifier C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\wingdi.h 683 1 FtoC
What went wrong ??

It will not show you any output because WinForm does not have a console attached to them. Now if you really want to use a console
BOOL chk = AllocConsole();
if(chk)
{
freopen("CONOUT$", "w", stdout);
printf (" printing to console");
}
else
{
throw new SomeException();
}
Ref AllocConsole()

Related

"Hello world" program not running

I typed this program on code blocks but it is showing error on int main line
Here's the program
#include <stdio.h>
int main()
{
printf("Hello");
return 0;
}
The message return is " multiple definition of main"
The sample code is correct. It might be an IDE configuration error. Use gcc to compile your code on Linux and on Windows you can install MinGW and execute from a command window.

drawing operation was attempted when there was no current window error while using getch() in C

In C there is a error I'm facing
that when ever I use getch() command in my code and run it either in codeblocks or the .exe file after everything is done and when the control goes to getch() command it shows an error pop up window saying
Drawing operation was attempted when there was no current window.
#include <stdio.h>
#include <conio.h>
int main() {
int a;
scanf("%d", &a);
printf("%d", a);
getch();
return(0);
}
Using Code::Blocks 16.01.
Use _getch() instead of getch():
#include<conio.h>
_getch();
Source: https://learn.microsoft.com/cpp/c-runtime-library/reference/getch
I researched, what I understood was that the command getch is deprecated and the command you can use to replace it is the _getch.
There is more information at this link:
https://learn.microsoft.com/cpp/c-runtime-library/reference/getch

creating a getchar function with read( , , ) error

I wrote read this code from the K&R book. But i compile it i get an error:
gcc: error: getchar.c: No such file or directory
gcc: fatal error: no input files
compilation terminated.
Code:
#include <sys/syscall.h>
#include <stdio.h>
int getchar(void)
{
char c;
return (read(0, &c,1) == 1) ? (unsigned char) c : EOF ;
}
main()
{
printf("\nEnter the character you want to getchar: \n");
getchar();
return 0;
}
The error message is telling you that there is no file called getchar.c in the current directory to compile. That might mean that you accidentally called it getchar.c or getchar.c instead, so you should look carefully at what you called it -- it might look like it is correct, but have extra invisible characters in it.
The easiest fix is probably to open the file in your editor, and then "save as" and type in a name that has no invisible characters in it.
If you're just compiling it in the Windows environment
try
#include <sys/syscall.h>
change to
#include <io.h>

c program graphics error

I have implemented the boundary fill algorithm in C language with the following code:--
/* WAP to fill the polygon using boundary fill 4 connected algo */
#include "stdio.h"
#include "conio.h"
#include "graphics.h"
#include "dos.h"
void main()
{
int gd = DETECT, gm;
clrscr();
detectgraph(&gd, &gm);
initgraph(&gd, &gm , "C:\\TC\\BGI");
rectangle(60,60,500,500);
boundary_fill(65,65,4,15);
getch();
closegraph();
}
boundary_fill(int x, int y, int fclr, int bclr)
{
if(getpixel(x,y)!= bclr && getpixel(x,y)!= fclr)
{
putpixel(x,y,fclr);
boundary_fill(x+1,y,fclr,bclr);
boundary_fill(x-1,y,fclr,bclr);
boundary_fill(x,y+1,fclr,bclr);
boundary_fill(x,y-1,fclr,bclr);
}
}
when i compile it no error come. But When i run the program the window closes and i get the following error:--
C:\TC\BIN\TC.EXE
The NTVDM CPU has encounterer an illegal instruction.. . . . . .
PLease help
stop using turboC. run your 16 bit programs(such as TurboC/C++) with DosBox instead. NTVDM error occurs because of 32bit COMMAND-PROMPT trying to run a 16 Bit Program.

How to use System(const char*) in TC++

Today , When i coding, met a question..my Code as follow:
#include<stdlib.h>
void main()
{
system("dir");
getch();
}
The question : The user Screen is nothing..Why ? where is my result?
If you want the output when using system, at least into something you can read in your application, you need to pipe the output:
system("dir > /tmp/output.txt");
FILE *f = fopen("/tmp/output.txt", "r");
char text[1024]; // max sizeof of 1 kb, any more and I'd consider using `malloc()` instead.
fread(text, 1, 1024, f);
printf("%s\n", text);
fclose(f);
There are some problems in your program, at least one of which has already been mentioned.
void main() should be int main(void).
As I recall, the Windows/DOS getch function is declared in <conio.h>; you should have a #include directive for it. Be aware that both <conio.h> and getch are non-standard.
Since main returns int, you should return an int result.
But none of these problems explain the problem you're seeing.
With these changes:
#include <stdlib.h>
#include <conio.h>
int main(void)
{
system("dir");
getch();
return 0;
}
This should work; it should show a directory listing of whatever directory your program runs in (which is determined by TC; I don't know the details).
It's possible that the program is running in an empty directory, which means the dir command wouldn't show any files, but it should still produce some output.
Try commenting out the system() call and adding a printf call (note the added #include <stdio.h>):
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
printf("Hello, world\n");
getch();
return 0;
}
This should open a console window, print "Hello, world" in it, and wait for you to type Enter.
If you still don't see any output (either no console window, or a console window with nothing in it), then you have a problem that's not related to the system() call. Most likely the problem has to do with the way you're using Turbo C (I presume that's what "TC" stands for).
The main function in every C program is supposed to return an int you are returning void
Change void to int:
#include<stdlib.h>
int main()
{
system("dir");
getch();
}
When I tested, the dir command ran in my console and printed to standard out.
May be he is the running the program directly in the Turbo C IDE and hence his output is not visible. If he runs the program directly from cmd line it works. I remember you need to run Alt - F5 or some other combination to see the output window in Turbo C++

Resources