A more simplified program.

Hello. I just made a program that request the number of graduates as input and then displays the number of tickets to be distributed to each graduates.
Can it be simplified again?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
int main()

{
	int seats=2000, graduates, tickets;
	cout<<"Enter number of graduates: ";
	cin>>graduates;
	
	if ((graduates>=1)&&(graduates<=2000))
	{			  
	   tickets=seats/graduates;
	   cout<<"Each graduates gets "<<tickets<<" tickets"<<endl;   
	}
   
   else if ((graduates<=0)||(graduates>=2000))
	{
	   cout<<"Not in range!";
	}
	 return 0;
}
Last edited on
First, your code doesn't not compile as-is. I believe you forgot a "using namespace std;" ( The use of "using namespace std;" can be bad if not used carefully, see: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice ).

Second, decide on one style. Currently, you have a mix of tabs and spaces. It's a holy war to say which is better (no flame wars, pls), but one thing everyone agrees on is to stay consistent.

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

int main()
{
	const int seats = 2000; // variable will not change; make it const

	std::cout << "Enter number of graduates: ";
	int graduates = seats; // (initialize graduates with an invalid number)
			// if std::cin fails to receive input, this will be the default value now

	std::cin >> graduates;
	
	// (Personally I prefer to deal with bad logic first,
	// then move onward with the good logic.
	// just a personal preference, though.)
	if (graduates <= 0 || graduates >= seats) // removed magic number
	{
		std::cout << "Not in range!" << std::endl;
	}
	else // we don't need further if-logic here
	{			  
		int tickets = seats/graduates; // put variables on the inner-most scope necessary
		std::cout << "Each graduate gets " << tickets << " tickets" << std::endl;   
	}
	
	return 0;
}


Edit: Updated to remove magic numbers
Last edited on
Topic archived. No new replies allowed.