Need a little help :)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Need a little help :)

  1. #1
    Join Date
    Aug 2012
    Posts
    1

    Need a little help :)

    So recently i made this code for my homework:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    
    {
       int date;
       int month;
       char ans;
       cout<<"Enter your birth month and date to determine your zodiac sign\n\n"<<endl;
    
        while (!((ans=='n') || (ans=='N')))
        {
        cout<<"Enter your birth month:";
        cin>>month;
        cout<<"Enter your birth date:";
        cin>>date; 
       
        
        if (( month == 1 && date >= 20 && date <= 31 ) || ( month == 2 && date <=18 ))
        {
             cout<<"Your zodiac sign is AQUARIUS\n\n";
             cout<<"Aquarius is an old constellation. As the Water Carrier or Water Bearer,\nhe is carved in stones";
             cout<<"of the Babylonian Empire and probably is still older than that period.\n";
             
        }
        else if (( month == 2 && date >= 19 && date <= 28) || ( month == 3 && date <= 20))
        {
             cout<<"Your zodiac sign is PISCES\n\n";
             cout<<"Pisces was named after Aphrodite and Eros.The story of Pisces originated from\n";
             cout<<"the destructive monster, Typhon.\n";
        }
        else if (( month == 3 && date >= 21 && date <= 31) || ( month == 4 && date <= 19))
        {
             cout<<"Your zodiac sign is ARIES\n\n";
             cout<<"Aries is a zodiacal constellation representing the ram of the Golden\n";
             cout<<"Fleece sought by Jason and the Argonauts.\n";
             
        }
        else if (( month == 4 && date >= 20 && date <= 30 )  || ( month == 5 && date <= 20))
        {
             cout<<" Your zodiac sign is TAURUS\n";
        }
        else if (( month == 5 && date >= 21 && date <= 31 )  || ( month == 6 && date <= 20 ))
        {
             cout<<" Your zodiac sign is GEMINI\n";
        }
        else if (( month == 6 && date >= 21 && date <= 30 ) || ( month == 7 && date <= 22 ))
        {
             cout<<" Your zodiac sign is CANCER\n";
        }
        else if (( month == 7 && date <= 23 && date <= 31) || ( month == 8 && date <= 22))
        {
             cout<<" Your zodiac sign is LEO\n";
        }
        else if (( month == 8 && date >= 23 && date <= 31) || ( month == 9 && date <= 22 ))
        {
             cout<<" Your zodiac sign is VIRGO\n";
        }
        else if (( month == 9 && date >= 23 && date <= 30) || ( month == 10 && date <= 22))
        {
             cout<<" Your zodiac sign is LIBRA\n";
        }
        else if (( month == 10 && date >= 23 && date <= 31 ) || ( month == 11 && date <= 21))
        {
             cout<<" Your zodiac sign is SCORPIO\n";
        }
        else if (( month == 11 && date >= 22 && date <= 30 ) || ( month == 12 && date <= 21))
        {
             cout<<" Your zodiac sign is SAGUITTARIUS\n";
        }
        else if (( month == 12 && date >= 22 && date <= 31 ) || ( month == 1 && date <= 19 ))
        {
             cout<<" Your zodiac sign is CAPRICORN\n";
             
        }
        else
        {
            cout<<" INVALID INPUT\n";
        }
        cout<<"do you want to continue? y/n"<<endl;
        cin>>ans;
        }
        if ( ans == 'n')
        {
             cout<<"Thank you for your precious time! :D\n\n";
        }
    
         system("pause");
    
    }
    but now i have another problem..the problem is i need to have a invalid input when the program asks "do you want to continue? Y/N"..for ex:

    Do you want to continue? Y/N :
    z (this is the input)
    Sorry, i can only determine yes or no.

    something like that..

  2. #2
    Join Date
    Aug 2012
    Posts
    12

    Re: Need a little help :)

    I am posting this reply for the OP or anyone else who might have the same question.
    I am sorry if there are any errors. Do note that this code is not optimized, but it should be helpful.

    Please, feel free to use the code for any purpose.

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    // Declare your functions up front. Define them later.
    // You don't have to write out the full names here, as it is obvious
    //  what these functions are for.
    
    void coutAries ( void );
    void coutTaurus ( void );
    void coutGemini ( void );
    void coutCancer ( void );
    void coutLeo ( void );
    void coutVirgo ( void );
    void coutLibra ( void );
    void coutScorpio ( void );
    void coutSagittarius ( void );
    void coutCapricorn ( void );
    void coutAquarius ( void );
    void coutPisces ( void );
    
    // Using enum can help prevent logical errors.
    // Just remember, the first value in any array is located in index 0.
    // Any easy way to solve this problem is to simply label the first
    //  element to the number you desire. The elements following the
    //  labeled one will continue from there.
    /*
    	const enum
    	{
    		JANUARY = 1,
    		FEBRUARY,
    		MARCH,
    		APRIL,
    		MAY,
    		JUNE,
    		JULY,
    		AUGUST,
    		SEPTERMBER,
    		OCTOBER,
    		NOVEMBER,
    		DECEMBER
    	};
    */
    
    // You can also use shorthand for months. They, too, are obvious.
    const enum
    {
    	JAN = 1,
    	FEB,
    	MAR,
    	APR,
    	MAY,
    	JUN,
    	JUL,
    	AUG,
    	SEP,
    	OCT,
    	NOV,
    	DEC
    };
    
    // This function returns the upper bound of the date range according
    //  to month.
    
    // The easiest way to return a months maximum date, is to use an array.
    // Remember, our number for January is 1, so we need to either subtract
    //  1 from 'month' or use a placehold in our array.
    const int monthMaxDate[13] =
    { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    
    int dateMax ( int month )
    {
    	return monthMaxDate[month];
    }
    // I think I have an extensive piece of code that checks for leap years
    //  as well. But, I'll leave that fun up to you. :)
    
    // Here is a function that I have sitting around. It can be used
    //  to convert a C++ string into a number. It returns the result
    //  as an integer.
    
    int stringToInt ( const string & String )
    {
    	stringstream Stream ( String );
    	int Result;
    	return ( Stream >> Result ) ? Result : 0;
    }
    
    int main ()
    {
    	// When naming variables, you should be as specific as possible.
    	// Also, try to use the smallest type possible. In this case,
    	//  a char would be the best choice, because it is a single byte.
    	char userDate, userMonth;
    	string userInput;
    
    	// For while checking, I would suggest something a bit different.
    	// There is nothing wrong with checking the input for a value type.
    	// However, it is easier to manage a variable with the specific task
    	//  of iterating the loop.
    
    	// You can use any data type here, but a 1 byte type saves space.
    	bool exitLoop = false;
    
    	// Now we can simply check for one value here. This method saves
    	//  nothing. However, it is far clearer and easier to read.
    	while ( exitLoop == false )
    	{
    		// Initialize your variables to zero.
    		userDate = 0;
    		userMonth = 0;
    		userInput = "";
    
    		// This output should ideally go here, in case you want to restart
    		//  the main loop so that the user can run it again.
    
    		// I like letting my users exit when entering a blank.
    		// But I forgot, it takes some other code to do, so nevermind that for now.
    		cout <<
    			"Enter your birth month and date to determine your Zodiac sign.\n"
    			<< endl;
    
    		// Now, I have a very helpful library that makes input validation
    		//  10 times easier. However, I am not ready to have my code
    		//  picked apart by other programmers here.
    
    		// I'll show you a simple way to validate your user input.
    		// Idealy, you would want to use a C++ string for this task.
    		// Strings are easy to use, and prevent many errors that
    		//  you might encounter with a C string.
    
    		// First, we drop everything into our string type userInput variable.
    		// Then, using some functions, we can check the input to see if it
    		//  complies with what we are asking for.
    
    		// The point of this program is to tell the user which zodiac sign
    		//  he or she is. It would be rather pointless to continue the
    		//  program if the user enters invalid data at any step in the
    		//  input process. Therefore, we should confront the user if
    		//  he or she gives an invalid input, then re-prompt.
    
    		// For this program, I believe this is the easiest and fastest
    		//  way to check for proper input.
    		while ( userMonth == 0 )
    		{
    			cout << "Enter your birth month: ";
    			cin >> userInput;
    
    			// This is the easiest way to exit the loop and then program.
    
    			// Now we shall convert the input into a number. Our function
    			//  will do all the dirty work for us. If the input was not a number,
    			//  the result will be 0.
    			userMonth = stringToInt ( userInput );
    
    			// Finally, we check to make sure the data is in the proper range.
    			if ( userMonth < 1 || userMonth > 12 )
    			{
    				userMonth = 0;
    				cout << " INVALID INPUT\n";
    			}
    		}
    
    		while ( userDate == 0 )
    		{
    			cout << "Enter your birth date: ";
    			cin >> userInput;
    
    			userDate = stringToInt ( userInput );
    
    			// Now, checking for the range of the month is easy. There are only
    			//  12 months. However, the the date range varies month to month.
    			// To fix this, we will use a function! It depending on the month
    			//  the user entered, it will give us the upper bound of the date
    			//  range.
    			if ( userDate < 1 || userDate > dateMax ( userMonth ) )
    			{
    				userDate = 0;
    				cout << " INVALID INPUT\n";
    			}
    		}
    
    		// Here, we can use a series of conditional statements. Though,
    		//  it would be far easier and less time consuming to use a different
    		//  method. However, I assume this is assignment specific.
    
    		// Instead, let me fix up some redundancy, make the list easier to read.
    
    		// For zodiac signs, I would start with January, and check for
    		//  the low order dates first. Notice that there is no reason to
    		//  do full bounds checking here. Your input validation should
    		//  do all the bounds checking for you, prior to this part.
    
    		if ( userMonth == JAN )
    		{
    			if ( userDate <= 19 )
    			{
    				coutCapricorn ();
    			}
    			else // if ( userDate >= 20 )
    			{
    				coutAquarius ();
    			}
    		}
    
    		if ( userMonth == FEB )
    		{
    			if ( userDate <= 18 )
    			{
    				coutAquarius ();
    			}
    			else // if ( userDate >= 19 )
    			{
    				coutPisces ();
    			}
    		}
    
    		if ( userMonth == MAR )
    		{
    			if ( userDate <= 20 )
    			{
    				coutPisces ();
    			}
    			else // if ( userDate >= 21 )
    			{
    				coutAries ();
    			}
    		}
    
    		if ( userMonth == APR )
    		{
    			if ( userDate <= 19 )
    			{
    				coutAries ();
    			}
    			else // if ( userDate >= 20 )
    			{
    				coutTaurus ();
    			}
    		}
    
    		if ( userMonth == MAY )
    		{
    			if ( userDate <= 20 )
    			{
    				coutTaurus ();
    			}
    			else // if ( userDate >= 21 )
    			{
    				coutGemini ();
    			}
    		}
    
    		if ( userMonth == JUN )
    		{
    			if ( userDate <= 20 )
    			{
    				coutGemini ();
    			}
    			else // if ( userDate >= 21 )
    			{
    				coutCancer ();
    			}
    		}
    
    		if ( userMonth == JUL )
    		{
    			if ( userDate <= 22 )
    			{
    				coutCancer ();
    			}
    			else // if ( userDate >= 23 )
    			{
    				coutLeo ();
    			}
    		}
    
    		if ( userMonth == AUG )
    		{
    			if ( userDate <= 22 )
    			{
    				coutLeo ();
    			}
    			else // if ( userDate >= 23 )
    			{
    				coutVirgo ();
    			}
    		}
    
    		if ( userMonth == SEP )
    		{
    			if ( userDate <= 22 )
    			{
    				coutVirgo ();
    			}
    			else // if ( userDate >= 23 )
    			{
    				coutLibra ();
    			}
    		}
    
    		if ( userMonth == OCT )
    		{
    			if ( userDate <= 22 )
    			{
    				coutLibra ();
    			}
    			else // if ( userDate >= 23 )
    			{
    				coutScorpio ();
    			}
    		}
    
    		if ( userMonth == NOV )
    		{
    			if ( userDate <= 21 )
    			{
    				coutScorpio ();
    			}
    			else // if ( userDate >= 22 )
    			{
    				coutSagittarius ();
    			}
    		}
    
    		if ( userMonth == DEC )
    		{
    			if ( userDate <= 21 )
    			{
    				coutSagittarius ();
    			}
    			else // if ( userDate >= 22 )
    			{
    				coutCapricorn ();
    			}
    		}
    
    		// Now we should ask if the user wants to rerun the program.
    		cout << "\n\nWould you like to re-run the program? (Y/N) ";
    		while ( exitLoop == false )
    		{
    			cin >> userInput;
    
    			if ( userInput == "Y" || userInput == "y")
    			{
    				cout << "\n\n";
    				break;
    			}
    			else if ( userInput == "N" || userInput == "n")
    			{
    				exitLoop = true;
    				break; // This break isn't necessary.
    			}
    			else
    			{
    				cout << "Please enter Y to re-run the program or N to exit. (Y/N) ";
    			}
    		}
    	}
    
    	// There is no reason to check this. You should always thank your users,
    	//  before the program exits.
    	//if ( userInput == "n" )
    	//{
    	cout << "\n\nThank you for your precious time! :D\n\n";
    	//}
    
    	// I suggest that you do not use System Pause. It is cpu expensive,
    	//  and other programmers simply love to make fun of people who use it.
    	// Now. The fact that you are trying to pause the program makes the
    	//  cpu expense rather a pointless argument. The real reason they don't
    	//  like it is because it isn't very portable. And it's windows. Haha
    
    	// If you want to use System Pause, go ahead. Though, you should
    	//  understand that console applications are 'supposed' to be run under
    	//  some form of console. This defeats the purpose of the System Pause
    	//  at the end of a program. It simply does no good as a console will
    	//  stay open even after the program finishes. But, most people don't
    	//  really know what a console is and how to use it, so they just
    	//  double click an exe file and hope it works.
    
    	//system ("pause");
    }
    
    void coutAries ( void )
    {
    	// Instead of breaking up your output into separate statements like this,
    	//  you should look up a help document on strings in C/C++.
    	cout << "Your zodiac sign is ARIES\n\n"
    		"Aries is a zodiacal constellation representing the ram of the Golden\n"
    		"Fleece sought by Jason and the Argonauts.\n";
    }
    
    void coutTaurus ( void )
    {
    	cout << "Your zodiac sign is TAURUS\n";
    }
    
    void coutGemini ( void )
    {
    	cout << "Your zodiac sign is GEMINI\n";
    }
    
    void coutCancer ( void )
    {
    	cout << "Your zodiac sign is CANCER\n";
    }
    
    void coutLeo ( void )
    {
    	cout << "Your zodiac sign is LEO\n";
    }
    
    void coutVirgo ( void )
    {
    	cout << "Your zodiac sign is VIRGO\n";
    }
    
    void coutLibra ( void )
    {
    	cout << "Your zodiac sign is LIBRA\n";
    }
    
    void coutScorpio ( void )
    {
    	cout << "Your zodiac sign is SCORPIO\n";
    }
    
    void coutSagittarius ( void )
    {
    	cout << "Your zodiac sign is SAGUITTARIUS\n";
    }
    
    void coutCapricorn ( void )
    {
    	cout << "Your zodiac sign is CAPRICORN\n";
    }
    
    void coutAquarius ( void )
    {
    	cout << "Your zodiac sign is AQUARIUS\n\n"
    		"Aquarius is an old constellation. As the Water Carrier or Water Bearer,\n"
    		" he is carved in stones of the Babylonian Empire and probably is still older than that period.\n";
    }
    
    void coutPisces ( void )
    {
    	cout << "Your zodiac sign is PISCES\n\n"
    		"Pisces was named after Aphrodite and Eros. The story of Pisces originated from\n"
    		" the destructive monster, Typhon.\n";
    }
    Fixed a couple mistakes, haha.
    Last edited by Vlykarye; August 10th, 2012 at 02:38 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center