entering a string using for loop, ""

This is a discussion on entering a string using for loop, "" within the C++ Programming forums, part of the General Programming Boards category; Hi In the code below both blue cout statements should produce the same result because in both cases name[] variable ...

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

    entering a string using for loop, ""

    Hi

    In the code below both blue cout statements should produce the same result because in both cases name[] variable is being worked on. But you see in one case I get this "". Why is so? Please let me know. Thanks.

    When a string is entered using cin a space is considered to be end of a string. While entering the name using the for loop I inserted two spaces but they were ignored. Why is so?

    Please help me. Thanks.

    Code:
    // read_your_name.cpp
    // read and print your name using an array and using
    // cin, for loop, cin.get()
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <string>
    
    using namespace std;
    
    const int C = 20;
    
    int main()
    {
            int i;
            char name[C];
            string urname;
    
            cout << "enter your name: ";
            cin >> name;
    
            cout << "your name is: Mr. " << name << endl;
    
    
            cout << "enter your name: ";
    
            for (i=0; i<C; i++)
            {
                    cin >> name[i];
            }
    
            cin.ignore();
    
            cout << "your name is: Mr. ";
    
            for (i=0; i<C; i++)
            {
                    cout << name[i];
            }
    
            cout << endl;
    
            cout << "your name is: Mr. " << name << endl;
    
    
            cout << "\nenter your name: ";
            cin.get(name, C);
    
            cout << "enter your name: ";
            getline(cin, urname);
    
            cout << "your name is: Mr. " << name << endl;
    
            system("pause");
            return 0;
    }
    OUTPUT
    Code:
    enter your name: jackson
    your name is: Mr. jackson
    enter your name: j
    a
    c
    k
    s
    o
    n
       //space
    h
    e
    i
    g
    h
    t
    s
      //space
    0
    0
    0
    0
    0
    0
    your name is: Mr. jacksonheights000000
    your name is: Mr. jacksonheights000000
    
    enter your name: jackson heights
    enter your name: your name is: Mr. jackson heights
    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
    1. Which part of "put a @$#%!@#$!@#$ \0 character at the end of your string" do you continually fail to understand?

    2. You correctly note that spaces are ignored; then ask why, when you type spaces, they are ignored? Why do you ask this question?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi

    Quote Originally Posted by tabstop View Post
    1. Which part of "put a @$#%!@#$!@#$ \0 character at the end of your string" do you continually fail to understand?
    Both of these:
    Code:
    your name is: Mr. jacksonheights000000
    your name is: Mr. jacksonheights000000
    are couts of the same variable. The first one results from using the for loop, and the second one using the 'normal' cout. I wanted to know that why do I get "" when the variable name[] is worked on by 'normal' cout?

    2. You correctly note that spaces are ignored; then ask why, when you type spaces, they are ignored? Why do you ask this question?[/QUOTE]
    I asked because in normal cases when an array is entered using cin the portion of the string entered after pressing space is ignored. But here space is ignore but the portion entered after space is not ignored.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because you didn't put a \0 character at the end of your string. The first time you did not print the string, you printed a bunch of individual characters. The second time you printed the string. When you print a C-string, it goes until it finds a \0 character. If you didn't put one in (and you didn't), then it's going to keep walking. You must lead a charmed life, because you again only have one extra character instead of screenful after screenful of garbage. But you must put a \0 character at the end of your C-string. It is not optional.

    2. C++ ignores all spaces at the beginning of input (spaces = the big long bar at the bottom of the keyboard, the tab key, and the enter-key). So your space was ignored, your enter key was ignored, and finally once you typed a letter it actually accepted the input.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by tabstop View Post
    Because you didn't put a \0 character at the end of your string. The first time you did not print the string, you printed a bunch of individual characters. The second time you printed the string. When you print a C-string, it goes until it finds a \0 character. If you didn't put one in (and you didn't), then it's going to keep walking. You must lead a charmed life, because you again only have one extra character instead of screenful after screenful of garbage. But you must put a \0 character at the end of your C-string. It is not optional.

    2. C++ ignores all spaces at the beginning of input (spaces = the big long bar at the bottom of the keyboard, the tab key, and the enter-key). So your space was ignored, your enter key was ignored, and finally once you typed a letter it actually accepted the input.
    Thanks a lot, tabstop.

    Yes, I didn't put a \0 at the end because I was wrongly under the impression the impression that the compiler would insert it automatically. Although, I think, the compiler automatically inserts a \0 character but it requires that you haven't overrun the string's limit.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you do character-by-character input, then the compiler is not going to treat the data as a string, because you are explicitly telling it not to. In that case, you are responsible for all the characters.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Code:
    cin >> name;
    How many times do we have to tell you to not do that?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi Elysia

    Quote Originally Posted by Elysia View Post
    Code:
    cin >> name;
    How many times do we have to tell you to not do that?
    You mean I should have used cin.getline() or cin.get() instead? Please let me know this know, then I won't repeat this mistake again. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Yeah, get and getline are safe to use. You could use cin >> setw(C-1) >> name, but the other functions are easier.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by King Mir View Post
    Yeah, get and getline are safe to use. You could use cin >> setw(C-1) >> name, but the other functions are easier.
    Hi King Mir

    Please have a look on the code below. Shouldn't it be
    Code:
    cin >> setw(MAX-1) >> str;
    ? Please guide me on this. Thanks.


    Code:
    / safetyin.cpp
    // avoids buffer overflow with cin.width
    #include <iostream>
    #include <iomanip>                  //for setw
    using namespace std;
    
    int main()
       {
       const int MAX = 20;              //max characters in string
       char str[MAX];                   //string variable str
    
       cout << "\nEnter a string: ";
       cin >> setw(MAX) >> str;         //put string in str,
                                        // no more than MAX chars
       cout << "You entered: " << str << endl;  
       return 0;
       }
    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. Need "if","for loop",&"else" source codes
    By dn_angel_07 in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2009, 11:01 PM
  2. Replies: 5
    Last Post: 03-22-2008, 01:49 PM
  3. Replies: 46
    Last Post: 08-24-2007, 05:52 PM

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