name program

This is a discussion on name program within the C++ Programming forums, part of the General Programming Boards category; Let's say i typed in CHRIS then i want the program to print out Chris make lower case everyting except ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    15

    name program

    Let's say i typed in CHRIS
    then i want the program to print out Chris
    make lower case everyting except first letter of the name
    ty
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    char upper(char upper);
    char lower(char lower);
    
    int main(void)
    
    {
            string first, middle, last, whole;\
            long number;
    
            cout <<"\nWhat is your name?\n ";
    
            cin >> first >> middle;
    
            if (cin.peek() != '\n')
            {
                    cin >>last;
             }
            else
            {
                    last = middle;
                    middle = "";
            }
            cout << upper(last[0]);
    
    
            last = last.substr(1);
    
            number = 0;
            while (number < last.length())
            {
                    last += lower(last[number]);
                    number++;
            }
    
    
    
            //whole = last + ',' + first + ' ' + middle[0] + '.';
            cout << whole;
    
    
    
    
    
            return 0;
    }
    
            char upper(char upper)
            {
                    return toupper(upper);
            }
    
            char lower(char lower)
            {
                    return tolower(lower);
            }

  2. #2
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    And what was the question?
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    how do i change big case to lower case?

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    this code is suppose to find all the charcters except first letter of the name
    and change to lower case... but it doesnt work...T_T
    Code:
      while (number < last.length())
            {
                    last += lower(last[number]);
                    number++;
            }

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    It seems to me you're modifying (and making longer) the same string that you're reading from.

    Read from one string, and write to another.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    This should do the trick:
    Code:
    int NameFormat(char* pszString, DWORD dwString)
    {
       if(dwString < 1 || !pszString)
          return 1;
       if((pszString[0] >= 'a') && (pszString[0] <= 'z'))
          pszString[0] += ('A' - 'a');
       for(DWORD dwLoop = 1; dwLoop < dwString; dwLoop++)
          if((pszString[dwLoop] >= 'A') && (pszString[dwLoop] <= 'Z'))
             pszString[dwLoop] -= ('A' - 'a');
       return 0;
    }
    Last edited by Queatrix; 05-13-2007 at 01:25 PM.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    88
    This should also do the trick:

    Code:
    #include <iostream>
    #include <algorithm>
    #include <cctype>
    
    using namespace std;
    
    //We have to wrap tolower so that transform can determine its type
    char tolower_wrap(char c)
    {
    	return tolower(c);
    }
    
    int main() 
    {
    	string foo("CHRIS");
    	//Use transform to make each char after the first one lowercase
    	transform(foo.begin()+1, foo.end(), foo.begin()+1, tolower_wrap);
    	//Did it work?
    	cout <<foo <<endl;
    }

  8. #8
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> This should also do the trick:

    Phahch, mine is way more portable.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > Phahch, mine is way more portable.
    Yours isn't portable at all with all that 'A'-'a' stuff. Would it work with EBCDIC?

    At least using tolower() is an improvement.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    char my_tolower(char c)
    {
      char letters[52] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
      for(int i = 0; i < 26; i++)
      {
        if(c == letters[i])
        {
          return letters[i + 26];
        }
      }
      return c;
    }
    There you go. Completely portable.

  11. #11
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Just to be contrary:

    bl&#228;ck
    BL&#196;CK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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