My First C++ Program - It works, but I have another issue...

Below I will paste my first program, but, my issue I think is with DEV C++. We downloaded DEV C++ onto the computer at school, and it runs perfectly, outputs the .cpp and .exe files perfectly, and runs everything perfectly.

I get home and run the .exe and it starts like normal, but halfway through it crashes. I can compile and run it through the DEV C++ program and it works perfectly. I can even edit things within my code and the .exe will reflect my changes, but still crash at the same spot... I have no errors or warnings...

It crashes on line 36-38 (or maybe 40-42) after "Enter number of trees to plant: ";

But like I said, when I compile and run it through DEV C++ it works perfectly...

Below is my code:

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double length;
    double width;
    double area;
    double mowing_cost;
    double application_cost;
    double trees_cost;
    double total;

    int applications;
    int trees;

    cout << "Tom & Jerry's Lawn Care" << endl << endl;

    cout << "Enter length in yards: ";
    cin >> length;
    cout << "Enter width in yards: ";
	cin >> width;
    cout << endl;

    area = length *  width;

    cout << "Length = " << length << endl;
    cout << "Width = " << width << endl;
    cout << "Area = " << area << endl << endl;

    cout << "Enter number of fertilizing applications: ";
    cin >> applications;

    cout << "Enter number of trees to plant: ";
    cin >> trees;
    cout << endl;

    mowing_cost = area / 5000 * 35.00;
    application_cost = 30.00 * applications;
    trees_cost = 50.00 * trees;

    cout << fixed << showpoint << setprecision (2);
    cout << "Mowing Cost = $" << mowing_cost << endl;
    cout << "Application Cost = $" << application_cost << endl;
    cout << "Trees Cost = $" << trees_cost << endl << endl;

    total = mowing_cost + application_cost + trees_cost;

    cout << "Total = $" << total << endl;

    return 0;
}
Last edited on
There doesn't appear to be anything wrong with your program, except that it does not handle erroneous non-numeric input.

What is the compiler that you are using at home?

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
40
41
42
43
44
#include <iostream>
#include <iomanip>

int main()
{
    std::cout << "Tom & Jerry's Lawn Care\n\n" ;

    double length ;
    double width ;
    std::cout << "Enter length in yards: ";
    std::cin >> length;
    std::cout << "Enter width in yards: ";
    std::cin >> width;

    if( length <= 0 || width <= 0 )
    {
        std::cout << "invalid input\n" ;
        return 1 ;
    }

    const double area = length * width;

    std::cout << "\nLength = " << length
              << "\nWidth = " << width
              << "\nArea = " << area << "\n\n" ;

    int applications ;
    int trees ;
    std::cout << "Enter number of fertilizing applications: ";
    std::cin >> applications;
    std::cout << "Enter number of trees to plant: ";
    std::cin >> trees;

    const double mowing_cost = area / 5000 * 35.00;
    const double application_cost = 30.00 * applications;
    const double trees_cost = 50.00 * trees;
    const double total = mowing_cost + application_cost + trees_cost ;

    std::cout << std::fixed << std::showpoint << std::setprecision(2)
              << "\nMowing Cost = $" << mowing_cost
              << "\nApplication Cost = $" << application_cost
              << "\nTrees Cost = $" << trees_cost
              << "\n\nTotal = $" << total << '\n';
}
I am using the compiler built in to DEV C++, found here:

https://sourceforge.net/projects/orwelldevcpp/

I just noticed your corrections to my code, I appreciate that, tomorrow I will examine it and try to understand it all... then implement it... but for now, I'm tired, I'll check back in the morning.

Haha, I figured out how to display code properly, thanks for that also.
Last edited on
Hope this helps, with any future problems that may arise with DevC++
http://www.cplusplus.com/articles/36vU7k9E/

Switching to Visual Studio's made my life a little easier, and prettier.
Ok, thanks MoreUmph, I'll check it out. After reading that link, I completely understand.
Last edited on
MoreUmph wrote:
Hope this helps, with any future problems that may arise with DevC++
http://www.cplusplus.com/articles/36vU7k9E/


From the linked article:
This article does not apply to Orwell Dev-C++


Generally the Orwell version is reasonable.

The only issue now is that it, along with code::blocks, may come bundled with TDM-GCC. In itself, that is pretty good as a start, but it hasn't been updated since 2015 - which at the moment in some respects is almost a lifetime. C++ is undergoing various revision stages, and if you want to use the very latest features then you'd need to separately install a newer compiler - which is probably more than one should be asking of a beginner.
Generally the Orwell version is reasonable.

The only issue now is that it, along with code::blocks, may come bundled with TDM-GCC. In itself, that is pretty good as a start, but it hasn't been updated since 2015 - which at the moment in some respects is almost a lifetime. C++ is undergoing various revision stages, and if you want to use the very latest features then you'd need to separately install a newer compiler - which is probably more than one should be asking of a beginner.


The things I am looking for:

1. I would prefer it to be all in one (comes with a "good" compiler).

2. I would prefer it to be light - as my home PC is older, some programs make it run hard (and slow).

3. I would love for it to be a standalone so I can use it at home and at different computers at school straight from my flash drive.

4. I want to find one program that accomplishes all of the specifications above so I can get used to it and only work on that one program - as a beginner I don't want to worry about the different nuances from different programs.
It looks like Visual Studio can be run off a USB drive although it takes some work, you'd have to look into that one. Eclipse on the other hand can apparently be run on USB, I've never used it so you'd also have to look into that one, but I've heard good things about Eclipse so its worth taking a look I'd say.
Thanks MoreUmph, I will check those out.
Going back to my original question, why did my .exe from my compiled program work at school, but not when I got back home?

I added (below) right before return 0; at the end and that fixed the problem.

system ("pause");

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double length;
    double width;
    double area;
    double mowing_cost;
    double application_cost;
    double trees_cost;
    double total;

    int applications;
    int trees;

    cout << "Tom & Jerry's Lawn Care" << endl << endl;

    cout << "Enter length in yards: ";
    cin >> length;
    cout << "Enter width in yards: ";
	cin >> width;
    cout << endl;

    area = length *  width;

    cout << "Length = " << length << endl;
    cout << "Width = " << width << endl;
    cout << "Area = " << area << endl << endl;

    cout << "Enter number of fertilizing applications: ";
    cin >> applications;

    cout << "Enter number of trees to plant: ";
    cin >> trees;
    cout << endl;

    mowing_cost = area / 5000 * 35.00;
    application_cost = 30.00 * applications;
    trees_cost = 50.00 * trees;

	cout << fixed << showpoint << setprecision (2);
    cout << "Mowing Cost = $" << mowing_cost << endl;
    cout << "Application Cost = $" << application_cost << endl;
    cout << "Trees Cost = $" << trees_cost << endl << endl;

    total = mowing_cost + application_cost + trees_cost;

    cout << "Total = $" << total << endl;

system ("pause");
    return 0;
}
If the two computers run on different operating systems it won't be compatible on both. I'm not completely sure what all else could've happened as I only code on two devices but I'm guessing its just a compatibility issue. Unfortunately C++ isn't nearly as compatible as languages such as java, where you could run it on probably any machine you could think of.
I added (below) right before return 0; at the end and that fixed the problem.
system ("pause");

It doesn’t fix anything: it keeps the console window opened until you press the ENTER key.
A console program can execute and close in a blink, so fast that you could not see it.
But you could have noticed that there are two posts pinned in the first page of the beginners forum, one of which is this:
http://www.cplusplus.com/forum/beginner/1988/
Just have a look at it.
Topic archived. No new replies allowed.