0

This program is suppose to grab the first letter of each name and then output the initials.

#include <iostream>
using namespace std;
const char blank= ' ';
const char newline= '\n';

void main()
{
    //Declare character variables
    char firstInitial, middleInitial, lastInitial;

cout<<" Please enter first, middle, and last name separated by spaces "<< newline;

cin.get(firstInitial); 
cin.ignore(100,blank);

cin.get(middleInitial) ;
cin.ignore(100,blank);

cin.get(lastInitial); 
cin.ignore(100,blank);

cout << "The initials are: "<< firstInitial, middleInitial, lastInitial;

system("pause");

}

Edited by Dani: Formatting fixed

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by edwalk
1

thats a great story, but you are more likely to get help if you say what is doing aswell as what it is supposed to do.

0

In my opinion, it would be much easier to think of the each name as separate character arrays. Take each name in separately and print the 0th element of each.

#include <iostream>
#define MAX 20
using namespace std;

int main()
{
    char first[MAX];
    char last[MAX];
    char mid[MAX];
    
    cout<<"enter first name"<<endl;
    cin>>first;
    cout<<first[0];

     return 0;
}

That should be able to get you on the right track.

EDIT: If you need to use the get and ignore functions, sorry to have wasted both of our time.

Edited by Grn Xtrm: n/a

0

Thank you all for the help.

#include <iostream>
#include <string>
using namespace std;


void main()
{
    char blank = ' ', newline = '\n', firstInitial, middleInitial, lastInitial;
    string first, middle, last;

    cout << "PLease enter first, middle, and last" << endl;

    cin.get(firstInitial);
    cin.putback(firstInitial);
    cin>>first;  
    cin.ignore(100,blank);  

    cin.get(middleInitial);
    cin.putback(middleInitial);
    cin>>middle;
    cin.ignore(100,blank);

    cin.get(lastInitial);
    cin.putback(lastInitial);
    cin>>last;
    cout<<"your name is :"<< first << middle<< last<<endl;
    cout << "Your Initials are " << firstInitial << middleInitial << lastInitial << endl;

    system("pause");

}

Edited by Dani: Formatting fixed

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.