Counting the number of words input

This is a discussion on Counting the number of words input within the C++ Programming forums, part of the General Programming Boards category; I am trying to count the number of words the user inputs and print out the input and show how ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    3

    Lightbulb Counting the number of words input

    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.
    Last edited by lostnthestars; 12-18-2011 at 05:43 PM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    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;
    
    }
    
    

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-05-2010, 04:04 AM
  2. Counting input words
    By Soulrivers in forum C++ Programming
    Replies: 26
    Last Post: 01-17-2008, 03:06 PM
  3. Counting Number of Words in a Text File Using C
    By wvu2005 in forum C Programming
    Replies: 16
    Last Post: 09-27-2005, 12:45 PM
  4. counting the number of words
    By ccoder01 in forum C Programming
    Replies: 3
    Last Post: 05-29-2004, 03:38 AM
  5. counting the number of words in a file
    By stuartbut in forum C Programming
    Replies: 2
    Last Post: 04-13-2002, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21