name altering

This is a discussion on name altering within the C++ Programming forums, part of the General Programming Boards category; ok what im trying to do is if a name is longer than 5 characters it deletes the other characters ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    name altering

    ok what im trying to do is if a name is longer than 5 characters it deletes the other characters and puts ... at the end.

    but i cant figure out how to do i tried deleting chars but that only got through the debugger it didnt work.

    does anyone know how to do this?

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Read in one character at a time, store character in a string, and increment a
    counter - when you hit the fifth character, check if it's a newline/EOF, if it's not,
    then flush the buffer.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Alternatively you could read in the name in it's entirety. Determine the length of the name. If it is longer than 5 create a new string using just the first 5 char of the name and the 3 periods to the end of the 5 letters. The syntax of either approach is going to depend on which version of a string you use.
    You're only born perfect.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    Declare a char array of, like, 40 elements (or a higer number if you want), and then read the input into the char array (or whatever you do). Then, check with an if statement, if the 6th element is different than '\0'. if it is, use a loop to set the rest of the char array to ...

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    There's no reason to use a char array for this. Just use a C++ string.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    There's no reason to use a char array for this. Just use a C++ string.
    But, I assume this is homework. In that case, we need to know if you are have been studying C-style strings <cstring> (character arrays) or C++ string objects <string>.

    You should have learned how to check the length of a string, how to trim-off the end of a string, how to combine two strings, etc.

    Give it a try. If you can't get it to work, post your code and we will try to give you some help and hints.... But, you have to do your own work!

    ...but that only got through the debugger it didnt work.
    The debugger should be the last resort. It can help you figure out what the program is doing, but it doesn't know what you program should do, or how you should do it.

    P.S.
    Here's a C-style string tutorial, and another C-style string tutorial. It's not a tutorial, but here's a C++ string reference.
    Last edited by DougDbug; 09-15-2006 at 02:17 PM.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    I wish this was homework ..but i dont have grade 9 "introduction to technology" until next semester,so i suppose you could call this training?

    especially since im trying to get a "buisness endorsement certificate".when i find out what that is

    p.s.when im trying code where i dont know where to start,getting through the debugger is a major sucess
    Last edited by lilhawk2892; 09-15-2006 at 03:28 PM.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Richie gave you the answer. Explore now. Use google on his post using some of the key wording there... for instance "c++ string" and "c++ read character" and such... Use tutorials, read them, explore ways to solve your problem learning in the process... in other words, be a programmer.

    Someone will be here helping you fix your code when you have something workable
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    haha well i suppose this isnt workable but its a start

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char name[10]
        
        cin>>name;
        if (name > 5)
        name - 5 + "...";
        
        cout<<name[10];
    };
    by the way while im here,i see some people using


    "using std::cout"

    etc,etc.why do this?wouldnt it be simpler to use "using namespace std"?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It's not about what is simpler, it is about what is "better". Adding using namespace std is fine for small projects, but it defeats the entire purpose of namespaces. Some people prefer to get into good habits from the beginning. There are tons of threads and posts on this subject if you'd like to search for more detail.

    If you are into simpler and "better", you would be using C++ strings, instead of C style character arrays.

    Either way, you need to find the length of the string as mentioned already. Use some of the posted resources to find out how strings work so you can improve your code.

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by lilhawk2892
    haha well i suppose this isnt workable but its a start

    by the way while im here,i see some people using


    "using std::cout"

    etc,etc.why do this?wouldnt it be simpler to use "using namespace std"?
    In general, using is a bad keyword to use. When you work with multiple namespaces, it tends to make things confusing.

    It's even worse when you do "using namespace std" because you run a risk of accidently naming a variable or function the same as a name in the std library.

    Using std::cout is a little better because it reduces the risk of name collision, but it still defeats the purpose of namespaces. Personally, I think the "using" keyword never should have been added to the language, as it just tends to add confusion, but I do recognize it was done for helping people rapidly get legacy code compiling after they added namespaces to C++.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >if a name is longer than 5 characters it deletes the other characters and puts ... at the end
    If you use a std::string, you can accomplish this easily using the string member functions length and substr.

    Here's an online reference: http://www.cppreference.com
    There's also a string tutorial here: http://www.cprogramming.com/tutorial/string.html

  13. #13
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Code:
    #include <iostream>

    using namespace std;

    int main()
    {
    char name[10]

    cin>>name;
    if (name > 5)
    name - 5 + "...";

    cout<<name[10];
    };
    OK. You are using C-style strings (AKA character arrays). Some people would tell you to use C++ strings, but I won't. C-style strings can teach you about how variables are stored in memory. C++ strings just work "magically", and you will need to learn about both types, eventually.

    if (name > 5) will not work, because when you use name without the brackets, it means a pointer (an address) to the first character in the string. name[0] will give you the first letter of the name, name[1] gives you the 2nd character, etc.

    name - 5 + "..."; won't work either. Again, "name" alone is an address. Subtracting 5 from that address will give you a meaningless number. Finally, you can't "add" two C-style strings together with a plus sign. (You can do that with C++ strings, because '=' is overloaded for <string> (magic!).


    The functions you need to use are in the <cstring> header.

    strnlen() will give you the length of the string.

    strcpy() and strncpy() can copy one string into another. You could use strncpy() to make a new string with just the first 5 characters.

    strcat() and strncat() can be used to combine two strings (the way you tried to do it with +).

    There are more str...() functions in <cstring>.

    i suppose you could call this training?
    OK. Do you have a book? It's very hard to learn this stuff without a book or a class to guide your study. In any case, most of us are going to give you hints & suggestions, instead of spoon-feeding you the answer. I know it's a pain, but we are all "hacker types" who enjoy solving puzzles, and we expext everybody else to be that way too!
    Last edited by DougDbug; 09-15-2006 at 06:53 PM.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I try to avoid overallocating, so I was just thinking about using getline.
    Code:
    #include <iostream>
    
    int main(void)
    {
       char name[6];
       std::cout << "name? ";
       std::cin.getline(name, sizeof name);
       std::cout << "name = \"" << name;
       if ( !std::cin )
       {
          std::cout << "...";
          while ( std::cin.get() != EOF ); // eat extra characters
       }
       std::cout << "\"\n";
       return 0; 
    }
    
    /* my input/output
    name? Dave
    name = "Dave"
    name? Sinkula
    name = "Sinku..."
    */
    Last edited by Dave_Sinkula; 09-15-2006 at 07:08 PM. Reason: Touchups.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Dave_Sinkula
    I try to avoid overallocating, so I was just thinking about using getline.
    Code:
    #include <iostream>
    
    int main(void)
    {
       char name[6];
       std::cout << "name? ";
       std::cin.getline(name, sizeof name);
       std::cout << "name = \"" << name;
       if ( !std::cin )
       {
          std::cout << "...";
          while ( std::cin.get() != EOF ); // eat extra characters
       }
       std::cout << "\"\n";
       return 0; 
    }
    
    /* my input/output
    name? Dave
    name = "Dave"
    name? Sinkula
    name = "Sinku..."
    */
    And if name happens to be <= 5 characters?

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

Similar Threads

  1. Real time altering of screen output
    By Eirik in forum Windows Programming
    Replies: 4
    Last Post: 05-14-2009, 12:05 AM
  2. Need help with altering info on a txt file
    By Netflyer in forum C Programming
    Replies: 29
    Last Post: 02-14-2008, 06:23 AM
  3. Altering The Program's Executable?
    By Aidman in forum C++ Programming
    Replies: 7
    Last Post: 12-31-2002, 04:11 AM
  4. Replies: 10
    Last Post: 06-12-2002, 03:15 PM
  5. Altering files in another file system??
    By Raavin in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2001, 09:18 AM

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