What's wrong with this code?

This is a discussion on What's wrong with this code? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <string.h> Don't ask why its there... EDIT: Again with the too slowness...I should be working anyways...

  1. #16
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Code:
    #include <string.h>
    Don't ask why its there...

    EDIT: Again with the too slowness...I should be working anyways

  2. #17
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Damnit, it still doesn't work.

    Here is my entire program.
    Code:
    #include <vcl.h>
    #include <iostream>
    #include <string>
    #pragma hdrstop
    using namespace std;
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    main()
    {
    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";
    
      memset(s1, '\0', 100);
      }
            return 0;
    }

  3. #18
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    It doesn't work as in it doesn't compile, or as in the string isn't erased? If it's the former, what's the compiler's output? Help us out here, dude.

  4. #19
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    For starters, try moving the memset to the start of the loop. Since you aren't initializing s1, the first iteration might look like crap because the array will contain whatever junk is in memory (you'd be lucky if it was already 0)

  5. #20
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Hehe, sorry, I was down getting some tan.

    It doesn't work in the way that it doesn't clear s1. It can compile and all, it just keeps the value.

    Doesn't help that I move it to the top of the loop.

  6. #21
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Reset n and t to 0 at the start of the loop as well.

  7. #22
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Yes, of course !

    It's working

    Thanks for your help guys.

  8. #23
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Can I lock this thread, btw?
    Why?

    >>Hmmm, this doesn't work either:
    What do you mean by 'work'? Any of those methods will make the string 'empty', i.e. the first character will be NULL, meaning the string will have 0 length (and the rest of the characters will be NULL too, although that isn't necessary), so any function that requires a string will see an empty string ("").

    As xErath said (although slightly modified), to 'empty' the string, you just do:
    s1[0] = '\0';

    And as has been said already, you don't need to clear a string to store a different value in it.

    The reason your size isn't getting 'reset' is because you aren't resetting your counters (doh!). But even if you do that correctly, you should be using strlen() to calculate the length of a string - that's what it's there for - or if you're using a std::string, you should be using its .length() or .size() member functions.

    **EDIT**
    Also, if you're having problems, there are two easy ways to debug it yourself:
    1) Use a debugger and step through the code line by line, checking that the values are what they should be at each step of the program
    2) Use tons of std::cout << statements to make sure that everything is what it should be. If you did this, it would have been fairly obvious to see that the string does indeed get emptied, and that only the size reading is having problems; and from there, you could trace the program flow and see why the size isn't getting reset.
    Last edited by Hunter2; 10-07-2004 at 12:29 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #24
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Hey, don't mock me, I'm new to this.

    And it works now, but thanks for the help anyway.

    "2) Use tons of std::cout << statements to make sure that everything is what it should be. If you did this, it would have been fairly obvious to see that the string does indeed get emptied, and that only the size reading is having problems; and from there, you could trace the program flow and see why the size isn't getting reset"

    I would use it if I knew how. Like I've said, I'm pretty new to this.

  10. #25
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I would use it if I knew how. Like I've said, I'm pretty new to this.
    Don't worry about it, we all had to start somewhere But I'm just letting you know, cout is a powerful debugging tool especially when you have no idea where to start looking for the problem.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #26
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    To use cout to help debugging without a debugger you can do something like this.

    Identify a variable you want to monitor. Anytime the code modifies the variable, print out the value to be sure the results are as desired with a given input.

    Example: you are getting value of 0.0 for return value from the following function no matter what you put in for i:
    Code:
    //goal of function is to implement following 
    //formula d = ((i + 3) * (i + 3))/2;
     
    double changeInput(int i)
    {
       double d = 0.0;
       i += 3;
       i *= i;
       i /= 2;
       return d;
    }
       
     
    //add cout statements to help debug code
    double changeInput(int i)
    {
       //did I receive the correct input?
       cout << "i = " << i << endl;
       //pause program to evaluate result
       cin.get();
       
       //declare return type
       double d;
       i += 3;
       //did the code add 3 to the initial value?
       cout << "i += 3 = " << i << endl;
       cin.get();
       
       i *= i;
       //did the code multiply i by itself
       cout << "i * i  = " << i << endl;
       cin.get();
     
      i = i / 2;
      //did the code divide current result by 2
      cout << " d = " << d << endl;
      cin.get();
     
      return d
    }
    I know it's a mistake you can see right away without aid of debugging statements, but for illustrative purposes it's an example that isn't difficult to write.

    Process--run program with debug code passing known value of i. Follow values of i and d through each step to find step with error. Fix error. Remove debugging code. Move on.

    Oh, the std:: in front of cout is just the namespace where cout can be found, if that's what you don't understand.

  12. #27
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Personally I'd use std::cerr instead of std::cout for debugging.
    Two reasons:
    1) Allows you to use redirection on the input and output to files so that you get just the errors and not all the normal program garbage. Also you could redirect stderr to another file so that you have a record of just the errors.
    2) Thats whats its there for. Doesn't it make more sense to put the errors to standard error then standard out
    Just have to make sure you are descriptive enough in the messages. Such as "In function soandso, line XY, trying to execute command: "Blah blah blah". And most important flush the buffer immediatly. Use either std::flush or std::endl.

    I use the output method of debugging a lot in C++ code since there can be so many function calls its hard at times to keep track of exactly where you are and what you are doing.

  13. #28
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    **EDIT**
    Also, if you're having problems, there are two easy ways to debug it yourself:
    1) Use a debugger and step through the code line by line, checking that the values are what they should be at each step of the program
    2) Use tons of std::cout << statements to make sure that everything is what it should be. If you did this, it would have been fairly obvious to see that the string does indeed get emptied, and that only the size reading is having problems; and from there, you could trace the program flow and see why the size isn't getting reset.

    Those are my favorite methods of debugging Also, I like to compile as I go.. testing out small algorthms before actually writing up an entire program and then trying to compile it all at once at the end.
    Last edited by The Brain; 10-07-2004 at 05:16 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  14. #29
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>1) Allows you to use redirection on the input and output to files
    I've actually been wondering about that. Is that where you do "prog.exe > somefile.txt" on the command line or something?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #30
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yeah
    > redirects standard output
    < redirects standard input
    ?> redirects standard error ( think thats the right symbol)

    Using file redirection for input is a really nice tool for debugging as you should be able to predict the results for the given input and can easily use the same set of input to test until the program works. But if you have a bunch of prompts for the input then your output will be mangled with the error messages.
    But if you are using cerr for error messages and cout for normal messages:
    program.exe <input.txt >output.txt ?>errors.txt
    nicely seperates them.

Page 2 of 3 FirstFirst 123 LastLast
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, 09: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, 05:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01: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

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