Please support our C++ advertiser: Programming Forums
![]() |
•
•
Join Date: Sep 2005
Posts: 11
Reputation:
Rep Power: 4
Solved Threads: 0
I'm kinda new to c++ and can only do very simple applications.
Could someone explain why this code works:
And this doesn't:
Please help if you know any solution.
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.
•
•
Join Date: Jun 2005
Location: Novi Sad, Serbia
Posts: 273
Reputation:
Rep Power: 6
Solved Threads: 29
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++;
}
if(letter >= 'a' && letter <= 'z')
{
numletters++;
}
•
•
Join Date: Jun 2005
Posts: 16
Reputation:
Rep Power: 4
Solved Threads: 1
•
•
•
•
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.
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Rep Power: 4
Solved Threads: 4
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'.
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'.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Please suggest a very simple web design app (Site Layout and Usability)
- PCI Simple Communication Controller (Windows NT / 2000 / XP / 2003)
- need help in creating simple line editor (C++)
- Simple (I think) Code Question (PHP)
- PCI Simple Communication Controller (Windows NT / 2000 / XP / 2003)
- How do i create a simple game using c++?? (C++)
- A simple question about CMOS batteries (Motherboards, CPUs and RAM)
- A C++ Simple Address book (C++)
- Simple answer for vexing problem? (OS X)
Other Threads in the C++ Forum
- Previous Thread: Unloading a stack to a string
- Next Thread: Creating Graphs
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)





Linear Mode