Thread: What's wrong with this code?

  1. #1
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80

    What's wrong with this code?

    I'm trying to make a program that can determine how many characters there is in a text, but it outputs the same result, nomatter how long the text is.

    What's wrong with it?
    Code:
    char s1[100];
    int n=0, t=0;
    
      cout << "Inset text: "; cin >> s1;
    
      while(s1[n]!='\n')
            {
            t++;
            n++;
            }
    
      cout << "You text has " << t << " characters in it";

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    cin doesn't append a \n
    It should append a \0 though - try testing for that in your loop
    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.

  3. #3
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Hehe, I knew that. What a dumb mistake. Thanks for the help again

    Can I lock this thread, btw?

  4. #4
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    I have a new question. How do I clear s1 after every loop?

    Here is my code as it looks now. Apparently cin.clear() doesn't work, or I'm using it wrong.
    Code:
    char s1[100];
    int n=0, t=0, x=0;
    
      while(x==0)
      {
      cout << "Inset text: "; cin >> s1;
    
      while(s1[n]!='\0')
            {
            t++;
            n++;
            }
    
      cout << "You text has " << t << " characters in it\n";
    
      cin.clear();
      }
    Last edited by stillwell; 10-07-2004 at 08:08 AM.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Clear s1?? Like erasing the string??
    perhaps s[0]=0; ?

  6. #6
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    No, like getting rid of the input just made.

    i.e I type in wolla and s1 gets the value wolla. If I then input the word, word, 2nd time around, s1 will be worda, and still give the output 5, even thou word is a 4 letter word.

    How do I clear s1, so that there will be no data in it, when I loop.
    Last edited by stillwell; 10-07-2004 at 08:08 AM.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Code:
    #include <string>
    
    using namespace std;
    
    //...
    
    
    	string foo;
    
    	cin >>foo; //or getline(cin, foo) for \n terminated
    	
    	cout <<foo.size();
    I wish I understood why people think C-style strings are easier for newbies to work with...

  8. #8
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Ah, that's the same program, but with string? How would I clear the string then?

    I also still want to know how to clear the char array s1.
    Last edited by stillwell; 10-07-2004 at 08:35 AM.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by stillwell
    Ah, that's the same function, but with string? How would I clear the string then?
    Code:
    string foo;
    ...
    foo.erase();  // Clears contents of string
    You would not need to clear the contents if using a string... the cin or getline calls will overwrite any existing contents of the string container.

    Quote Originally Posted by stillwell
    I want to know how to clear the char array s1.
    If you want to stick with a character array, you can probably do something like calling memset on your array to 'zero' it out every time within your loop:

    Code:
    char array[100];
    ...
    memset(array,0,sizeof(array));
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Code:
    memset(s1, 0, 100);
    That will 0 out any memory block. Look up the function to know which param is which, but it should be obvious.

    Edit: Beaten to the punch.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    To clear a string, you would use the following line:

    Code:
    string foo;
    
    foo.erase();
    To clear a char array, I suppose you could use memset, although there's probably a better way...

    Code:
    char foo[100];
    cin.getline( foo, 99 );
    
    memset( foo, '\0', 100 );
    
    cout <<s1 <<endl;
    [EDIT]: Wow, you guys are fast today...

  12. #12
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Doesn't seem to work. Isn't it supposed to be written like so, in my program?
    Code:
    memset(s1,0,sizeof(s1));
    Do I need to include some stream?

    (sorry if I'm not using correct termanalogy, but I'm a damn noob )

    "[EDIT]: Wow, you guys are fast today..."

    Hehe, no kidding. I didn't even see the 2 other answers.
    Last edited by stillwell; 10-07-2004 at 08:42 AM.

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    You'd probably have better results if you used whatever magic number you used to create the array for the size argument of memset, rather than the sizeof operator.

  14. #14
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Hmmm, this doesn't work either:

    Code:
    memset(s1, '\0', 100);
    Don't I need to include something for it to work?

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    You need to have <string> included if you don't already.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM