Help with this assignment please!

*I don't know how to use peek or putback! (no loops allowed)

Write a C++ program that prompts the user to enter their full name (first name, middle name or middle initial and then last name) each separated by a whitespace.

Your program will then output their name, as well as their initials, to the standard output screen.

Your program MUST utilize the following member functions for the cin object:
peek, get, putback and ignore. You may use these functions more than once BUT YOU MUST USE ALL OF THEM AT LEAST ONCE.

Your program is NOT allowed to have more than the following six variable declaration types:
-­‐three variables of type string to store the three name parts
-­‐three variables of type char to store the initials

Call your source file Initials.cpp

Example:

If the input was: John W Frank
The output would be: Your name is John W Frank Your initials are JWF
I have looked at those multiple times and I think they are very vague... I still don't understand
closed account (o1vk4iN6)
Look at the example codes, they usually give a general idea of what they do. Why don't you start programming something and if you run into a problem post the code along with a question and someone will try to help.

Are you sure no loops are allowed ? These member functions (for this task anyways) would be optimally used in a loop, I can't see them not being used in at least one loop.
This is what I have so far:

String = firstName
String = middleName
String = lastName
Char = ch1
Char = ch2
Char = ch3

Cout << “Please enter your full name.” << endl;
Cin >> firstname >> middleName >> lastName;

Cin.get(ch1);
Cin.ignore(100,’ ‘);

Cin.get(ch2);
Cin.ignore(100,’ ‘);

Cin.get(ch3);

Cout << “Your full name is “ <<firstName << middleName << lastName << endl;
Cout<< “Your initials are” << ch1 << ch2 << ch3 << endl;


The problem is that I can't figure out how to use peek or putback. Also, I can't figure out how to make it get the first character of each word and the entire word for outputting the entire name. For example, if I type in John A Doe, it will give me JAD but it can't tell me that the name is John A Doe.
Why in the world is you instructor making you use peek and putback when this scenario doesn't call for them?

It's like he is forcing you to use them when they aren't even required for the task which is not cool imo.

I honestly never really used peek or putback because when I need strict input detection I just read char by char.


EDIT - This should work but it doesn't use peek or putback so you will need to modify it. I kept it as simple as possible since it is apparent that you are just beginning C++.

For reference, getline gets all chars until the newline character or until a certain char is found if your provide a third argument.

Strings can be accessed like arrays so someString[0] accesses the first letter of the string.

Good luck!

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

int main()
{
	char fInitial, mInitial, lInitial;
	string fName, mName, lName;
	
	cout << "Enter your name" << endl;
	
        // get all chars until a whitespace delimiter is found
        // if they only entered one name this will hang
	getline(cin, fName, ' ');
	fInitial = fName[0];
	
	// same as above
        // if they only entered two names this will hang
	getline(cin, mName, ' ');
	mInitial = mName[0];

	// gets all chars until a newline is found because it's
        // supposed to be the last word and so unless they actually
        // press the spacebar at the end a whitepace will not show up 
	getline(cin, lName, '\n');
	lInitial = lName[0];	

         cout << "Your name is " << fName  << " " << mName << " " << lName
         << " your initals are " << fInitial << mInitial << lInitial << endl; 

	return 0;
}



Last edited on
Regardless of whether you know or don't know how to use cin member function, you really need to go back a few lessons and review the basics. Your code is rife with errors:

String change to string

...

String = firstName change to string firstName

...

Cin change to cin

...


For example, if I type in John A Doe, it will give me JAD but it can't tell me that the name is John A Doe.

You obviously haven't compiled your own code. Please go back to the beginning of your lectures or notes or whatever, then move to some basic iostream, and tell your class mate to do so too...

http://www.cplusplus.com/forum/beginner/74003/



Last edited on
When I was doing things like this in school, I wrote a comment above what the line should be and just used the teachers recommended way...
What is an equivalent to getline(cin,fName,' ')?
How could you make something exactly like that outcome but using other functions such as get or ignore?
You'd have to make your own using a loop and such. It shouldn't be too hard.
Topic archived. No new replies allowed.