compiles but doesn't execute (c++ runtime error)

Hi my code is for a program that divides two inputed numbers with an error handler. I am trying to create this using modules because I am learning about that. I've gotten the program to work and and just tried to refactor it with a few modules. I got it to compile but I get a c++ runtime error about the application wanting to terminate in an unusual way.

heres the main.cpp

#include <iostream>

#include "PromptModule.h"
#include "ErrorHandlingModule.h"

using namespace std;

float GetDividend(void)
{
float Dividend = 0;

cout << "Dividend: ";
cin >> Dividend;

return Dividend;
}

float GetDivisor(void)
{
float Divisor = 1;

cout << "Divisor: ";
cin >> Divisor;

return Divisor;
}

float Divide(const float theDividend, const float theDivisor)
{
return (theDividend/theDivisor);
}

int main(int argc, char* argv[])
{
SAMSErrorHandling::Initialize();

int ReturnCode = 0;

try
{
float Dividend = GetDividend();
float Divisor = GetDivisor();

cout << Divide(Dividend, Divisor) << endl;
}
catch(...)
{
ReturnCode = SAMSErrorHandling::HandleNotANumberError();
};

SAMSPrompt::PauseForUserAcknowledgement();
return ReturnCode;
}


I am running windows 7 here are the other files

header for a prompt that just prompts user so you can see the program result


#ifndef PromptModuleH
#define PromptModuleH

namespace SAMSPrompt
{
void PauseForUserAcknowledgement(void);
}
#endif


implementation of promt module

#include <iostream>

#include "PromptModule.h"

namespace SAMSPrompt
{
using namespace std;

void PauseForUserAcknowledgement(void){
char StopCharacter;
cout << endl << "Press a key and \"Enter\": ";
cin >> StopCharacter;
}
}


an error catching header followed by the implementation

#ifndef ErrorHandlingModuleH
#define ErrorHandlingModuleH

namespace SAMSErrorHandling
{
void Initialize(void);
int HandleNotANumberError(void);
}



#endif


implementation

#include <iostream>
#include "ErrorHandlingModule.h"

namespace SAMSErrorHandling
{
using namespace std;

void Initialize(void)
{
cin.exceptions(cin.failbit);
}

int HandleNotANumberError(void)
{
cerr << "Inpout error - not a number?" << endl;
cin.clear();
char BadInput[5];
cin >> BadInput;
return 1;
}
}
"Runtime error" is not very descriptive.
Run it with the debugger to see which line it happens in and which kind of runtime error it is.
Or maybe tell us what inputs you used? I typed in one and one after I finally got all your code copy and pasted into the correct files and linked them all together. Please use the forum code tags in the future, and describe your problem more clearly. Your program ran just fine on my computer with no modification.
Please use code tags when posting code ... no need to bold the code. It is the <> button under Format when posting.

It's a handy little function!

Edit: Oops, Kevin got there first!
Last edited on
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Main.cpp
#include <iostream>
#include "Helpers.h"
using namespace std;

float GetDividend() {
    float Dividend = 0;
    cout << "Dividend: ";
    cin >> Dividend;
    return Dividend;
}

float GetDivisor() {
    float Divisor = 1;
    cout << "Divisor: ";
    cin >> Divisor;
    return Divisor;
}

float Divide(const float theDividend, const float theDivisor) {
    return (theDividend / theDivisor);
}

int main() {
    Helpers::Initialize();
    int ReturnCode = 0;

    try {
        float Dividend = GetDividend();
        float Divisor = GetDivisor();
        cout << "\n" << Divide(Dividend, Divisor) << "\n";
    } catch (...) {
        ReturnCode = Helpers::HandleNotANumberError();
    };

    Helpers::Pause();
    return ReturnCode;
}

Helpers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Helpers.cpp
#include <iostream>
#include "Helpers.h"

namespace Helpers {
    using namespace std;

    void Pause() {
        cout << "\nPress 'Enter' to continue ... ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin.get();
    }

    void Initialize() { cin.exceptions(cin.failbit); }

    int HandleNotANumberError() {
        cerr << "Input error - not a number?" << endl;
        cin.clear();
        char BadInput[5];
        cin >> BadInput;
        return 1;
    }
}

Helpers.h
1
2
3
4
5
6
7
8
9
10
11
12
// Helpers.h
#ifndef HelpersH
#define HelpersH

namespace Helpers {
    void Pause();
    void Initialize();
    int HandleNotANumberError();
}

#endif


This works for me.
BTW, I simplified it a bit...
Here are some more details....I had trouble getting it to compile for a while and was getting a message that said something about an undefined reference to WinMain @ 16 but I think I fixed this because my compiler or IDE was set to Win App mode.

Now it compiles but when I run the .exe I get a Windows dialog that says "this application has requested to terminate the Runtime in an unusual way...Please contact the appilcations support team." The window is titled Microsoft Visual C++ Runtime Library..

I am new to using modules. I have everything in the same folder at the time of compile and the .exe gets outputted to a separate folder...is this fine...also do I have to put the module files anywhere else or just wherever im working.

I am using gcc 3.4.5 and my IDE is Sally a simple c++ IDE although I don't know why the iDE would matter.

I am using the same gcc version and using Eclipse... But I compile my stuff using the command-line, which is as follows:
g++ -O3 -Wall -o<output name.exe> <input files>


You will need to link against the 'implementation' source files... i.e.
g++ -O3 -Wall -oDivide.exe main.cpp prompt.cpp errorHandling.cpp
or whatever your src files are. I just put both the ErrorHandling and Prompt into a Helpers.cpp and Helpers.h, for simplicity, but the end result is the same either way.

The version I posted should work perfectly. Use this command to compile it, with the files I posted.

g++ -O3 -Wall -o Divide.exe Main.cpp Helpers.cpp
Last edited on
Wow that worked... Except the i entered it without the "-03" as it returned an unrecognized command. Thanks so much...what is the -03 for by the way?
It's a capital o not a zero. O vs 0, hard to tell.

It is an optimization option. I use that one for release programs, for debug you want to use -O0. The options go from 0 to 3 and s, for size. I beleive 3 is the optimal between speed and size of the program... -O2 gets the fastest program, while -Os gets the smallest. I think so at least. You can read on the man page for g++. I think the default optimization is -O0... i could be wrong.

-Wall is WarnOnAll, basically gives more verbosity by not ignoring certain things and treating them as warnings.
Last edited on
Topic archived. No new replies allowed.