Hey supra,
You started your word counting (well space counting) okay but then you lost it starting the "I'm not sure...".
Your brackets were completely wrong and your code did not compile. Here's a slightly corrected version:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
char tab[80];
int i,n,c=0,k;//c is to count the spaces
cout << "entrez une ligne de 80 characteres :";
cin.getline (tab,81,'\n');
cout << "entrez le numero du mot :";
cin >> n;
for (i=0;i<strlen(tab);i++) {
if (isspace (tab[i]))
c++;
if (n==c)//I'm not sure about this part
break;
}
for (k=i+1;k<strlen(tab);k++)
do{
cout << tab[k];
}while (isspace(tab[k] == false));
return 0;
}
There are many things to be improved in your code (it kinda works, but breaks easily):
1- Why count spaces? Counting space is inherently fragile. What if input has 2 consecutive spaces? or uses tabs for separation? It would be more interesting to count the words by detecting beginning/end of each word.
2- You break as soon as you get to your desired count. This is okay I guess, but will make it more difficult for you to add more features to your code in the long run. One of the best advice I could give you is to try to make your code as re-usable as possible. I suggest you try creating an array with one word/index.
3-
for (k=i+1;k<strlen(tab);k++)
do{
cout << tab[k];
}while (isspace(tab[k] == false));
return 0;
}
You're using a
while loop, when you clearly mean to use an
if conditional. I urge you to take a look at the definition of while and do/while loops one more time.
That should help you come up with a better solution. Be patient while working on it, it's normal that it doesn't come easy at first.
Also, I did not comment on the style of your writing (because I'm not a C++ programmer), but one thing I can tell you is to
indentcorrectly. You would've detected the problem with the brackets right away.
Don't hesitate to ask if something's not clear.