string problem

This is a discussion on string problem within the C++ Programming forums, part of the General Programming Boards category; First, have you had any experience with functions? You know their format, how to call them, etc.? Second, a general ...

  1. #16
    Registered User
    Join Date
    May 2007
    Posts
    77

    Post

    First, have you had any experience with functions? You know their format, how to call them, etc.?

    Second, a general rule of thumb is that for any bracket "{", put that on the same indent as the code it is for (i.e., if you have an "if", after the parameters are set, on the new line, you want to line the "{" right under the "i" in if). Then, anything inside the bracket is a tab (usually 4 spaces in coding programs) further than the bracket. Like this:
    Code:
    while(ans=='y' || ans=='Y')
    {
        cout<<"what number would you like to convert?:\n";
        cin>>input;
        cin.ignore();
    
        if(input>0 && input<4000 )
        {
            roman="";
      
           //thousands place to get remainder(1000-3000)
           i=1;
           n=input/1000;
          while(i<=n)
          {
              roman=roman+rome1000;
              i=i+1;
          }
    Now, as for functions, if you are familiar with them, start simply with making one part of your program a function (probably start with the 1000's), then call that function in your "main()". You'll probably want to start with globals, just to make it easier, and then work on how to work with calling and returning variables through functions.

    And by the way, for your declaration of roman, for your output, you can avoid having to use the line (roman="") by simply declaring it in the while loop, or even in the if section it is used in. That will especially come in handy once you start passing variable through functions.
    Last edited by Molokai; 11-07-2007 at 08:26 PM.

  2. #17
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Quote Originally Posted by INeedSleep View Post

    i think i pretty much finished these levels:

    Add (Level 3) to use functions to break your program into more manageable pieces. I'd recommend the bounds checking code for the user's number and the display of the result. In fact, that results display itself could be broken down into a couple of functions: random pre-message and random post-message (with the result itself printed between these calls).

    You can add another (Level 1.5) to place the repetitious pattern of converting [each digit of] the user's number into A re-usable function.

    i have to look over the code more throughly
    Code:
    //updated 11/7/07
    // Jason helped:  Wed Nov  7 15:49:56 CST 2007
    #include <cctype>
    #include <string>
    #include <iostream>
    #include <climits>
    using namespace std;
    
    // single digit n, three char string with ones digit in position 0,
    // fives digit in position 1, and tens digit in position 2.  returns
    // digit n represented in Roman numeral form.
    string to_roman(short n, const string & OneFiveTen);
    
    int main()
    {
    
       char ans='y';
       string roman;
       short input; //user input
    
    //string names converted to roman numerals
       const string rome1="I";
       const string rome5="V";
       const string rome10="X";
       const string rome50="L";
       const string rome100="C";
       const string rome500="D";
       const string rome1000="M";
    
       while(tolower(ans)=='y')
       {
          cout<<"what number would you like to convert?:\n";
          cin>>input;
          cin.ignore(INT_MAX, '\n');
    
          if(input>0 && input<4000 )
          {
      
             roman="";
      
      //thousands place to get remainder(1000-3000)
             roman = roman + to_roman(input/1000, rome1000);
        
       //hundreds place to get remainder(100-300)
             roman = roman + to_roman(input%1000/100, rome100+rome500+rome1000);
    
       //tens place
             roman = roman + to_roman(input%100/10, rome10+rome50+rome100);
    
       //ones place
             roman = roman + to_roman(input%10, rome1+rome5+rome10);
       
             cout << "that is converted from a" <<" " << input << " "
                  <<"into" << " " << roman <<" " << "in roman numerals\n";
             cout << "would you like to try again?";
             cin >> ans;
          }
       
          else
          {
             ans='n';
             cout<<"then get the f*&k out then !";
          }
       
          if(ans =='y' || ans =='Y')
          {
             cout<<"dont come the f@*k back !";
          }
      
       }
     
       return 0;
    }
    
    // single digit n, three char string with ones digit in position 0,
    // fives digit in position 1, and tens digit in position 2.  returns
    // digit n represented in Roman numeral form.
    string to_roman(short n, const string & OneFiveTen)
    {
       string in_roman;
       short i;
       if( n == 4 || n == 9) // handle 4 and 9 separately
       {
          in_roman=OneFiveTen[0];
          if(n==4)
          {
             in_roman+=OneFiveTen[1];
          }
          else // if(n==9)
          {
             in_roman+=OneFiveTen[2];
          }
       }
       else
       {
          if(n>=5) // handle 5 ..and start 6,7,8
          {
             in_roman+=OneFiveTen[1];
             n=n-5;
          }
          i=1;
          while(i<=n)
          {
             in_roman+=OneFiveTen[0];
             i=i+1;
          }
       }
       return in_roman;
    }
    ignore the "then get the f*&k out then !" and "dont come the f@*k back !"

    Jason is my professor, btw

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
          i=1;
          while(i<=n)
          {
             in_roman+=OneFiveTen[0];
             i=i+1;
          }
    Why not use the standard idiom?
    Code:
    for(i = 0; i < n; i ++) {
        in_roman += OneFiveTen[0];
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    ^
    teacher never covered for loops, only while
    i was thinking about doing it, but i dont want to risk getting anything marked off

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    If your teacher really docks you for using for loops instead of while loops, maybe you should email him that program -- with the . . . messages left in.

    Seriously. A for loop is functionally equivalent to a while loop.

    And if you don't want to use a for loop, consider counting from 0..n-1 rather than 1..n.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    i'll put it in and show it to him later today and see what he says

    anyway, i started this program a few hours ago:
    http://home.earthlink.net/%7Ecraie/121/labs/suffix.html

    i will post my code later on today

  7. #22
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    *finding the suffix*

    i'm in the real early stages
    i have the formulas for the special cases and for the regular cases

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    
    
    //char ans = 'y';
    double left_numbers;//numbers left over
    short 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;
    
    //special cases 
    left_numbers = input &#37; 100;
    
    
    //ordinary cases
    //left_numbers = input % 10;
    
    
    
          
    //special cases 11-13
    if( (left_numbers <=13) && (left_numbers >= 11)  )
    {
    cout<<input<<suffix_beyond;
    }
    
    /*
    //suffix for 0
    else if(left_numbers == 0)
    {
    cout<<input<<suffix_beyond;
    }
    
    
    //suffix for 1
    else if(input % 10 == 1  )
    {
    cout<<input<<suffix_1;
    }
    */
    
    else
    {
    cout<<"done";
    }
    
    return 0;
    }
    btw, i'm adding a loop after i get the basics to work first

  8. #23
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    i finally got the basic code to work

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    
    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;
    }
    now i need to add all of these(or most)

    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.

    need help with starting anyone of these.

  9. #24
    Registered User
    Join Date
    May 2007
    Posts
    77
    How did the roman numeral conversion work out? Or is this somehow part of it?

  10. #25
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    it worked out well. my professor helped me out

    Code:
    //updated 11/7/07
    // Jason helped:  Wed Nov  7 15:49:56 CST 2007
    #include <cctype>
    #include <string>
    #include <iostream>
    #include <climits>
    using namespace std;
    
    // single digit n, three char string with ones digit in position 0,
    // fives digit in position 1, and tens digit in position 2.  returns
    // digit n represented in Roman numeral form.
    string to_roman(short n, const string & OneFiveTen);
    
    int main()
    {
    
       char ans='y';
       string roman;
       short input; //user input
    
    //string names converted to roman numerals
       const string rome1="I";
       const string rome5="V";
       const string rome10="X";
       const string rome50="L";
       const string rome100="C";
       const string rome500="D";
       const string rome1000="M";
    
       while(tolower(ans)=='y')
       {
          cout<<"what number would you like to convert?:\n";
          cin>>input;
          cin.ignore(INT_MAX, '\n');
    
          if(input>0 && input<4000 )
          {
      
             roman="";
      
      //thousands place to get remainder(1000-3000)
             roman = roman + to_roman(input/1000, rome1000);
        
       //hundreds place to get remainder(100-300)
             roman = roman + to_roman(input&#37;1000/100, rome100+rome500+rome1000);
    
       //tens place
             roman = roman + to_roman(input%100/10, rome10+rome50+rome100);
    
       //ones place
             roman = roman + to_roman(input%10, rome1+rome5+rome10);
       
             cout << "that is converted from a" <<" " << input << " "
                  <<"into" << " " << roman <<" " << "in roman numerals\n";
             cout << "would you like to try again?";
             cin >> ans;
          }
       
          else
          {
             ans='n';
             cout<<"then get the f*&k out then !";
          }
       
          if(ans =='y' || ans =='Y')
          {
             cout<<"dont come the f@*k back !";
          }
      
       }
     
       return 0;
    }
    
    // single digit n, three char string with ones digit in position 0,
    // fives digit in position 1, and tens digit in position 2.  returns
    // digit n represented in Roman numeral form.
    string to_roman(short n, const string & OneFiveTen)
    {
       string in_roman;
       short i;
       if( n == 4 || n == 9) // handle 4 and 9 separately
       {
          in_roman=OneFiveTen[0];
          if(n==4)
          {
             in_roman+=OneFiveTen[1];
          }
          else // if(n==9)
          {
             in_roman+=OneFiveTen[2];
          }
       }
       else
       {
          if(n>=5) // handle 5 ..and start 6,7,8
          {
             in_roman+=OneFiveTen[1];
             n=n-5;
          }
          i=1;
          while(i<=n)
          {
             in_roman+=OneFiveTen[0];
             i=i+1;
          }
       }
       return in_roman;
    }
    i got these 2 done

    Add (Level 3) to use functions to break your program into more manageable pieces. I'd recommend the bounds checking code for the user's number and the display of the result. In fact, that results display itself could be broken down into a couple of functions: random pre-message and random post-message (with the result itself printed between these calls).

    You can add another (Level 1.5) to place the repetitious pattern of converting [each digit of] the user's number into A re-usable function.



    i just need to add these to my program and i am finished:

    Add (Level 1) to add failure checking to the numeric entry code. (Yes, this would go into the function if you do that option. But, you can add another (Level 1) if you make it a separate function that works well together with the bounds checking function...)

    Add (Level 2) to use a switch to good effect in your [digit] conversion code. (This option is unaffected by that code being placed in a function. i.e. you still get this level as well as that one...)

    (Note: the switch variant still has four 'branches', but it may need more than one case per branch. Some programmers call each case a branch even when several match to the same statement(s). *shrug* I'd probably go with 'the number of branches in a switch is the number of statement groups -- each possibly matchable by several cases'.)

    Oh, why not? Go ahead and add (Level 2.5) to translate fractions. (See the resources above!)
    Last edited by INeedSleep; 11-08-2007 at 11:57 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21