I'm completely new to the world of programming Please Help!!

Hello all!! I have to write a program for my class that not only recognizes the input of the user, but also counts the white space. The assignment is: I'm supposed to prompt the user to enter in their name. Usually first, middle, and last. However, if they wish to enter JUST their first name, they can. Along with JUST their first and last. I have it just where they enter their name and it displays their first, middle, and last. However, I'm at a loss for just the first, and just the first and last. Along with counting the spaces the name took up. This is my program:

#include <iostream>
#include <string>

void newLine();

int main()
{
using namespace std;

string firstName, middleName, lastName;

cout << "Please enter your full name: \n";
cin >> firstName >> middleName >> lastName;

cout << "First name: " << firstName << endl;
cout << "Middle name: " << middleName << endl;
cout << "Last name: " << lastName << endl;

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::cout << "enter your name: first middle last / first last / first\n"
              << "end input with a new line (enter)\n" ;

    std::string input ;
    // http://www.cplusplus.com/reference/string/string/getline/
    std::getline( std::cin, input ) ; // read a full line as the input
    std::cout << "input: '" << input << "'\n" ;

    // create a string stream to read from the characters in the input string
    // https://latedev.wordpress.com/2011/11/16/c-stringstreams/
    std::istringstream stm(input) ;

    // read up to three possible parts of name
    std::string first, second, third ;
    stm >> first >> second >> third ;

    // if there is not even one part (first is empty),
    // or if can read a fourth part (stm >> fourth), the input is bad
    std::string fourth ;
    if( first.empty() || stm >> fourth ) std::cout << "bad input\n" ;

    else
    {
        std::cout << "First name: " << first << '\n' ; // the first part is the first name

        if( !second.empty() ) // if there is a second part
        {
            // if there is no third part, the second part is the last name
            if( third.empty() ) std::cout << "Last name: " << second << '\n' ;

            // otherwise, the second part is the middle name, the third part is the last name
            else std::cout << "Middle name: " << second << "\nLast name: " << third << '\n' ;
        }

        // compute the number of white spaces in input and print it out
        // (number of characters in input - total number of characters in the name parts)
        const std::size_t nspaces = input.size() - ( first.size() + second.size() + third.size() ) ;
        std::cout << "#spaces: " << nspaces << '\n' ;
    }
}
Ok, so, it does run, WAY better than my own, and I can see why, however, it still is yet to count the spaces, including whitespace, it took to create the name. Either way, this is WAY more advanced than my own.
Use getline() to take a whole line into string input.

Create a stringstream from input and use a while loop to push_back to a vector<string>.

The size of the vector will tell you the number of items entered. Deal appropriately with the alternatives 1, 2, 3.

No idea why you want to count spaces, but you can simply add up the lengths of the strings if you want to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
   string firstName, middleName, lastName;       // These will each default to "" (blank)
   string input, part;

   cout << "Enter your name: ";
   getline( cin, input );                        // The whole lot

   stringstream ss( input );                     // Create a stream
   vector<string> items;                         
   while ( ss >> part ) items.push_back( part ); // Push separate parts successively into vector

   firstName = items[0];                         // Always true;
   if ( items.size() == 2 )
   {
      lastName = items[1];
   }
   else if ( items.size() == 3 )
   {
      middleName = items[1];
      lastName = items[2];
   }

   cout << "First name: " << firstName << '\n';
   if ( middleName != "" ) cout << "Middle name: " << middleName << '\n';
   if ( lastName != "" ) cout << "Last name: " << lastName << '\n';
}


Enter your name: Bourbaki
First name: Bourbaki

Enter your name: Leonhard Euler
First name: Leonhard
Last name: Euler

Enter your name: Carl Friedrich Gauss
First name: Carl
Middle name: Friedrich
Last name: Gauss
Last edited on
Thank you all for your help.
Topic archived. No new replies allowed.