0

Trying to run this program, but whenever it runs is quickly opens up and closes right away without allowing me to interact with it. What am I doing wrong to make it open and close?

Below is my program, and I'm sure I'm doing something wrong.

Thanks for your help!

Driver.cpp

#include<string>
#include<algorithm>
#include<iostream>

using namespace std;

int main()
{
    char Anagram();
}

Anagram.cpp

#include<string>
#include<algorithm>
#include<iostream>
#include "Anagram.h"

using namespace std;

char Anagram()
{
string FirstAnagram, SecondAnagram;
char keep_going;

do
{
    cout << "Enter word one: ";
    cin >> FirstAnagram;
    cout << "Enter word two: ";
    cin >> SecondAnagram;

    sort(FirstAnagram.begin(), FirstAnagram.end());
    sort(SecondAnagram.begin(), SecondAnagram.end());

    if (FirstAnagram == SecondAnagram)
    {
        cout << "They are anagrams of each other.";
    }
    else
    {
        cout << "They are not anagrams of each other.";
    }
    cout << "\n\nTry another?";
    cin >> keep_going;
}

while (keep_going == 'y');

return 0;
}

Anagram.h

char Anagram();
Mario
  • 83
  • 2
  • 3
  • 10
  • look up how to correctly call a function in C++ and compare it with the way you call `char Anagram()`. – yngccc Jan 14 '14 at 01:35
  • You might also want to reconsider why you are having `Anagram` return `char` instead of `void` since the return value is not useful (it's always 0 no matter what) and is not used anyway. Unless this is a stub for future error codes or something. – JBentley Jan 14 '14 at 01:50

2 Answers2

3

Try this:

#include<string>
#include<algorithm>
#include<iostream>
#include "Anagram.h"  // add this

using namespace std;

int main()
{
    char ch = Anagram();  // call the function like this
}
bcf
  • 2,104
  • 1
  • 24
  • 43
  • Doesn't work, it says "press any key to continue..." then when you hit anything, the program closes. – Mario Jan 14 '14 at 01:34
  • It worked without the system("pause"); Thanks for the help! :) – Mario Jan 14 '14 at 01:46
  • I'll change my downvote to an upvote if you remove the `system("pause");` - it doesn't address the OP's issue (which was confusingly worded) but it is also a [bad idea in general](http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong) for many reasons. – JBentley Jan 14 '14 at 01:47
  • @JBentley Since it wasn't the cause of OP's problems, fair enough. There seems to be some debate on how to stop the console from closing using Visual, what do you rec? Besides using emacs :) – bcf Jan 14 '14 at 01:54
  • @David Depends on what you want to achieve. In the majority of cases, you don't want your program to pause prior to termination, since command-line tools don't usually behave that way. If for some reason you do then ThomasMatthews' answer works. However, often the goal (as you said) is to view the output in Visual Studio. In which case in the project properties go to `Configuration Properties -> Linker -> System -> SubSystem` and make sure it is set to `Console (/SUBSYSTEM:CONSOLE)`. Having this option at `Not set` is usually the cause of the console disappearing in my experience. – JBentley Jan 14 '14 at 11:57
0

Try adding this to the end of your main function:

std::cout << "Press Enter to close application.\n";
std::cin.ignore(10000, '\n');
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • The question is badly worded - the OP's issue isn't that he wants his console app to pause before terminating. It's that his function (which is meant to get user input from `cin`) is never called in `main` because of a syntax error. – JBentley Jan 14 '14 at 01:45
  • @Thomas Matthews: I do not understand why sometimes Visual Studio gives 'Press any key to continue...' and sometimes we need to take care of it. – Pranit Kothari Jan 14 '14 at 02:01
  • @JBentley: It *isn't* a syntax error to declare a function. That's why compilation succeeded. – Ben Voigt Jan 14 '14 at 02:02
  • @BenVoigt Well ok, a logic error then. Either way, the OP doesn't need a way to pause his program. – JBentley Jan 14 '14 at 11:18
  • @pranitkothari In the project properties go to `Configuration Properties -> Linker -> System -> SubSystem` and make sure it is set to `Console (/SUBSYSTEM:CONSOLE)`. Having this option at `Not set` is usually the cause of the console disappearing in my experience. – JBentley Jan 14 '14 at 11:58