RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1423 | Replies: 7 | Thread Tools  Display Modes
Reply
Join Date: Sep 2005
Posts: 11
Reputation: MillStrike is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
MillStrike MillStrike is offline Offline
Newbie Poster

Need some simple help!

  #1  
Sep 12th, 2005
I'm kinda new to c++ and can only do very simple applications.
Could someone explain why this code works:
#include <iostream>
using namespace std;

int main()
{

    bool inword = 0;
    unsigned int numwords = 0;
	char *str = "this is a test string for my project";
    char letter;

    while (letter = *str++)
    {
        if(letter == ' ') 
        {
            inword = 0;
        }
        else if(inword == 0)
        {
            inword = 1;
            numwords++;
        }
    }
    cout << "Found " << numwords << " words!\n";
    system("PAUSE");
	return 0;
}

And this doesn't:
#include <iostream>
using namespace std;

int main()
{

    bool inword = 0;
    unsigned int numletters = 0;
	char *str = "this is a test string for my project";
    char letter;

    while (letter = *str++)
    {
        if(letter >= 'a' && letter <= 'z') 
        {
            inword = 0;
        }
        else if(inword == 0)
        {
            inword = 1;
            numletters++;
        }
    }
    cout << "Found " << numletters << " letters!\n";
    system("PAUSE");
	return 0;
}

Please help if you know any solution.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Novi Sad, Serbia
Posts: 273
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Rep Power: 6
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Need some simple help!

  #2  
Sep 12th, 2005
in first example counting the words at the begining of every word, in second example counting the words at following space (0x20). Try to add space after 'project'.
Reply With Quote  
Join Date: Sep 2005
Posts: 11
Reputation: MillStrike is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
MillStrike MillStrike is offline Offline
Newbie Poster

Re: Need some simple help!

  #3  
Sep 12th, 2005
I think i see what you mean, but that's not the problem. How come it can count spaces but not letters?
Reply With Quote  
Join Date: Feb 2005
Posts: 464
Reputation: winbatch is on a distinguished road 
Rep Power: 4
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Need some simple help!

  #4  
Sep 12th, 2005
Your 'else if' doesn't really make sense in the second one. If it's a-z you're obviously in a word, so no need to do that check, just increment the number of letters. (Especially since you don't even keep track of the number of words in the second example)

if(letter >= 'a' && letter <= 'z')
{
numletters++;
}
Reply With Quote  
Join Date: Sep 2005
Posts: 11
Reputation: MillStrike is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
MillStrike MillStrike is offline Offline
Newbie Poster

Re: Need some simple help!

  #5  
Sep 12th, 2005
Thank you very much for your help, it works fine without the "else if". :cheesy:
Reply With Quote  
Join Date: Sep 2005
Posts: 11
Reputation: MillStrike is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
MillStrike MillStrike is offline Offline
Newbie Poster

Re: Need some simple help!

  #6  
Sep 13th, 2005
Just one more small thing, how do I get the user to enter a line instead of declearing one in the code? Tried many things, but since i can't use "cin >> *str" I don't know what to do.

I appriciate any help.
Reply With Quote  
Join Date: Jun 2005
Posts: 16
Reputation: drock9975 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
drock9975 drock9975 is offline Offline
Newbie Poster

Re: Need some simple help!

  #7  
Sep 13th, 2005
Originally Posted by MillStrike
Just one more small thing, how do I get the user to enter a line instead of declearing one in the code? Tried many things, but since i can't use "cin >> *str" I don't know what to do.

I appriciate any help.

you need to either declare a fixed size char array... i.e. str[100] or use dynamic memory and a deep copy using strcpy or stricpy. If you use the fixed size array you can cin >> str and all will be good.

however, this line will be/is problematic:

while (letter = *str++)

you're assigning letter the character one beyond the begining of str every time without a stop condition. I may be wrong, because I'm still a novice too, but shouldn't that keep on going beyond the end of str into other memory that wasn't allocated for str? You have a loop that will end only when the computer isn't able to assign letter a value... maybe that is why it works because you are dereferencing str, but I'm not positive on that one.

while (letter != '\n')

with an update of *str++ at the end of the loop, will make it better. That will help you with the user input check, because the loop will be testing for the new line that is created when the user hits enter at the end of their string. If they just hit enter, the loop will be skipped and all will be well.

I hope some of this helped. I'm still pretty much a noob at C++ and programming in general.
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Need some simple help!

  #8  
Sep 13th, 2005
It may overwrite memory.

And if it may, then you're doing something wrong.
Pointer arithmetic should only be used when you know that the memory being pointed to is 'yours', such as in arrays and byte-order operations.
Otherwise, I think you're going to end up crashing and burning.

Edit: Of course, I'm pretty new myself, and tend to think that the sky will fall if I engage in code that only works 'sometimes'.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 5:48 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC