Thread: Utter beginner question about using notepad for c++

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    Unhappy Utter beginner question about using notepad for c++

    Hi all, I did try searching for this but I couldn't come with anything (I'm not even sure it is actually a problem!)

    I'm a total beginner, about a third the way through C++ A beginners guide by Herbert Schildt, and thats the sum total of my programming experience!

    My problem is that when coding the examples that are in the book, ocassionaly I'll get a compile time error and for the life of me a) not be able to match the debug error to any obvious problem in my program AND b) be able to see NO difference between my program and the example text in the book.

    Up till now I've put this down to my being too tired to see the spelling mistake or missing semi-colon or whatever but today I came across one that really ........ed me off because the program was only about 12 lines long and was i.d.e.n.t.i.c.a.l. (or so I thought) to the example from the book. I went so far as to download the code from the osbourne books website so I could sit them side by side and compare them and as far as Im concerned, there was no visual difference. However, when i checked the total number of characters in each, mine was off by one character, and the debugger mentioned a missing semi-colon, but the number of semi-colons was identical (I hope you can see my gradual spiral into insanity) .

    Anyyyyway, I tried compiling the downloaded code and obviously it went through first time no errors, I cried, I raged.... Then I opened MY notepad file in visual basic (which I dont use normally) and noticed that the 'void' in 'void strInvertCase(char *str)' wasnt highlighted in blue where it should have been. I didn't really understand why that would be the case so just for fun i deleted and rewrote the 'void' and lo and behold it suddenly worked.

    So my question is basically; what the hell happened?!? Was there some formatting preventing it from compiling (I didnt even think this was possible with notepad!)?! I repeat I'm a complete newbie at programming but even so this has driven me mad. The only thing I can think of that makes sense with what I know so far, is that I spelled void incorrectly......but... but...i CHECKED! I checked SO thoroughly! Any help would be greatly appreciated and tips on avoiding this in future would be great, for instance, should i just get used to using visual basic so I can see "problems" like this more readily?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Post your code.

    2. Throw away your Schildt books. In a discipline which demands technical accuracy, Schildt's "easy reading" misses the point on so many levels. Also, where the code does work, it's full of implementation specific crud which won't work with any other compiler.
    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
    Join Date
    Aug 2008
    Posts
    3

    re:

    Code:
    #include <iostream>
    #include <cstring>
    #include <cctype>
    using namespace std;
    
    void strInvertCase(char *str);
    
    int main()
    {
      char str[80];
    	
      strcpy(str, "This Is A Test");
    
      strInvertCase(str);
    
      cout << str; 
      
      return 0;
    
    }
    
    // Invert the case of the letters within a string. 
    Void strInvertCase(char *str)
    {
      while(*str) {
    
        //invert case
        if(isupper(*str)) *str = tolower(*str);
        else if(islower(*str)) *str = toupper(*str);
    
        str++; 
      }
    }
    Doh, got it whilst void was spelled correctly I'd put a capital 'V' that I didn't pick up on!
    Sigh, so not a problem after all! Thanks for the recommendation though! I've been tempted to get a different book for a while now, albeit not for the reason you gave (at this level I don't think I'd know enough to see that criticism for myself). I'll take a look at the stickied book thread. Thanks again!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't use notepad for coding.
    The best thing to do is get a real IDE (Code::Blocks, GCC?, Visual Studio). A free list: http://cpwiki.sf.net/IDE
    Or get Notepad++ to write the code in.
    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.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Or emacs:

    http://www.gnu.org/software/emacs/

    The advantage of emacs or notepad++ is that you can use one editor for everything:
    - your documents in Latex,
    - C/C++,
    - java,
    - xml,
    - python,
    - ...

    Emacs has more extensions and is more parametrizable than notepad++ , but might seem to be slightly more difficult to use.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    re

    Okie dokie I'll give visual basic a go seeing as it's up and ready to go allready. Thanks for the recommendation!

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Borodin View Post
    Okie dokie I'll give visual basic a go seeing as it's up and ready to go allready. Thanks for the recommendation!
    Visual Basic? Don't you mean Visual C++?

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    8
    Quote Originally Posted by Borodin View Post
    Okie dokie I'll give visual basic a go seeing as it's up and ready to go allready. Thanks for the recommendation!
    Try DevC++. It's free. Visual C++ Express is also free. If I were you, I'd give them both a try... DevC++ has some advantages in simplicity; however, Visual C++ has some enhancements that are nice--a little bit better help and some autocomplete. I prefer DevC++ for my hobby-like, amateur coding.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    ...a) not be able to match the debug error to any obvious problem in my program AND b) be able to see NO difference between my program and the example text in the book.
    Yeah... This kind of thing happens to programmers every day! And, not just when copying an example program... Sometimes you just can't "see" what's wrong. And, it can be very frustrating... You never know if it's going to take you five minutes or five hours to find a bug. You will get better at avoiding errors, spotting errors, and interpreting compiler messages. But, your programs will also get bigger and more complicated.

    The error message is usually helpful, but compilers are easily confused.... The compiler doesn't know what you are trying to do, and sometimes the error messages can be misleading. I once misplaced one "tiny" bracket (in a big program), and the compiler reported hundreds of errors... And, none of those errors said anything about a misplaced bracket!

    BTW - You are doing the right thing by typing-in the programs by hand. It's easy to gloss-over details if you simply download the examples.

    When you can't figure-out the problem from the compiler error, or visually find the problem, the best solution is usually to start "commenting-out" lines (or sections of code) 'till the compile problem goes away. That way, you can zero-in on the exact line of code that's causing the error.

    Of course, you have to know something about C++, so that you can comment-out lines without causing additional errors... You have to know which lines to comment-out first. For example, you can usually comment-out all of the lines inside a function, and you can make the function return a "dummy" value, if it's supposed to return something.

    And yes, a proper programming editor will make things easier. It will color-highlight keywords, and other "parts" of your program, and it will make indenting easier, etc. (I've got my editor set-up to color-code simicolons, brackets, and parenthesis.)

    When you are writing/developing your own programs, you should take a similar approach. But, you don't have to "comment-out" lines, you just have to add code a few lines at a time. Test compiling, and test-running every few lines of code. This will make programming a lot less frustrating... It's much easier to debug, if you know the error is in the last one or two lines you just added.

    Again, the trick is to know what's required to make the program compile and run, and how to sequence your program. (i.e. Your program won't compile or run if you just write the first five lines of code... You have to know figure-out which lines to write first, and what blanks to fill-in next, etc.)

    Professional programmers do the same thing. Of course, they won't test-compile and test-run every few lines. But they will test-out their code frequently as they develop the program.

    And, it's often useful to add some "extra" cout statements during development. It can help you to "see" what your program is doing, before it's done. You can display variables or you can, add little messages that say something like "Starting strInvertCase() function". The extra cout statements wont' help with compiler errors, but they can help you with run-time errors, and they can help to confirm that your program is "working" before it's completely done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner ASM question: jump instructions
    By geek@02 in forum Tech Board
    Replies: 2
    Last Post: 09-07-2008, 06:46 AM
  2. Beginner Classes Question
    By kbro3 in forum C++ Programming
    Replies: 9
    Last Post: 08-14-2008, 07:43 AM
  3. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  4. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  5. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM