display 1st char of string

I have been trying to find a way to only display the first letter of the middle string but have been having difficulties can someone pleases help?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;
int main()
{ 
  string First, Middle, Last, Formated;
    cout << "Enter Full Name" << endl; 
    cin >> First;
    cin >> Middle;
    cin >> Last;
    Last = Last + ',' + ' ';
    First = First + ' ';
    Middle = Middle + '.';
    Formated = Last + First + Middle;
    cout << Formated << endl;
system("pause");
return 0;
}
You can access string like an array
1
2
3
4
5
6
7
8
#include <iostream>

using namespace std;
int main(){
	string middle;
	cin >> middle;
	cout << middle[0];
}


A couple other things, try not to use capital letters for your variable names, those usually imply classes.
Also, try not to use system(). Here are many ways you can avoid using it: http://www.cplusplus.com/articles/iw6AC542/
Last edited on
cool thank you
I am just wondering but is it possible to let the user put in only two names and have the program behave the same way and just output the last and first name. I was thinking an If statement but im not sure how to word it.
There are probably several ways, here is one.

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
47
48
49
50
51
52
53
54
55
56

#include <iomanip>      // library to set up leading 0s.
#include <unistd.h>     // library to let me delay
#include <fstream>		// for ifstream
#include <iostream>		// for cin, cout and cerr
#include <string>		// for the string datatype
#include <cstdlib>		// needed for the exit function
#include <sstream>		//
#include <math.h>		//
#include <stdio.h>		//
#include <time.h>		//
#include <conio.h>		//
#include <dos.h>		//
#include <stdlib.h>		//
#include <time.h>		//
#include <windows.h>	//
#include <sys/stat.h>	//
#include <cstring>		//
#include <winsock.h>    //
#include <ctype.h>
using namespace std;	//


int main ()
{
  char c;
  int i=0;
//  char str[]="First Middle Last";
  char str[]="First Last";
  int spacecount=0;
  
  while (str[i])
  {
    c=str[i];
    if (isspace(c)) 
	{
	spacecount++;
	}
    i++;
  }
  
  if (spacecount==2)
  {
	cout << "Show\tFirst Middle Last" << endl;	
  }
  else if (spacecount==1)
  {
  	cout << "Show\tFirst Last" << endl;	
  }

// cout << spacecount << endl;

  return 0;
}

Topic archived. No new replies allowed.