entering your name and using break

This is a discussion on entering your name and using break within the C++ Programming forums, part of the General Programming Boards category; Hi 1: One way to the end the name is to input "0" once you have entered your name but ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    entering your name and using break

    Hi

    1: One way to the end the name is to input "0" once you have entered your name but this "0" would appear at the end of your name which doesn't look good. Is there some simple way to get over it? Please let me know.

    Code:
    // learning_example_arrays_and_strings.cpp
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    const int LEN = 20;
    
    int main()
    {
            char name[LEN] = {0}; //twenty zeros
    
            for(int i = 0; i < (LEN-1); i++)
            {
               cout << "enter letter " << (i+1) << " of your name: ";
               cin >> name[i];
    
               if (name[i] == '0')
               {
                       break;
               }
            }
    
            cout << "Your name: " << name << endl;
            system("pause");
            return 0;
    }
    [/code]

    OUTPUT
    Code:
    Enter letter 1 of your name: j
    Enter letter 2 of your name: a
    Enter letter 3 of your name: c
    Enter letter 4 of your name: k
    Enter letter 5 of your name: s
    Enter letter 6 of your name: o
    Enter letter 7 of your name: n
    Enter letter 8 of your name: 0
    Your name: jackson0
    
    Process returned 0 (0x0)   execution time : 12.040 s
    Press any key to continue.
    2: I have changed the (LEN - 1) to just (LEN). It still works. I can input 20 characters instead of 19. But I get a weird looking character at 21st position at the end.

    OUTPUT
    Code:
    Enter letter 1 of your name: j
    Enter letter 2 of your name: a
    Enter letter 3 of your name: c
    Enter letter 4 of your name: k
    Enter letter 5 of your name: s
    Enter letter 6 of your name: o
    Enter letter 7 of your name: n
    Enter letter 8 of your name: 1
    Enter letter 9 of your name: 1
    Enter letter 10 of your name: 1
    Enter letter 11 of your name: 1
    Enter letter 12 of your name: 1
    Enter letter 13 of your name: 1
    Enter letter 14 of your name: 1
    Enter letter 15 of your name: 1
    Enter letter 16 of your name: 1
    Enter letter 17 of your name: 1
    Enter letter 18 of your name: 1
    Enter letter 19 of your name: 1
    Enter letter 20 of your name: 1
    Your name: jackson1111111111111
    
    Process returned 0 (0x0)   execution time : 18.399 s
    Press any key to continue.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So check for something better than '0'.

    Your 20th character means you no longer have a \0 character at the end of your string, and nowhere to put that \0 character since you don't own any more memory. Hence your string will continue in memory until such time as you happen to find a \0 character. You are extremely fortunate that it only took one extra byte, as it could easily have been 1 MB of garbage.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    1) You can do:
    Code:
    if (name[i] == '0')
    {
        name[i] = '\0';
        break;
    }
    2) You're lucky that it does work. You're overwriting the last zero, therefore "cout" doesn't know when to end printing, and it prints garbage.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, tabstop, Sipher. I'm happy that I was fortunate... Sipher, your suggestion solved the problem.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is there something like break(2) (multiple break) ?
    By Phuzzillogic in forum C++ Programming
    Replies: 11
    Last Post: 05-11-2009, 04:05 AM
  2. Entering a SPACE
    By peckitt99 in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 04:55 AM
  3. ,getting the key without ENTERing
    By fnoyan in forum Linux Programming
    Replies: 1
    Last Post: 03-06-2006, 06:22 AM
  4. entering into 2d array
    By gammacad in forum C Programming
    Replies: 7
    Last Post: 06-09-2002, 03:21 PM
  5. Entering passwords in C++
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2001, 06:37 AM

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