You've got a lot of problems with your code, but you do get bonus marks for being one of the few people who use code tags on their first post!
>#include
>#include
Those headers are nonstandard and not recommended. Try removing the ".h" from them and adding the line "using namespace std;" below them.
>#include
>#include
Old headers. Get rid of them.
>#include
This is a C header. You shouldn't need or use it either.
>void main()
void main() is also nonstandard and bad. Use int main() instead.
>clrscr();
Don't clear the screen. It relies on those old headers, and annoys people like me who might actually have important data on the screen before you wiped it.
>char text1[]=" ";
You're only allocating 2 bytes (one for the space, one for the terminating null character) to hold an entire word. Nope, that's not a good idea.
Instead of using character arrays, consider using a C++ string. You don't need to worry about character allocation, and it makes your job a heck of a lot easier. If you're confident enough, you could even use string's built in searching functions to do the tokenizing instead of implementing your own.
>getch();
This relies on the nonstandard conio.h header. Try using getchar(); instead.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Or better yet: use std::strings and don't worry about dynamicly allocating memory at all.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
If you have to implement your own algorithm to do this, try taking a look at a snippet I made some time ago. Link
But otherwise, this is an easy problem, you could even try std::stringstream . You practically don't have to do anything :icon_lol:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string sentence_s;
stringstream sentence_ss;
cout << "Enter a sentence: ";
getline(cin, sentence_s);
sentence_ss << sentence_s;
string word;
while ( sentence_ss >> word )
cout << '\n' << word;
cin.ignore();
}
If you can't do something like this, try using strtok , it will help you split up the word using a delimiter.
Hope this helps.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129