i need a function where i can find the space. this is what i have. but when i compile it it shows no errors but when i enter something it shows nothing can someone tell me why
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main()
{
char inj[15];
int p;
cin >> inj;
for (p=0;p<15;p++)
{
if(inj[p]==' ')
{
cout << p << endl;
}
}
system("pause");
return 0;
}
This is because the cin>> operator only gives you everything in that stream up to the first whitespace. This lets you do things like:
1 2 3 4 5 6 7 8
int main()
{
int a, b, c;
cout << "Enter 3 numbers: ";
cin >> a >> b >> c;
cout << "The 3 numbers are: " << a << ", " << b << ", " << c << endl;
return 0;
}
Enter 3 numbers: 2 45 76
The three numbers are: 2, 45, 76
press any key to continue...
Instead of the cin>> operator, use the getline() function from istream like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main()
{
char inj[15];
int p;
cin.getline(inj,15);
for (p=0;p<15;p++)
{
if(inj[p]==' ')
{
cout << p << endl;
}
}
system("pause");
return 0;
}