Simplier way to do this?

Could I do this in a easier way. Is the way im doing it stupid. I've been doing this since I begun programming 5 months ago. I don't know if it's wrongg it just look dumb.
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

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

bool weight(int IfHungry)
{
    if (IfHungry <= 50)
        return cout << "Hes Hungry." << endl;

}
bool Not_weight (int IfHungry)
{
    if (IfHungry >= 50)
        return cout << "Hes is not Hungry." << endl;

}

int main()
{
  int Mike;

  cout << "Please enter a number to see if he's hungry." << endl;
  cin >> Mike;
  if (Mike < 50)
  {
      weight(Mike);
  }
else
{
    Not_weight(Mike);

}
The biggest problem with your code are your functions. You should not be returning cout, especially as a bool. your functions should be a little more like this:

1
2
3
4
5
6
void Not_weight (int IfHungry)
{
    if (IfHungry >= 50)
        cout << "Hes is not Hungry." << endl;
    return;
}


void means the function returns no value, and you are not required to use return.

The next thing I noticed was that you are not catching the values returned from your functions, you should do that.

Then, you are including headers you are not using. You are not using vectors so line 4 is not needed, and you are not accessing files so line 2 is not needed.

Also, you are checking the value of mike twice, which may not be needed.

Here is your code with a few changes:

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

void weight()
{
	std::cout << "Hes Hungry." << std::endl;

}
void Not_weight ()
{
	std::cout << "Hes is not Hungry." << std::endl;

}

int main()
{
	int Mike;

	std::cout << "Please enter a number to see if he's hungry." << std::endl;
	std::cin >> Mike;
	if (Mike <= 50)
	{
		weight();
	}
	else
	{
		Not_weight();
	}
}


You may notice that I have chosen to remove using namespace std;. I have done this to avoid namespace conflicts, which you may find hard to find in the future.
Last edited on
Topic archived. No new replies allowed.