0

:) I'm having trouble with c++ (I'm a beginner lol) I need help with my release .exe I run my program in Code::Blocks and everything works fine... But when I run the standalone .exe it doesn't work. :( my code contains "cmath" so i think that might be causing the problem? my program works like this: it asks the user their car's weight and 1/4 mile trap speed. It runs these numbers through an equation: //Formula = HP = [(MPH/234)^3] * Weight The user can enter the weight, but when the trap speed is entered the program just closes :\ (By program i mean the standalone release .exe). everything works fine in C::B. If anyone of you pros could help that would be wonderful. Thanks -Evan... code:

#include <iostream>
#include <cmath>

using namespace std;


//Formula = HP = [(MPH/234)^3] * Weight

int main()
{
    cout << "Source for formula:" << endl <<"http://beatersbanter.blogspot.com/2011/08/how-to-calculate-horsepower-from.html" << endl;
    cout << "Let's calculate your horsepower!" << endl << "I just need some car stats first..." << endl;

    float whp = 0;
    float weight = 0;
    float mph = 0;
    float mphdivided = 0;

    cout << "Enter weight (pounds):" << endl;
    cin >> weight;

    cout << "Enter quarter mile trap speed (mph):" << endl;
    cin >> mph;

    mphdivided = mph / 234;
    whp = pow(mphdivided, 3) * weight;

    cout << "Great!" << endl << "Your wheel horsepower rating is: " << whp << endl;
    cout << "Thank you for your time!" << endl;

    return 0;
}
Evan732
  • 3
  • 1
  • What you mean by 'doesn't work' ? CMD shows up for a short while ? If so, add system("pause") before return 0; – Scony Nov 11 '13 at 00:30
  • I wouldn't recommend `system("pause")` as it only works on Windows: http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong – yizzlez Nov 11 '13 at 00:32
  • lol-beginner uses windows for sure. – Scony Nov 11 '13 at 00:33
  • Scony, thank you for the response. It opens and i can enter my car weight, but when i enter my 1/4 mile trap speed it just closes the exe. I think it takes the input, but something goes wrong when it tries to do the "pow" function to the variables. Can you please help with that. Thanks. :) – Evan732 Nov 11 '13 at 03:03

1 Answers1

3

It just opens and instantly closes right? Add getchar() or cin >> variable. Where variable is a variable of your choice:

char c;
cin >> c; //add this or getchar();
yizzlez
  • 8,757
  • 4
  • 29
  • 44
  • Another alternative: `cin.ignore(100000, '\n');` – Thomas Matthews Nov 11 '13 at 01:45
  • @Evan732 add what I told you to add at the end of `main()`, just before `return 0` – yizzlez Nov 11 '13 at 22:11
  • @awesomeyi Thank you for the response, but that is not the problem. The problem is that the user can input the variable "weight" but when they type in "mph" and press enter, the application just closes. Can you please try to run the program on your computer to see if it works for you? It works in code blocks when you press "F9" but when you try the .exe it does what i said above. Please help. ty in advance lol. – Evan732 Nov 11 '13 at 23:24
  • @Evan732 Yes I know what you mean, when you enter "mph" the application just closes- but have you tried adding the two lines of code I gave you? When I added them, the application did not close right away when you enter "mph" – yizzlez Nov 11 '13 at 23:58
  • @awesomeyi lol your a genius. I'm such a dork lol I forgot that it was showing everything super fast and then closing lol. sorry for being such a noob hehehehe. I got so desperate that i made a video a few minutes ago: http://youtu.be/tsqp5tfk9E0 Every thing's all good now because of you thank you sooo much I've been stuck here for days. Thanks again. – Evan732 Nov 12 '13 at 00:04