Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy Console project problem. Won't display output

Status
Not open for further replies.

jwarmuth

IS-IT--Management
Sep 22, 2001
151
CA
Thanks in advance for the help. I'm brand spankin' new to C++ (I'm coding .Net too btw), and I'm having a problem with the first tutorial I've done! Heheh, 'cept this isn't my fault, even the book example project file does the same thing.

So here's the code:
************************Begin Code**************************

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

__gc class Animal
{
public:
int legs;
void SetName(String *name)
{ strName = strName->Copy(name); }
String* GetName() { return strName; }
private:
String *strName;
};

int _tmain()
{
Animal *cat, *dog;
cat = new Animal;
dog = new Animal;

cat->SetName("Cat");
cat->legs = 4;
dog->SetName("Dog");
dog->legs = 4;

Console::WriteLine("Animal 1");
Console::Write("Name: ");
Console::WriteLine(cat->GetName());
Console::Write("Legs: ");
Console::WriteLine(cat->legs);
Console::WriteLine();
Console::WriteLine("Animal 2");
Console::Write("Name: ");
Console::WriteLine(dog->GetName());
Console::Write("Legs: ");
Console::WriteLine(dog->legs);
Console::WriteLine();

return 0;
}


***********************End Program****************************

That compiles and executes okay except that nothing shows up in the command window. Just a blinking cursor and then the window dissappears after a few seconds.

I went through the output logs and nothing jumped out at me. Here they are in case they may help.

****************Output log***********************

'Animals.exe': Loaded 'D:\Visual C++ Projects\Book Examples\Chapter 02\Animals\Debug\Animals.exe', Symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\system32\mscoree.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\system32\SHLWAPI.DLL', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\system32\user32.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorwks.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\msvcr71.dll', Symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\fusion.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\system32\shell32.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.10.0_x-ww_f7fb5805\comctl32.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\system32\comctl32.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorlib.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\assembly\NativeImages1_v1.1.4322\mscorlib\1.0.5000.0__b77a5c561934e089_2dc5a000\mscorlib.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\diasymreader.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\system32\ole32.dll', No symbols loaded.
'DefaultDomain': Loaded 'c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorsn.dll', No symbols loaded.
'Animals': Loaded 'd:\visual c++ projects\book examples\chapter 02\animals\debug\Animals.exe', Symbols loaded.
'Animals.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorjit.dll', No symbols loaded.
The thread 'Win32 Thread' (0x43c) has exited with code 0 (0x0).
The program '[260] Animals.exe' has exited with code 0 (0x0).
The program '[260] Animals.exe: Native' has exited with code 0 (0x0).

**********************End Log******************


Any help is appreciated. :)

Jeff Warmuth
MCSE, CNE
ICQ 129152989
 
The program probably ends before you have chance to see any output.

There are several things you can do
1. Put some kind of "user input" command right at the end of the program to force it to wait.

2. Or this, which introduces you to something else you'll need to get to grips with.
- Put the cursor on the return 0; line at the end of the program
- Press F9 (a red circle appears - this is a breakpoint)

Now run the program. After the usual things you see, you should then see focus return to .NET, and a yellow arrow pointing at the return 0; statement.
- Now click on the program icon in the taskbar to see your output.
- Return to .NET, and put the cursor on say 'dog', right-click and select "quick watch". Use the tree view +/- tool to examine the variable in depth.
- When you're done, just press F5 to let the program run to completion, or use the menu option Debug->Stop Debugging.

--
 
Figured it out.

The program needs to be compiled without debugging (Shift+F5).

LOL~!

Jeff Warmuth
MCSE, CNE
ICQ 129152989
 
jwarmuth, congratulations on finding a solution to your problem. I would suggest you look at Salem's proposed solutions, as it turned out that he (or she) was right about your problem. In the future (or probably more like now), you are going to want to debug your program. Your solution doesn't allow you to do that, and it also doesn't work if you just double click on the executable file that is created by the build.
 
It does work, thanks Salem. :)

Strange, when I run the program without debugging, it displays the results immediately upon execution. However when debugging is enabled with a breakpoint after 'return 0;', it does work, but takes about 5 seconds for the text to appear. Then it shows the text and pauses (as instructed).

Does anyone know why?

Jeff Warmuth
MCSE, CNE
ICQ 129152989
 
The time taken to load the debugger, load all the symbols, load all the code....

vs.

Just run the code.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top