Computer Hope

Software => Computer programming => Topic started by: SLiV on November 28, 2009, 04:06:44 AM

Title: [C++] Executable does nothing when running simple iostream program
Post by: SLiV on November 28, 2009, 04:06:44 AM
I was trying to make a program that simulates a cardgame, but I run on to something at the very start. I wasn't able to figure out what I or it was doing wrong, since it doesn't run it at all. No error, no bleeps, no blank screens. Nothing. Just a flash.

Code: [Select]
#include <iostream.h>

int main()
{
    int a;
    char clr[5] = {'o','h','d','s','c'};
    char nmr[10] = {'0','A','J','Q','K','@','#','7','8','9'};
     
    cout << "The Jack of Spades (3;2) is abbreviated: " << clr[3] << "" << nmr[2];
     
       
    cout << "How Awesome...";
   
    cin.get();
    return 0;
}
This is not an extract, this is in fact the whole code. That's what bothers me about it.
I know I could use "ohdsc" instead, but that shouldn't mather, right? Also, clr[5] has 5 variables, clr[0]-clr[4], so that should work as well.
Maybe it thinks I want to define nmr[5] instead of the whole nmr range, but I don't know how else to define it. I almost copied it from the book I'm working with, so what is happening?

I'm running it with Dev-C++ and Vista.
Title: Re: [C++] Executable does nothing when running simple iostream programme
Post by: TheUnixGuy on November 29, 2009, 03:45:39 AM
Hello,

Try making:
Code: [Select]
#include <iostream.h>
...

to
Code: [Select]
#include <iostream>
...

NOTE: We write 'programs' not 'programmes'.
Title: Re: [C++] Executable does nothing when running simple iostream programme
Post by: SLiV on November 29, 2009, 11:44:41 AM
I tried that, because that was what Dev-C++ suggested. Unfortunatly it immediatly errored that the function 'cout' was first declared etc.
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: BC_Programmer on November 29, 2009, 11:56:08 AM
this works for me- in Visual Studio 2008:

Code: [Select]

#include <iostream>
#include <ctime>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

    int a;
    char clr[5] = {'o','h','d','s','c'};
    char nmr[10] = {'0','A','J','Q','K','@','#','7','8','9'};
     
    cout << "The Jack of Spades (3;2) is abbreviated: " << clr[3] << "" << nmr[2];
     
       
    cout << "How Awesome...";
   
    cin.get();
    return 0;

}


just ignore the tmain part there- I'm sure you can use main() as you are doing instead. I just went with the template it gave me.
you might want to add a endl to the end of the first cout call, though; otherwise, the entire program writes to one line.

Code: [Select]
    cout << "The Jack of Spades (3;2) is abbreviated: " << clr[3] << "" << nmr[2] << endl;
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: SLiV on November 30, 2009, 01:59:30 AM
So you're suggesting that I'd add #include <ctime> and using namespace std;?

And yeah, I should probably add an endline, but that's not the real issue here.
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: BC_Programmer on November 30, 2009, 02:14:29 AM
oh.. ignore the ctime include too. the namespace directive couldn't hurt, though.
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: SLiV on December 01, 2009, 02:13:51 PM
What do you mean 'couldn't hurt'?

Then what did you add?

Just the endline? Because that's obviously not the error.

I checked them both, with no improvements whatsoever. I'm not sure you read the topic start carefully. Or at all.
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: BC_Programmer on December 01, 2009, 02:54:00 PM
What do you mean 'couldn't hurt'?

Then what did you add?

Just the endline? Because that's obviously not the error.

I checked them both, with no improvements whatsoever. I'm not sure you read the topic start carefully. Or at all.

Quote
this works for me- in Visual Studio 2008:


the code I posted works fine for me; I can see the output just fine. And notice I actually didn't add the endl; it was merely a suggestion. I pasted your code into VS2008 and tweaked it until it compiled and ran and then posted the code I had at that point. the <ctime> was from the program template I loaded; I simply forgot to remove it.

I just downloaded Dev C++; copy-pasted your code- and it compiled and ran fine. a few warning about deprecation or something, but it ran just fine.

EDIT:

the console window output was:

Code: [Select]
The Jack of Spades (3;2) is abbreviated: sJHow Awesome...
and the window stayed visible until I closed it; this was a fresh install of Dev C++, so I hadn't fiddled with any options.
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: Salmon Trout on December 01, 2009, 03:42:40 PM
Quote
I'm not sure you read the topic start carefully. Or at all.

Check the attitude at the door, please.
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: TheUnixGuy on December 02, 2009, 02:18:44 AM
Hello,

In case BC_Programmer's method doesn't work out, try running the program from the command line or whatever MS Vista calls it. Just CD into that directory and type its name as if it were a command.
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: SLiV on December 02, 2009, 09:35:58 AM
@Salmon Trout / BC_Programmer
Sorry, I didn't mean to be rude, as it is me who is asking your help; I'm just getting irritated by a lot of things not working.

@Topic
My point being, BC_Programmer has no method, and shouldn't. Because apparently the code contains no bugs at all. But then why isn't my pc able to run it?

About the command line, I'm not that good. But creating an exe and running that doesn't work either, and running it only partially won't do because Dev-C++ says there is no Debugging Information. Clicking on 'create' doesn't seem to improve anything.

So probably either my DevC++ is causing it, or my pc. The problem remaining, what should I do about it?
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: Salmon Trout on December 02, 2009, 09:44:29 AM
Quote
it doesn't run it at all. No error, no bleeps, no blank screens. Nothing. Just a flash.

Have you tried running the exe by typing its name at an already open command prompt? (i.e. not from Windows Explorer by clicking in its icon) At least that way you might see some informative messages.

Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: TheUnixGuy on December 02, 2009, 09:48:10 AM
Hello,


About the command line, I'm not that good.

If you're using MS Vista, click on Start and type "cmd" and press ENTER. [1] The command prompt will appear. Copy your program into D: Drive (using My Computer) and rename it to "newprog1" (without  quotes).  Type "D:" into the command prompt and press ENTER. Type in "newprog1.exe" and press ENTER. Your program should run and display correctly.

[1]: http://www.computerhope.com/issues/chdos.htm#02

NOTE: I recommend learning the command line before diving into programming.
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: SLiV on December 02, 2009, 01:50:41 PM
Just running the .exe in the current folder did nothing either; the D: drive is a CD/DVD drive, so I don't think that'll work either.
Maybe you mean C:? But entering the command "C:" (without quotes) was not recognised as a command.
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: TheUnixGuy on December 03, 2009, 01:38:02 AM
Hello,

Just running the .exe in the current folder did nothing either

Through command line?  I would try another compiler.
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: robin1232 on December 04, 2009, 02:36:36 PM
its deprecated, you have to say "using namespace STD"
like this:

#include <iostream>
using namespace std;
void main()
{
  cout << "Hello World!"
}

you're probably following too old tutorials
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: aiko on December 18, 2009, 01:50:24 PM
if you use vham.exe you can make this automatic.
1.just write your source
2.press edit>executable>
3.write your name of your game/program
4.save it typ in dos IE00 CHK <name of program>
5. if your program has no error it is succesfully else the program will fail like a flash
Title: Re: [C++] Executable does nothing when running simple iostream program
Post by: 2x3i5x on December 18, 2009, 02:12:14 PM
I think this is what you wanted? Your original code does run after doing the compiling. I compiled with M$ visual C++ 2008 express edition (on Vista 32 bit home edition)

1.Open start menu
2. In the search box, type CMD and press Enter when CMD is found
3. open up the folder where your EXE exists if not already there. Then type in name of program.
4. I did the above three steps and I see following (and I just saved the file as "jack.exe in a folder named C++")

NOTE: don't just double click EXE to attempt running it, actually manually open CMD then run it. Else, the program might just exit too quickly for you to see actually the result. This might be why you think program is not working, or you only see a quick flash and then nothing again IF you are sure that you have followed the appropriate steps to compiling your code via your compiler

Here is screenshot of result:

(http://img692.imageshack.us/img692/5416/testzu.jpg)


Here is code I compiled:
Code: [Select]
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    char clr[5] = {'o','h','d','s','c'};
    char nmr[10] = {'0','A','J','Q','K','@','#','7','8','9'};
     
    cout << "The Jack of Spades (3;2) is abbreviated: " << clr[3] << "" << nmr[2];
     
       
    cout << "How Awesome...";
   
    cin.get();
    return 0;
}

I got rid of int a, because it is an unused variable as is. Don't know what you wanted use a to do. And I find M$ Visual C++ express edition (the free edition of their professional software) to be relatively good to use.  :)