I am trying to count the number of words the user inputs and print out the input and show how many words were used, there are multiple problems that are mentioned in the tabs.
Printable View
I am trying to count the number of words the user inputs and print out the input and show how many words were used, there are multiple problems that are mentioned in the tabs.
Code:#include
Code:<cstdlib>
#include
<iostream>
#include
<string>
using
namespace std;
int
main(int argc, char *argv[])
{
int nos = 0; // Number of strings in the users input.
int nsc = 0; // Number of strings counted.
int start = 0; // Starting int
string nui =
""; // All the strings together
while (nos == start)
{
string ui;
// Users input
cout <<
"Type something" << endl; // Self explained
cin >> ui;
// Users input
++nos;
// +1 to nos
while ((nos > start) && (nos > nsc)) // while nos > nsc continue running this is where im having my problem
{
// insted of using > < is there another way to let the program know if
ui +=nui;
// + ui to nui // there is another string to be added continue running?
++nsc;
// +1 to nsc
}
}
// Prints out the final results, also this part does not print out at all at the end of the program.
cout <<
"Your string was - " << nui << " - and it had - " << nsc << " - words in it." <<
system(
"PAUSE");
return EXIT_SUCCESS;
}
I have no idea what your code is doing (it's also formatted very badly on the board, what did you do?).
However, if you want to count the number of words a user inputted, it should be as simple as counting the number of spaces. Number of spaces + 1 == number of words, wouldn't you agree?