HELP! Mining Program

Hello, I am trying to write a program which analyses mine ore production. The pogram recieves the number of mines to enter, the number of each mine, the year opened and year closed and each mine's total ore production. It outputs the lifespans of each mine, the average yearly ore production, the total ore production of all mines and the number of the mine with the longest lifespan.

I have got the program to work but I need a way for the program to check for errors when entering the mine lifespan in. I have used a function called compare() which uses yrOpened and yrClosed as an if statement and returns the value 0, 1 or -1 if the yrOpened is equal, greater than or less than yrClosed. The main program then puts in the value of compare() into the result variable. It is then meant to show an error message for if the value returned is 0 or 1 (equal or greater than) using an if statement. I have tried different times using different test data and the messga won't show what I want it to show. I think there is something wrong with the if statement, not the function.

To sum up:
If yrOpened is equal to yrClosed, return the value 0 and ask the user to re-enter the years.
If yrOpened is greater than yrClosed (meaning it closed before it opened), return 1 and ask the user to re-enter the years.
If yrOpened is less than yrClosed (the only other option, meaning it closed after opened), return -1 and continue with the program.

Here is the full code below:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <conio.h>
#include <stdio.h>
#include <windows.h>
 
int main()
{
 
/* variables and objects declared here*/
int result, totalMines, mineNumber, yearOpened, yearClosed, totalProduction, count, mineLifespan;
int totalMineProduction=0, storedTotal=0, longestLifespan=0, longestLifespanNo=0;
float yearlyProduction;
int compare(int yrOpened, int yrClosed);
 
system("cls");
 
/* User enters the number of mines */
printf("\nPlease enter the number of mines: ");
scanf("%d",&totalMines);
							 
/*For loop for entering data for each mine*/
 
for (count=1; count <= totalMines; count++, totalMineProduction, storedTotal, longestLifespan, longestLifespanNo)
{
printf("\n Please enter the number of the mine: ", count);
scanf("%d",&mineNumber);
printf("\n Please enter the year mine %d was opened: ", mineNumber);
scanf("%d",&yearOpened);
printf("\n Please enter the year mine %d was closed: ", mineNumber);
scanf("%d",&yearClosed);
 
result = compare(yearOpened, yearClosed);
 
while (result=1)
{
        printf("\n The year closed is before the year opened. Please re-enter the year opened and year closed for the mine");
        printf("\n Please enter the year mine %d was opened: ", mineNumber);
        scanf("%d",&yearOpened);
        printf("\n Please enter the year mine %d was closed: ", mineNumber);
        scanf("%d",&yearClosed);
if (result=0)
{
        printf("\n The year closed the same as the year opened. Please re-enter the year opened and year closed for the mine");
        printf("\n Please enter the year mine %d was opened: ", mineNumber);
        scanf("%d",&yearOpened);
        printf("\n Please enter the year mine %d was closed: ", mineNumber);
        scanf("%d",&yearClosed);
}
}
printf("\n Please enter the total ore production of mine %d in kg: ", mineNumber);
scanf("%d",&totalProduction);
 
/*Calculations for mine lifespans and production*/
mineLifespan = yearClosed - yearOpened;
printf("\n The lifespan of mine %d is %d", mineNumber, mineLifespan);
yearlyProduction = (float) totalProduction / mineLifespan;
printf("\n The average yearly ore production for mine %d is %0.2f", mineNumber, yearlyProduction);
 
/*Calculations for total combined mine production*/
storedTotal = totalProduction + totalMineProduction;
totalMineProduction = storedTotal;
 
/*If statement for adjusting longest lifespan*/
if(mineLifespan > longestLifespan)
{
longestLifespan = mineLifespan;
longestLifespanNo = mineNumber;
}
}
/*Printfs for showing total mine production and the mine number of the longest mine lifespan*/
printf("\n The total combined mine production is %d", totalMineProduction);
printf("\n The number of the mine with the longest lifespan is %d", longestLifespanNo);
							 	
 
getchar();
getchar();
}
 
int compare(int yrOpened, int yrClosed)
{
        if(yrOpened = yrClosed)
        {
                return(0);
        }
        else if (yrOpened > yrClosed)
        {
                return(1);
        }
        else
        {
                return(-1);
        }
}
Last edited on by twicker
closed account (j3Rz8vqX)
Use the equality operator:
 
if(yrOpened == yrClosed)

Unless your intentions were assignment..

Have fun.
closed account (j3Rz8vqX)
Don't mean to be bugging you, but this printouts looks strange...
1
2
3
printf("\n Please enter the number of the mine: ", count); 
//count never prints, if your intentions are to print it, add %d somewhere in the string prior to count.
//Edit: Or remove the comma and count variable. 

Also:
1
2
3
4
5
6
7
8
9
10
11
while (result=1) //assignment operation? or possibly:
while (result == 1)//? Test if result is 1

//same applies to:
if (result=0) //? or:
if (result==0) // Test if result is 0

//And this:
for (count=1; count <= totalMines; count++, totalMineProduction, storedTotal, longestLifespan, longestLifespanNo)
//can possibly be this?:
for (count=1; count <= totalMines; count++)


Other than that no more warnings.

Have fun.
Last edited on
I have done all of the suggested amendments but when I run the program and put in 2 identical years it still accepts it and continues the program. And another thing, when I put in 1900, 1900 and total ore production 1 it comes up with the average yearly ore production as 1.#J

It also accepts 1900 and 2000, which is supposed to.

And I tried 1900 and 1800 and it says, like it is supposed to, "The year closed is before the year opened..." etc., like it should.

Any suggestions? Is it the function, the for or while loop, or something else?

Here is the program again with the amended 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <conio.h>
#include <stdio.h>
#include <windows.h>

int main()
{

/* variables and objects declared here*/
int result, totalMines, mineNumber, yearOpened, yearClosed, totalProduction, count, mineLifespan;
int totalMineProduction=0, storedTotal=0, longestLifespan=0, longestLifespanNo=0;
float yearlyProduction;
int compare(int yrOpened, int yrClosed);

system("cls");

/* User enters the number of mines */
printf("\nPlease enter the number of mines: ");
scanf("%d",&totalMines);
							 
/*For loop for entering data for each mine*/

for (count=1; count <= totalMines; count++)
{
printf("\n Please enter the number of the mine: ");
scanf("%d",&mineNumber);
printf("\n Please enter the year mine %d was opened: ", mineNumber);
scanf("%d",&yearOpened);
printf("\n Please enter the year mine %d was closed: ", mineNumber);
scanf("%d",&yearClosed);

/*Calling the function*/
result = compare(yearOpened, yearClosed);

while (result == 1)
{
        printf("\n The year closed is before the year opened. Please re-enter the year opened and year closed for the mine");
        printf("\n Please enter the year mine %d was opened: ", mineNumber);
        scanf("%d",&yearOpened);
        printf("\n Please enter the year mine %d was closed: ", mineNumber);
        scanf("%d",&yearClosed);
if (result==0)
{
        printf("\n The year closed the same as the year opened. Please re-enter the year opened and year closed for the mine");
        printf("\n Please enter the year mine %d was opened: ", mineNumber);
        scanf("%d",&yearOpened);
        printf("\n Please enter the year mine %d was closed: ", mineNumber);
        scanf("%d",&yearClosed);
}
}
printf("\n Please enter the total ore production of mine %d in kg: ", mineNumber);
scanf("%d",&totalProduction);

/*Calculations for mine lifespans and production*/
mineLifespan = yearClosed - yearOpened;
printf("\n The lifespan of mine %d is %d", mineNumber, mineLifespan);
yearlyProduction = (float) totalProduction / mineLifespan;
printf("\n The average yearly ore production for mine %d is %0.2f", mineNumber, yearlyProduction);

/*Calculations for total combined mine production*/
storedTotal = totalProduction + totalMineProduction;
totalMineProduction = storedTotal;

/*If statement for adjusting longest lifespan*/
if(mineLifespan > longestLifespan)
{
longestLifespan = mineLifespan;
longestLifespanNo = mineNumber;
}
}
/*Printfs for showing total mine production and the mine number of the longest mine lifespan*/
printf("\n The total combined mine production is %d", totalMineProduction);
printf("\n The number of the mine with the longest lifespan is %d", longestLifespanNo);
							 	

getchar();
getchar();
}

int compare(int yrOpened, int yrClosed)
{
        if(yrOpened == yrClosed)
        {
                return(0);
        }
        else if (yrOpened > yrClosed)
        {
                return(1);
        }
        else
        {
                return(-1);
        }
}


And now it just keeps randomly say what message it wants to, I think it is something to do with the while loop?
Last edited on by twicker
Do I need to loop the function as well? It might explain the seemingly random outputs!
When I put 2 identical years in it continues with the program (that isn't good) but says the process exited with return value 0.

It says it returns 0 when using years 1900 and 2000 or 2000 and 1900 also.
UPDATE:
Now it seems to mostly work, except if I put in invalid years twice, it will allow it.
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#include <conio.h>
#include <stdio.h>
#include <windows.h>

int main()
{

/* variables and objects declared here*/
int result, totalMines, mineNumber, yearOpened, yearClosed, totalProduction, count, mineLifespan;
int totalMineProduction=0, storedTotal=0, longestLifespan=0, longestLifespanNo=0;
float yearlyProduction;
int compare(int yrOpened, int yrClosed);

system("cls");

/* User enters the number of mines */
printf("\nPlease enter the number of mines: ");
scanf("%d",&totalMines);
							 
/*For loop for entering data for each mine*/

for (count=1; count <= totalMines; count++)
{
printf("\n Please enter the number of the mine: ");
scanf("%d",&mineNumber);
printf("\n Please enter the year mine %d was opened: ", mineNumber);
scanf("%d",&yearOpened);
printf("\n Please enter the year mine %d was closed: ", mineNumber);
scanf("%d",&yearClosed);

/*Calling the function*/
result = compare(yearOpened, yearClosed);

if (result == 1)
{
        printf("\n The year closed is before the year opened. Please re-enter the year opened and year closed for the mine");
        printf("\n Please enter the year mine %d was opened: ", mineNumber);
        scanf("%d",&yearOpened);
        printf("\n Please enter the year mine %d was closed: ", mineNumber);
        scanf("%d",&yearClosed);
}
else if (result == 0)
{
        printf("\n The year closed the same as the year opened. Please re-enter the year opened and year closed for the mine");
        printf("\n Please enter the year mine %d was opened: ", mineNumber);
        scanf("%d",&yearOpened);
        printf("\n Please enter the year mine %d was closed: ", mineNumber);
        scanf("%d",&yearClosed);
}

printf("\n Please enter the total ore production of mine %d in kg: ", mineNumber);
scanf("%d",&totalProduction);

/*Calculations for mine lifespans and production*/
mineLifespan = yearClosed - yearOpened;
printf("\n The lifespan of mine %d is %d", mineNumber, mineLifespan);
yearlyProduction = (float) totalProduction / mineLifespan;
printf("\n The average yearly ore production for mine %d is %0.2f", mineNumber, yearlyProduction);

/*Calculations for total combined mine production*/
storedTotal = totalProduction + totalMineProduction;
totalMineProduction = storedTotal;

/*If statement for adjusting longest lifespan*/
if(mineLifespan > longestLifespan)
{
longestLifespan = mineLifespan;
longestLifespanNo = mineNumber;
}
}
/*Printfs for showing total mine production and the mine number of the longest mine lifespan*/
printf("\n The total combined mine production is %d", totalMineProduction);
printf("\n The number of the mine with the longest lifespan is %d", longestLifespanNo);
							 	

getchar();
getchar();
}

int compare(int yrOpened, int yrClosed)
{
        while(yrOpened < yrClosed)
        {
                return(-1);
        }
        if(yrOpened == yrClosed)
        {
                return(0);
        }
        else if(yrOpened > yrClosed)
        {
                return(1);
        }
}

Last edited on by twicker
closed account (j3Rz8vqX)
My apologies, it's been a long day.
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <conio.h>
#include <stdio.h>
#include <windows.h>

int main()
{

    /* variables and objects declared here*/
    int result, totalMines, mineNumber, yearOpened, yearClosed, totalProduction, count, mineLifespan;
    int totalMineProduction=0, storedTotal=0, longestLifespan=0, longestLifespanNo=0;
    float yearlyProduction;
    int compare(int yrOpened, int yrClosed);

    system("cls");

    /* User enters the number of mines */
    printf("\nPlease enter the number of mines: ");
    scanf("%d",&totalMines);

    /*For loop for entering data for each mine*/

    for (count=1; count <= totalMines; count++)
    {
        printf("\n Please enter the number of the mine: ");
        scanf("%d",&mineNumber);

        do
        {
            printf("\n Please enter the year mine %d was opened: ", mineNumber);
            scanf("%d",&yearOpened);
            printf("\n Please enter the year mine %d was closed: ", mineNumber);
            scanf("%d",&yearClosed);

            /*Calling the function*/
            result = compare(yearOpened, yearClosed);
            if (result == -1)
            {
                    printf("\n The year closed is before the year opened. Please re-enter the year opened and year closed for the mine");
            }
            else if (result == 0)
            {
                    printf("\n The year closed the same as the year opened. Please re-enter the year opened and year closed for the mine");
            }
        }while(result!=1);//Will repeat when true, hence we check the opposition (false).


        printf("\n Please enter the total ore production of mine %d in kg: ", mineNumber);
        scanf("%d",&totalProduction);

        /*Calculations for mine lifespans and production*/
        mineLifespan = yearClosed - yearOpened;
        printf("\n The lifespan of mine %d is %d", mineNumber, mineLifespan);
        yearlyProduction = (float) totalProduction / mineLifespan;
        printf("\n The average yearly ore production for mine %d is %0.2f", mineNumber, yearlyProduction);

        /*Calculations for total combined mine production*/
        storedTotal = totalProduction + totalMineProduction;
        totalMineProduction = storedTotal;

        /*If statement for adjusting longest lifespan*/
        if(mineLifespan > longestLifespan)
        {
            longestLifespan = mineLifespan;
            longestLifespanNo = mineNumber;
        }
    }
    /*Printfs for showing total mine production and the mine number of the longest mine lifespan*/
    printf("\n The total combined mine production is %d", totalMineProduction);
    printf("\n The number of the mine with the longest lifespan is %d", longestLifespanNo);


    getchar();
    getchar();
    return 0;
}

int compare(int yrOpened, int yrClosed)
{
    if(yrOpened == yrClosed)
    {
            return 0; //Test again, invalid
    }
    else if(yrOpened > yrClosed)
    {
            return -1;//Test again, invalid
    }
    return 1;//Everything, a-OK
}


The above code should solve the issues you had.

The methods I chose to resolve the issues were:
1
2
3
4
5
//line85: replaced while loop with if-statement

//line29-53: Embedded your 2-pompts, comparison function and conditional testing branch in do-while loop.

//line 40-43, 48-51: Removed, since do-while could repeat your prompts. 

I think you're all set.

I really ought to not answer the problem directly but... Ah, heck, I think you've done enough already.

Have fun.
Last edited on by twicker
Dput, you are a genius! It works! I did think I might have needed a different kind of loop, since it seemed that it didn't want to repeat the error checking for the years.
I also want to use an array for allowing the program to show 2 or more mine numbers with the longest lifespan(s). For example:

I enter 3 as the number of mines.

Mine 1 has a lifespan of 100 years (e.g. 1800 to 1900)

Mine 2 has a lifespan of 80 years (e.g. 1800 to 1880)

Mine 3 also has a lifespan of 100 years (e.g. 1760 to 1860)

How can I use an array to get the program say at the end:

"The number of the mine with the longest lifespan is 1 and 3"?
Last edited on
closed account (j3Rz8vqX)
I'm assuming the original conflicts were resolved.

Converting your current data(s) to array(s) is most definitely the correct approach.

I will let you attempt to do so first, then pitch in to help once you've got something working; Me programming it wouldn't benefit you in the long run.

Information on arrays:
http://www.cplusplus.com/doc/tutorial/arrays/

The modification will seem overwhelming but it really isn't.

This may be the process:

1) Change all of your primitive data (those that apply at least) into arrays of those same data; also change how they are access throughout the program as well.

2) Modify the behavior of some tests - the if_statement checking for the longest life span; a plural of longestLifespanNo may rise a bit of issue - flush the array of longestLifespanNo and insert the new longer life span - you may need a counter to keep track of the number of units in the longestLifespanNo array.

3) Possibly more - I may have overlooked something.

This modification may cause mass issues to arise, which is normal - do not let this discourage you.

If you're having issues with arrays I would recommend you design a separate program to first understand the behavior of arrays - you will have less errors when actually applying it to your working program if you do so.

Have fun.

Edit: Increased readability.
Last edited on
Sorry, I forgot to mention that I want the program with the array without the compare() function:
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
56
57
/*Header files*/
#include <conio.h>
#include <stdio.h>

void main(void)
{

	/* variables and objects declared here*/
	int totalMines, mineNumber, yearOpened, yearClosed, totalProduction, count, mineLifespan;
	int totalMineProduction=0, storedTotal=0, longestLifespan=0, longestLifespanNo=0;
	float yearlyProduction;

	/*Clears the screen*/							 
	clrscr();

	/* User enters the number of mines */
	printf("\nPlease enter the number of mines: ");
	scanf("%d",&totalMines);
							 
	/*For loop for entering data for each mine*/
							 
	for (count=1; count <= totalMines; count++)
	{
		printf("\n Please enter the number of the mine: ");
		scanf("%d",&mineNumber);
		printf("\n Please enter the year mine %d was opened: ", mineNumber);
		scanf("%d",&yearOpened);
		printf("\n Please enter the year mine %d was closed: ", mineNumber);
		scanf("%d",&yearClosed);
		printf("\n Please enter the total ore production of mine %d in kg: ", mineNumber);
		scanf("%d",&totalProduction);
							 		
		/*Calculations for mine lifespans and production*/
		mineLifespan = yearClosed - yearOpened;
		printf("\n The lifespan of mine %d is %d", mineNumber, mineLifespan);
		yearlyProduction = (float) totalProduction / mineLifespan;
		printf("\n The average yearly ore production for mine %d is %0.2f", mineNumber, yearlyProduction);

		/*Calculations for total combined mine production*/
		storedTotal = totalProduction + totalMineProduction;
		totalMineProduction = storedTotal;
									
		/*If statement for adjusting longest lifespan*/
		if(mineLifespan > longestLifespan)
		{
			longestLifespan = mineLifespan;
			longestLifespanNo = mineNumber;
		}
	}
	/*Printfs for showing total mine production and the mine number of the longest mine lifespan*/
	printf("\n The total combined mine production is %d", totalMineProduction);
	printf("\n The number of the mine with the longest lifespan is %d", longestLifespanNo);
							 	
							 	
	getchar();
	getchar();
}


By the way, the previous conflicts were resolved, thanks.
Last edited on by twicker
Topic archived. No new replies allowed.