Jump to content

[Help] Compiles but doesn't run.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
Shockwave

Shockwave

    Newbie

  • Members
  • Pip
  • 4 posts
Hey, I am quite new to coding previosly dabbling in QBasic for a little while. I quickly got bored of its limitations so picked up a copy of 'Begining C++ Game Programming - Michael Dawson', and started to go through it. I followed the tutorials but typed out the code to myself, often personalizing it to make sure I understand. Everything was plain sailing untill I ran into a snag during Lost Fortune.

//Lost Fortune

//Personalized Adventure Game


#include <iostream>

#include <string>


using std::cout;

using std::cin;

using std::endl;

using std::string;


int main()

{

    const int GOLD_PIECES = 900;

    int adventurers, killed, survivors;

    string leader;

    

    //Information

    cout << "Welcome to the Lost Fortune\n\n";

    cout << "Please enter the following information\n";

    

    cout << "Enter a number: ";

    cin >> adventurers;

    

    cout << "Enter a number smaller than" << adventurers << ": ";

    cin >> killed;

    

    survivors = adventurers - killed;

    

    cout << "Enter your last name: ";

    cin >> leader;

    

    //Story

    

    cout << "\nA brave group of " << adventurers << " set out on a quest ";

    cout << "-- to find the treasures of Smaug.";

    cout << "The group was led by the charming hobbit " << leader << ".\n";

    

    cout << "\nUnfortunately, Smaug was awake and fought the band of hobbits and dwarves.";

    cout << "Luckily, the group had a fine leader, " << leader << ".";

    cout << "And so Smaug was defeated, but at cost...";

    

    cout << "Of the party, " << killed << " were slain, leaving just " << survivors << ".";

    cout << "The remainder of the band shared the treasure of " << GOLD_PIECES << " gold pieces.";

    cout << "Of course, " << leader << " took the remaing " << (GOLD_PIECES % survivors);

    cout << ", to keep things fair.";

    

    return 0;

}

    

I have this in Dev-C++ and it compiles fine, but the second it runs I get the typical Vista fail message.

Quote

[Name].exe has stopped working. Windows will now check for a solution.

Does anybody know what I am doing wrong?

#2
Macoder

Macoder

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
I'm not exactly sure, but try replacing this:
cout << "Please enter the following information\n";
With this:
cout << "Please enter the following information" << endl;
I think that's how you're supposed to use cout.

#3
Shockwave

Shockwave

    Newbie

  • Members
  • Pip
  • 4 posts

Macoder said:

I'm not exactly sure, but try replacing this:
cout << "Please enter the following information\n";
With this:
cout << "Please enter the following information" << endl;
I think that's how you're supposed to use cout.

No, tested it to be sure, but endl stands for end line, ie the next cout will be on another line.

The \n was explaines in the book, but I don't have it with me and cannot remember why I used it.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It runs fine on XP for me. What was your input?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
In Linux, your code compiles and runs fine. There is nothing wrong with it. It could be a bug in Dev-C++.
Here is the program running on my computer:
Posted Image
You should fix the spelling and grammatical errors though.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#6
Shockwave

Shockwave

    Newbie

  • Members
  • Pip
  • 4 posts
Well, I was gonna compile it in another program and whilst it was running I continued to fiddle with Dev-C++, then somehow by clicking a orange shieldy thingy and changing a selection from GUI to Console, I fixed my problem.

Just wondering... What did I do?

Also, thanks for letting me know it wasn't my code at fault.

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Yeah, that'll do it. I've never used Dev-C++, but there's a pretty big difference between GUI and console programs. Console programs run on the command line and only have text output; GUI programs have their own windows, buttons, text boxes, and so on.
If I had to guess, I'd say that Dev-C++ was expecting you to be running a GUI, so it probably loaded the wrong debugging libraries or something and that's why it didn't run. Again, educated guess.
sudo rm -rf /

#8
Shockwave

Shockwave

    Newbie

  • Members
  • Pip
  • 4 posts
Ah, makes sense. Cheers, I'll make sure to check these sorta things next time. Anyway, it's all fixed now, added a while(1) loop and fixed all grammatical errors, thanks everyone.

#9
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
Just as a note, you SHOULD use endl instead of '\n', simply because it knows your machine and your locale, the line ending may not be \n on your system (\r\n on windows, \n or \r or \r\n on *NIX and \r on Mac OS)

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
I thought '\n' was automatically translated.
sudo rm -rf /

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
\n works fine for me on both Windows and Linux.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#12
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
I learned something new today. The standard defines '\n' as anything that'll get you to the begining of the next line so long as you're in a hosted environment. The majority of my work in C/C++ is freestanding, where '\n' is a literal 0x10. The only real difference is std::endl will flush the output immediately.