Making descisions using if and else statements. Console shutting down.

Write your question here.

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
  
#include <iostream>
using namespace std;


int main()

{

	int actualage = 0, dogweight = 0;
	



	cout << "Welcome to Tina's dog age calculator!" << endl;
	cout << endl;
	cout << "Please enter your dogs actual age (1-16): " << endl;
	cin >> actualage;



	if (actualage<1)
	{
		cout << "Invalid age. Please ener a number between 1 and 16. " << endl;
		cin >> actualage;
		
	}
	else if (actualage>16)
	{
		cout << "Invalid age. Please enter a number between 1 and 16. " << endl;
		cin >> actualage;
		
		
	}



	if ((actualage>=1) && (actualage<=16))
	{
		cout << "Please enter your dogs weight: " << endl;
		cin >> dogweight;
	}

	

		if ((dogweight>=20) && (dogweight<=50) && (dogweight<20) && (dogweight>50) 
			&& (actualage>=2) && (actualage<=16))
		{
			cout << "Your dogs age is " << 4*actualage+16 << endl;
	}
		 else if (actualage==1)
		{
			cout << "Your dogs age is " << actualage+14 << endl;
		}

		 else if ((dogweight>=21) && (dogweight<=50) && (actualage>=6) && (actualage<=16))
		{
			cout << "Your dogs weight is " << 4.5*actualage+16 << endl;
		}

		 else if ((dogweight>=50) && (actualage>=6) && (actualage<=16))
		{
			cout << "Your dogs age is " << 7.5*actualage << endl;
		}
		
	

	system ("pause");
		return 0;
	}



I'm having a problem getting my program to continue on after the user inputs their dog's weight. The console just closes instead of doing the calculations. It worked perfectly before I added the error messages. This is giving me such a headache.
I think it worked for me.

Welcome to Tina's dog age calculator!

Please enter your dogs actual age (1-16): 
1
Please enter your dogs weight: 
2
Your dogs age is 15

Process finished with exit code 0


using this 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
#include <iostream>

using namespace std;

int main() {
    int actualage = 0, dogweight = 0;
    cout << "Welcome to Tina's dog age calculator!" << endl;
    cout << endl;
    cout << "Please enter your dogs actual age (1-16): " << endl;
    cin >> actualage;
    if (actualage < 1) {
        cout << "Invalid age. Please ener a number between 1 and 16. " << endl;
        cin >> actualage;

    }
    else if (actualage > 16) {
        cout << "Invalid age. Please enter a number between 1 and 16. " << endl;
        cin >> actualage;

    }
    if ((actualage >= 1) && (actualage <= 16)) {
        cout << "Please enter your dogs weight: " << endl;
        cin >> dogweight;
    }
    if ((dogweight >= 20) && (dogweight <= 50) && (dogweight < 20) && (dogweight > 50)
        && (actualage >= 2) && (actualage <= 16)) {
        cout << "Your dogs age is " << 4 * actualage + 16 << endl;
    }
    else if (actualage == 1) {
        cout << "Your dogs age is " << actualage + 14 << endl;
    }
    else if ((dogweight >= 21) && (dogweight <= 50) && (actualage >= 6) && (actualage <= 16)) {
        cout << "Your dogs weight is " << 4.5 * actualage + 16 << endl;
    }
    else if ((dogweight >= 50) && (actualage >= 6) && (actualage <= 16)) {
        cout << "Your dogs age is " << 7.5 * actualage << endl;
    }
    
    // system ("pause");
    // http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong

    //do this
    // http://stackoverflow.com/questions/454681/how-to-keep-the-console-window-open-in-visual-c
    
    
    return 0;
}



are you using Visual Studio? See my comments.
Yes i'm using visual studio.
1
2
if ((dogweight >= 20) && (dogweight <= 50) && (dogweight < 20) && (dogweight > 50)
        && (actualage >= 2) && (actualage <= 16)) {


So all those conditions have to be true, for this to evaluate to true. It doesn't happen :+(

You could have a series of bool variables which age / weight ranges the animal is in. That would simplify the other tests considerably.
I have the problem narrowed down to one specific thing. For some reason if I take out the actualage>16 part, it works fine. But I need the error message to include that. The program is supposed to give an error message if the age entered is less than 1 or more than 16.
OMG I fixed it!! YEESSS!! I forgot to put a cin after the error message. Oh man. Thank you guys for your help I appreciate it!!!
Try:

1
2
3
4
5
6
7
8
9
10
11
bool ValidAge = false;
while (!ValidAge) {
    if (actualage < 1 || actualage > 16) {
        std::cout << "Invalid age. Please enter a number between 1 and 16. \n" ;
        std::cin >> actualage;
        std::cin.ignore(100, \n)
    }
    else {
        ValidAge = true;
    }
}


I have the problem narrowed down to one specific thing.


You still need to fix the other problems :+)
Topic archived. No new replies allowed.