compile error (beginner)

This is a discussion on compile error (beginner) within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm pretty new to programming and am attempting to build a text formatting program to help further my ...

  1. #1
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164

    compile error (beginner)

    Hi all,

    I'm pretty new to programming and am attempting to build a text formatting program to help further my knowledge.

    Its still in the early stages, but i've come across a problem. Here is a snippet of my program. The program will count the number of spaces in a given string.

    I am getting errors related to the if statement. Can someone please point out what I am doing wrong?

    Thanks.


    Code:
    #include <iostream>
    
    using namespace std;
    
    main()
    {
        char str[20] = {"This is my string"};
        char *ptr;
        ptr = str; //point to beginning of array
        int counter=0;
        
        while (*ptr != '\0')
        {
            if (*ptr == " ") 
            {
                ++counter;
            }
            *ptr++;
        }
        
        cout << "There are " << counter << " spaces\n";
    
        system("PAUSE");
        return 0;
    }

  2. #2
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    Well by all account I'm still a n00b myself but do you really need the * infront of ptr for all those calls?

    Shouldn't you juse declare it as a pointer char *ptr and then just use ptr or &ptr for the rest?

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    > if (*ptr == " ")
    if(*ptr == ' ')


    > *ptr++;
    ptr++;


    > main()
    Should be int main() by the way.
    Last edited by Omnius; 12-30-2003 at 07:14 AM.

  4. #4
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Thank you.

    I am currently learning pointers, hence the usage of them (and fall down) in nearly every program I write.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    It takes a little while to become comfortable with pointers, but it's time well spent. Good luck

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: compile error (beginner)

    Also you mention
    Originally posted by eth0
    I am getting errors related to the if statement. Can someone please point out what I am doing wrong?
    This one happened to be easy, but be less vague about "errors". Specify what errors you are getting. It tells us exactly what the problem is.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Hi,

    Can I just clarify before I continue.

    My if statement stated if (*ptr == " ") originaly.
    Omnius stated that it should have been if(*ptr == ' ') using single quotes.

    Is the reason behind this due to the fact that double quotes will insert an \0 at the end automatically??
    If not, what was the reason?

    You'll be getting many more questions from me in the future, I ask more questions that a five year old kid
    Hopefully i'll be able to answer some in the near future.

    Thanks again.

  8. #8
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Double quotes (" ") are for strings (meaning more then 1 character). Where as single quotes (' ') are for chars (meaning only 1 character). Make sure to use int main() as well.

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  9. #9
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    ok, thanks.

    1 more thing, (told you I like asking questions) Why int main?

    My lecturer at college always tells us to use void main, however if I'm right this is no longer the ANSI standard.

    What are the reasons behind this?

    Obviously something to do with ending the main function with return 0

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    In short, void main has never been standard. End of story.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Okay, then tell your teacher to go learn C++ . Void main is a bad, bad, thing. Keep away from it because it's not standard. I don't think it's EVER been standard. Return 0, to learn about that go read a C++ book. Some will explain what it means, mine did. So if he says use void main then go ahead and use int main anyways. Void main = bad programmer, int main = good programmer.

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by SirCrono6
    Okay, then tell your teacher to go learn C++ . Void main is a bad, bad, thing. Keep away from it because it's not standard. I don't think it's EVER been standard. Return 0, to learn about that go read a C++ book. Some will explain what it means, mine did. So if he says use void main then go ahead and use int main anyways. Void main = bad programmer, int main = good programmer.
    Actually, void main() is not such a big deal. In the grand scheme of things, it generates far more heat in discussions than its status deserves. It's non-standard. There's no advantage in using it. Lots of people continue to do so, and lots of compilers continue to support it.

    As far as your teacher is concerned, I would be inclined to point out that void main() is non-standard and that main should return an int. If they still insist that you should use void main during classes you've not got much choice but to go along with it for the time being. It's no big deal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 01:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 12:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 07:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 08:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 01:58 PM

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