Thread: Birthstones, Months, Strings and Functions!

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    12

    Question Birthstones, Months, Strings and Functions!

    Alright, I'm working on this program in C++ condole that will pretty much Greet you, ask some birthday questions and display information... Good thing is. I got it working, But then I re-read the instructions and I was suppose to use a function to get the Month and Birthstone...

    Sorry if my Pseudocode is crap

    (long)

    Code:
    #include "stdafx.h"
    #include <iostream> //Includes standard input-output library
    #include <string> //Allows use of String
    #include <cstdio> //Used for getchar
    using std::cin; //Standard output stream
    using std::cout; //Standard output stream
    using std::endl; //End line command
    using namespace std; //Uses namespace Std
    
    int _tmain(int argc, _TCHAR* argv[]) //Start Program execution
    {
    	string name; //Uses name as a String
    	int choice; //Choice for the Menu
    	int month,day; //Day and month of birth
    	string months;//Name of months
    	string stone; //Name of birthstones
    	int key; //A Wildcard for While loop
    
    	cout<<"Soo... what's your name?"<<endl;
    	cin>>name;
    	cout<<endl<<"What day(1-31) were you born?";
    	cin>>day;
    	cout<<endl<<"Which month(1-12) is that?";
    	cin>>month;
    	switch(month)
    	{
    		case 1:
    			months="January";
    			stone="Granet";
    			break;
    		case 2:
    			months="Febuary";
    			stone="Amethyst";
    			break;
    		case 3:
    			months="March";
    			stone="Aquamarine";
    			break;
    		case 4:
    			months="Apirl";
    			stone="Diamond";
    			break;
    		case 5:
    			months="May";
    			stone="Emerald";
    			break;
    		case 6:
    			months="June";
    			stone="Pearl";
    			break;
    		case 7:
    			months="July";
    			stone="Ruby";
    			break;
    		case 8:
    			months="Augest";
    			stone="Peridot";
    			break;
    		case 9:
    			months="September";
    			stone="Sapphire";
    			break;
    		case 10:
    			months="October";
    			stone="Opal";
    			break;
    		case 11:
    			months="November";
    			stone="Topaz";
    			break;
    		case 12:
    			months="December";
    			stone="Turquoise";
    			break;
    	}
    	cout<<"Thank you."<<endl<<endl;
        while(key)
    	{
    		cout<<"____________________________________"<<endl;//Displays the Diamond Header
    		cout<<"|*      *        *       *        *|"<<endl;//^^
    		cout<<"| **    ***     ***    ***      ** |"<<endl;//^^
    		cout<<"|  ***  *****  *****  *****   ***  |"<<endl;//^^
    		cout<<"|    **   ***   ***   ***    **    |"<<endl;//^^
    		cout<<"|      *    *    *    *     *      |"<<endl;//^^
    		cout<<"------------------------------------"<<endl;//^^
    		cout<<"          Welcome "<< name << "!" <<endl;//Welcome User
    		cout<<"          Born: "<<months<<" - "<<day<<endl;// Displays Birthday Info
    		cout<<"____________________________________"<<endl;//^^
    		cout<<"|          (1)Birthstone           |"<<endl;//Displays Menu
    		cout<<"|          (2)Astrological         |"<<endl;//^^
    		cout<<"|          (3)Season               |"<<endl;//^^
    		cout<<"|          (9)Exit                 |"<<endl;//^^
    		cout<<"|__________________________________|"<<endl;//Displays the Diamond Footer
    		cout<<"|      *    *    *    *     *      |"<<endl;//^^
    		cout<<"|    **   ***   ***   ***    **    |"<<endl;//^^
    		cout<<"|  ***  *****  *****  *****   ***  |"<<endl;//^^
    		cout<<"| **    ***     ***    ***      ** |"<<endl;//^^
    		cout<<"|*      *        *       *        *|"<<endl;//^^
    		cout<<"------------------------------------"<<endl;//^^
    		cout<<"Select Number catagory you wish to view."<<endl;//Prompts User
    		cin>>choice; //User's input
    		switch(choice)//Chooses Menu Options
    		{
    			case 1: //If Option 2
    				cout<<"_____________"<<endl;//Display
    				cout<<"|Brithstone!|"<<endl;//^^
    				cout<<"-------------"<<endl;//^^
    				cout<<"Ok, let's see... You were born on "<<months<<endl;//Displays Text & Month
    				cout<<"So that means your birthstone is "<<stone<<endl;//Displays Text & Stone
    				break;
    			case 2: //Option 2
    				cout<<"Not Avaiable"<<endl<<endl;//Display
    				break;
    			case 3: // Option 3
    				cout<<"Not Avaiable"<<endl<<endl;
    				break;
    			case 9: // Option 9
    				cout<<"Exit"<<endl<<endl;
    				return 0; //Ends Program
    				break;
    			default: // If no other option is made
    				cout<<"Please choose options 1-3 or 9 to Exit"<<endl<<endl;;
    				break;
    		}
    	}
    }
    As you can see, it works. at least as far as i got. Birthstones and all with a endless menu loop unless 9, makes return 0 and ends the program.

    I somehow have to convert the first "cases" into a function to call on to determin the birthstone for the month.. but I used the switch for the month Number to Text conversion and birthstone code... I tried a few different ways from

    With these as global declarations
    Code:
    int month(int);//Global Month integer
    string months;
    strong stone;
    Code:
    int getBirthstone(int)
    {
    switch(month)
    	{
    		case 1:
    			months="January";
    			stone="Garnet";
    			break;
    		case 2:
    			months="February";
    			stone="Amethyst";
    			break;
    		case 3:
    			months="March";
    			stone="Aquamarine";
    			break;
    		case 4:
    			months="April";
    			stone="Diamond";
    			break;
    		case 5:
    			months="May";
    			stone="Emerald";
    			break;
    		case 6:
    			months="June";
    			stone="Pearl";
    			break;
    		case 7:
    			months="July";
    			stone="Ruby";
    			break;
    		case 8:
    			months="August";
    			stone="Peridot";
    			break;
    		case 9:
    			months="September";
    			stone="Sapphire";
    			break;
    		case 10:
    			months="October";
    			stone="Opal";
    			break;
    		case 11:
    			months="November";
    			stone="Topaz";
    			break;
    		case 12:
    			months="December";
    			stone="Turquoise";
    			break;
    	}
    }
    Help if you can, I'll still be looking around.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you seem to have the right idea. global variables are (almost) always avoidable--and should be avoided.

    to fix this, you could pass the months and stone variable by reference to your new function (as well as your integer month variable as you have already, which is 'pass by value'). i will try and give a (somewhat silly but hopefully helpful) example that you could use to make yours.

    Code:
    // headers, etc
    
    void ComputeTax( float orig_price, float& total_price)
    {
        float tax = 1.15;  // the 1 is to simplify the calculation later, the 15 is 15&#37; tax
    
        total_price = orig_price * tax;
    }
    
    int main(int argc, char** argv)
    {
       float originalPrice;
       float totalPrice;
    
       cout << "enter the price of the product: ";
       cin >> originalPrice;
    
      ComputeTax(originalPrice, totalPrice);
    
       cout << "the total price will be $" << totalPrice;
    
       return 0;
    }
    notice no global variables are used, yet it allows for variables to be passed between the functions by reference. this means that whatever ComputeTax does to the second parameter (total_price) it will actually affect the original variable where the function was called (totalPrice in main).

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    12
    Wow, thank you, Quick response. I'm trying to help another newer user atm so I'll study this in a moment and try to apply it to my current project.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would be good style to use const here:
    Code:
    const float tax = 1.15;  // the 1 is to simplify the calculation later, the 15 is 15% tax
    And of course, you could, if you wanted to, use a table (array) to define what the name of the month is and the birthstone that goes with it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    12
    I'm still lost. That example and your advice on (No global variables) helped me clean up another program but I still don't see how I can use getBirthstone(int month, string months, string stone) and still deliver a switch to enter it into the program...

    here's what I tried

    Code:
    void getBirthstone(int& month, string months, string stone)
    {
    	switch(month)
    	{
    		case 1:
    			months="January";
    			stone="Granet";
    			break;
    		case 2:
    			months="Febuary";
    			stone="Amethyst";
    			break;
    		case 3:
    			months="March";
    			stone="Aquamarine";
    			break;
    		case 4:
    			months="Apirl";
    			stone="Diamond";
    			break;
    		case 5:
    			months="May";
    			stone="Emerald";
    			break;
    		case 6:
    			months="June";
    			stone="Pearl";
    			break;
    		case 7:
    			months="July";
    			stone="Ruby";
    			break;
    		case 8:
    			months="Augest";
    			stone="Peridot";
    			break;
    		case 9:
    			months="September";
    			stone="Sapphire";
    			break;
    		case 10:
    			months="October";
    			stone="Opal";
    			break;
    		case 11:
    			months="November";
    			stone="Topaz";
    			break;
    		case 12:
    			months="December";
    			stone="Turquoise";
    			break;
    	}
    }
    
    int _tmain(int argc, _TCHAR* argv[]) //Start Program execution
    {
    	string name; //Uses name as a String
    	int choice; //Choice for the Menu
    	int day; //Day and month of birth
    	string stone; //Name of birthstones
    	int key; //A Wildcard for While loop
    
    	cout<<"Soo... what's your name?"<<endl;
    	cin>>name;
    	cout<<endl<<"What day(1-31) were you born?";
    	cin>>day;
    	cout<<endl<<"Which month(1-12) is that?";
    	getBirthstone(int &month);
        cin>>month;
    	cout<<"Thank you."<<endl<<endl;
    Last edited by DigitalBear; 09-24-2008 at 09:00 AM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have the & backwards -- no on the month, yes on the months and the stone. (Those are the ones you need to change.)

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    also, your getBirthstone function you declared to (properly) accept the three arguments (note, however, what tabstop said above, which of course he is correct). however, you call it in main as "getBirthstone(int &month)", only passing one parameter. you should be passing all three. when you call it you dont have to worry about putting "&" in this case. also you shouldnt pass the argument as "int month"; leave out the datatype int.

    one final thing: lets assume you have already fixed up the getBirthstone function and are calling it with the three arguments properly. when in this function you will do "switch" on the month variable. however, you are calling this function before month is initialized. it is quite unlikely that the (somewhat) random value that is assigned to the month variable falls in the range [1,12]. to solve this, think about the order your program will follow, focusing on: the print statement asking for month, calling getBirthstone, and reading the month variable from console. this fix is very simple so dont overthink it.

    hope it helps

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    12
    Thank you for all your help but i figured it out in some way to find out it's harder.
    Code:
    string getBirthstone(int month)
    {
    	string stone;
    	switch(month)
    	{
    		case 1:
    			stone="Granet";
    			break;
    		case 2:
    			stone="Amethyst";
    			break;
    		case 3:
    			stone="Aquamarine";
    			break;
    		case 4:
    			stone="Diamond";
    			break;
    		case 5:
    			stone="Emerald";
    			break;
    		case 6:
    			stone="Pearl";
    			break;
    		case 7:
    			stone="Ruby";
    			break;
    		case 8:
    			stone="Peridot";
    			break;
    		case 9:
    			stone="Sapphire";
    			break;
    		case 10:
    			stone="Opal";
    			break;
    		case 11:
    			stone="Topaz";
    			break;
    		case 12:
    			stone="Turquoise";
    			break;
    	}
    	return stone;
    }
    So I can return, just stone and have it otuput correctly using
    Code:
    cin>>month;
    	stone = getBirthstone(month);
    The instructions also say use
    [code][enum Months{JAN=1,FEB,MAR,...,DEC};
    Which would turns to be pointless but nesseary for the program.. Anyways, I got it all figured out except now

    What I want to do now is made a option to list all birthstones, probably will use a cleaver cout and just list them all. Which will be accepted.

    But what I need help with is how to have the program list the month as a name instead of a number. But in order to use return to get = stone, there could only be one variable.

    I don't want to do a whole other case to change each number month into a string to say the months name like 1=january

    As it shows up now, it will say

    Code:
    cout<<"          Welcome "<< name << "!" <<endl;//Welcome User
    cout<<"          Born: "<<month<<"-"<<day<<endl;// Displays Birthday Info
    It will display 3-8 for march 8th... need to figure out a eays way to convert back to text.

    Thanks for all your help so far.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You could try a parallel array and just index all the answers.

    Code:
      month = LookupMonth[mo];
      stone = LookupStone[mo];
    Just be careful of fencepost errors. "mo" should index the right month.

    And because we need to return two variables from this function now, references would be a prudent choice.
    Last edited by whiteflags; 09-24-2008 at 12:28 PM.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not impliment a struct or a class to hold this information?

    Example:
    Code:
    #include <exception>
    #include <string>
    
    class whatever
    {
    public:
      whatever(int _month) : month(_month)
      {
        if(month < 1 || month > 12)
          throw std::out_of_range("What calendar are you using, buddy?");
      }
    
      whatever(const whatever &o) : month(o.month)
      {
    
      }
    
      virtual ~whatever()
      {
    
      }
    
      const std::string getBirthstone() const
      {
        switch(month) {
          case 1: return "Granet";
          case 2: return "Amethyst";
          case 3: return "Aquamarine";
          case 4: return "Diamond";
          case 5: return "Emerald";
          case 6: return "Pearl";
          case 7: return "Ruby";
          case 8: return "Peridot";
          case 9: return "Sapphire";
          case 10: return "Opal";
          case 11: return "Topaz";
          case 12: return "Turquoise";
        }
      }
    
      const std::string getMonth() const
      {
        switch(month) {
          case 1: return "January";
          case 2: return "February";
          case 3: return "March";
          case 4: return "April";
          case 5: return "May";
          case 6: return "June";
          case 7: return "July";
          case 8: return "August";
          case 9: return "September";
          case 10: return "October";
          case 11: return "November";
          case 12: return "December";
        }
      }
    private:
      int month;
    };
    [Edit] This is not the most efficient way, as you should immediately notice. Perhaps if you combined my code with some of the other suggestions... (That was my subtle hint of the day) [/edit]

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    12
    Thank you all for your support so far, hopefully I'm not too annoying. The Reasons I haven't used class, struct or arrays are because I haven't learned them yet. The class on Classes and Arrays is on Monday. So until then I'm doing my best to learn and reverse engineer your code to understand and each myself.

    How would I index all the answers if I used a parallel array? Can you give me an example?
    Last edited by DigitalBear; 09-25-2008 at 09:31 PM.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Perhaps this will help:

    Quote Originally Posted by http://en.wikipedia.org/wiki/Parallel_array
    In computing, a parallel array is a data structure for representing arrays of records. It keeps a separate, homogeneous array for each field of the record, each having the same number of elements. Then, objects located at the same index in each array are implicitly the fields of a single record. Pointers from one object to another are replaced by array indices. This contrasts with the normal approach of storing all fields of each record together in memory. For example, one might declare an array of 100 names, each a string, and 100 ages, each an integer, associating each name with the age that has the same index.
    But since you can't use arrays yet I don't think it matters. Use someone else's idea.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    12
    true, but I want to learn, this will be my career someday and in that pursuit I'm going to give my all to learn as much as quick and thoroughly as possible.. "believe it".. (lulz)

    Anyways. Thanks for the link, I've been trying a fre different ways from other tutorials and guides but they are all for int, not string/names. I keep getting can't convert to string from int, etc

    Here's what i got,
    Code:
    Removed
    Edit: Yeah I'm an idiot, useing Months(string) and trying to make it output to month(int)... I am an idiot
    It doesn't have errors but it still displays the number instead of the array name. I only did Jan and Feb to save time, didn't work with 1 as the answer to month


    Would this work months(month);?
    Last edited by DigitalBear; 09-26-2008 at 08:48 AM.

  14. #14
    Registered User
    Join Date
    Sep 2008
    Posts
    12
    It Worked! Yes!

    I'll post code n a moment.

    Thanks again to all who helped +rep(ocn)

    This isn't over tho This is an ongoing project. I'm happy to be a new part of this awesome community.

    Code:
    removed
    Also, if you got any ideas or suggestions for proper Pseudocode, let me know
    Last edited by DigitalBear; 09-26-2008 at 08:49 AM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I warned you about fencepost errors earlier remember? C++ arrays range from index 0 to n-1. You'll have to adjust your variable to index the right month.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing strings to functions and modifying them
    By diddy02 in forum C Programming
    Replies: 6
    Last Post: 08-11-2008, 01:07 AM
  2. Functions and Strings Question
    By StrikeMech in forum C Programming
    Replies: 4
    Last Post: 07-18-2007, 06:07 AM
  3. passing strings from functions as the "return" part
    By fraktal in forum C Programming
    Replies: 8
    Last Post: 12-13-2005, 01:38 AM
  4. Creating Functions that use Format control Strings
    By tzuchan in forum C Programming
    Replies: 6
    Last Post: 03-29-2004, 12:50 PM
  5. Using Strings with Functions
    By DJH in forum C Programming
    Replies: 10
    Last Post: 10-19-2001, 04:40 PM