Thread: functions frustration

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    functions frustration

    i dont know where to start with this code i have

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    
    
    //char ans = 'y';
    short left_numbers,last_number;//numbers left over
    long input;//user input
           
    
    string suffix_1 = "st";
    string suffix_2 = "nd";
    string suffix_3 = "rd";
    string suffix_beyond = "th";
    
    
    
    cout<<"what is your number?\n";
    cin>>input;
    
    
    left_numbers = input % 100;//special cases 
    last_number = input % 10;//ordinary cases 0,1,2,etc
    
    
    
      if( (left_numbers <=13) && (left_numbers >= 11)  )//special cases 11-13
      {   
      cout<<input<<suffix_beyond;
      }
    
    
         else if(last_number == 1)//suffix for 1
        {
         cout<<input<<suffix_1; 
         }
    
            else if(last_number == 2)//suffix for 2
            {
            cout<<input<<suffix_2;
            }
    
                else if(last_number == 3)//suffix for 3
               {
                cout<<input<<suffix_3;
               }
    
    
       else
       {
       cout<<input<<suffix_beyond;
       }
    
    
    return 0;
    }
    heres a link to the program
    http://home.earthlink.net/%7Ecraie/121/labs/suffix.html

    i got the basics of the program to work; but i'm trying to add more levels to it:

    Add (Level 2) to use functions to break your program into more manageable pieces. I'd recommend the display of the result go into a function to de-clutter the main.

    In fact, you can probably break out another function from the results-display: the random pre-message generation. If you do, I'll throw in another (Level 1.5) to make just the random number formula into an inline function.

    You can add a special (Level 1.5) to place your suffix-determining code in a generic/re-usable function.

    Add (Level 1.5) to place your suffix function in a library.

    Add (Level 1.5) to place your random number generation function in a library.

    what are yall recommendations on starting a function in order to add these levels?

    thanks in advance

    btw,i'm still working on it. hopefully i'll figure it out soon

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Does that tutorial have a lesson on functions? If not, take a look at the cprogramming.com function tutorial, and I strongly advise you to get a good beginning C++ book. Most books will give you much more explanation/information than an online tutorial... A typical beginning C++ book will cover esentially the same topics as a tutorial, but it will have 300-700 pages!

    The concept of a function is a "detour" in the program flow. So, as your programming is stepping-through main(), it takes a detour to do the stuff in the function, and then comes back to main where it left-off.

    Or, you can think of a function as a "subcontractor". You hire the main contractor to build a house, and he hires a bunch of subcontractors to do the actual work.

    One advantage of functions is that you can call the function several times, from different places in your program. If you didn't use a function, you would have to repeat that code everywhere it's needed. You can even build a library of functions and use them in several different programs.

    You will have a function prototype, a function definition, and one or more function calls. Take your time, and study these things carefully, because function prototypes, function calls, and the first line of a function definition, all look somewhat similar, but they have slightly different requirements.


    When your function returns to main(), it may return with one value. If your function needs to affect/change more than one value, you have to use references or pointers.

    And, when you pass-in a variable to a function, you are only passing-in the value associated with the variable. The variable name inside the function may be different from the name of the variable passed-in.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    i got the special cases for 11-13 to work; but it prints out the suffix first then the users input. how should i go about to fix this?

    anyway here is the code:

    * the function declaration,function call and function definition is bolded *

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    double suffix_th(long input, short left_numbers, const string suffix_beyond);
    //take in the last 2 numbers left from left_numbers 
    //and return the according user input+suffix(ie string)
    //example function for the special cases 11-13
    
    int main()
    
    {
    
    	short left_numbers;
    	//last_number;//numbers left over
    	long input;//user input
           
    
    	//const string suffix_1 = "st";
    	//const string suffix_2 = "nd";
    	//const string suffix_3 = "rd";
    	const string suffix_beyond = "th";
    
    
    
            cout<<"what is your number?\n";
            cin>>input;
    
              left_numbers = input &#37; 100;//special cases 
    
            cout << suffix_th(left_numbers, input, suffix_beyond);
    
    /*
    last_number = input % 10;//ordinary cases 0,1,2,etc
    
    
    
      if( (left_numbers <=13) && (left_numbers >= 11)  )//special cases 11-13
      {   
      cout<<input<<suffix_beyond;
      }
    
    
         else if(last_number == 1)//suffix for 1
        {
         cout<<input<<suffix_1;
        }
    
            else if(last_number == 2)//suffix for 2
            {
            cout<<input<<suffix_2;
            }
    
                else if(last_number == 3)//suffix for 3
               {
                cout<<input<<suffix_3;
               }
    
    
       else
       {
       cout<<input<<suffix_beyond;
       }
    
    */
    
    
    return 0;
    
    }
    
    	double suffix_th(long input, short left_numbers, const string, suffix_beyond)	
                                              
                    {
    		if( (left_numbers <=13) && (left_numbers >= 11)  )
                                   / /special cases 11-13
    		{   
    		left_numbers= input%100;
    		cout<<suffix_beyond;
    	}
                    return left_numbers;
    
    }
    am i on the right track? if i am, how can i write a function for all the cases(ie 1,2,3,4, special cases) to shorten the code even more?

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    well am i on the right track but taking a different train?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Keep going, you should get there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    ^
    thanks for the motivation

    but i still cant get the output to print the input in front of the suffix

    also, i think that i am using the wrong parameters and wrong variable declarations

    anyway, i'll try again in a couple of hours; need sleep

    i have to turn this in before 5pm

    wish me luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM