Beginner help!

A

aaron

I have a question in my class.. hoping to get some help
I need to create a program that will print
firstName middleName lastName
then their initials

User will input:
John Smith Doe

Output:
John
Smith
Doe
JSM

I can easily create the strings and cout the names but having trouble
finding how to print the first letter of each string
(cannot use array)
am I to use ignore function somehow?
can anyone help me????
 
A

Alf P. Steinbach

* aaron:
I have a question in my class.. hoping to get some help
I need to create a program that will print
firstName middleName lastName
then their initials

User will input:
John Smith Doe

Output:
John
Smith
Doe
JSM

I can easily create the strings and cout the names but having trouble
finding how to print the first letter of each string
(cannot use array)
am I to use ignore function somehow?
can anyone help me????

std::string, which you should be using to represent each name, has a
number of member functions you can use to extract the first character.

The 'at' member function, for example.
 
A

aaron

thank you...
Please understand this is the very beginning of a intro to comp
programming class
this at member function is never mention as of yet...

I am including string...
All I need now is to find out how to just print the first character
maybe the use of the ignore function?

#include <iostream>
#include <string>

using namespace std;

int main()
{
string first;
string middle;
string last;

cout << "Enter a name in the format First Middle Last: ";
cin >> first;
cin >> middle;
cin >> last;

cout << first << endl << middle << endl << last << endl;

return 0;

}
 
J

Jim Langston

aaron said:
thank you...
Please understand this is the very beginning of a intro to comp
programming class
this at member function is never mention as of yet...

I am including string...
All I need now is to find out how to just print the first character
maybe the use of the ignore function?

#include <iostream>
#include <string>

using namespace std;

int main()
{
string first;
string middle;
string last;

cout << "Enter a name in the format First Middle Last: ";
cin >> first;
cin >> middle;
cin >> last;

cout << first << endl << middle << endl << last << endl;

return 0;

}

Oh, if they are 3 seperate strings it's even much easier.

first[0] is the first character of the std::string first.
middle[0] is the first character of the std::string middle, etc..

so

std::cout << first[0] << middle[0] << last[0] << std::endl;

would print their initials.

It is [0] instead of [1] because in C, and C++, array elements start at 0,
not 1.
 
A

aaron

Thank you Jim
That does work... but is this an array?
I have been instructed not to use arrays.
 
B

benben

aaron said:
thank you...
Please understand this is the very beginning of a intro to comp
programming class
this at member function is never mention as of yet...

I am including string...
All I need now is to find out how to just print the first character
maybe the use of the ignore function?

#include <iostream>
#include <string>

using namespace std;

int main()
{
string first;
string middle;
string last;

cout << "Enter a name in the format First Middle Last: ";
cin >> first;
cin >> middle;
cin >> last;

cout << first << endl << middle << endl << last << endl;

Don't use std::endl in this way. Just use '\n'.
return 0;

}

Well if first is "hello" then first[0] will be 'h'.

You work out the rest.

ben
 
A

aaron

Here is the actual question I've been presented with...

Write a program that reads a person's name from the keyboard in the
format First Middle Last. It should then (1) print each of the names on
a separate line and (2) print the person's initials on the fourth line.
Assume that each person has exactly 3 names, that the first name begins
in the first position on a line (there are no leading blanks) and that
the names are separated from each other by a single blank.

Do not use arrays in this assignment.
 
A

Alf P. Steinbach

* benben:
Well if first is "hello" then first[0] will be 'h'.

I recommend the at function, first.at(0), as mentioned.

Especially for a beginner.

To the OP: no, std::string is not an array; using std::string is
probably what your instructor meant by no arrays.
 
J

Jim Langston

aaron said:
Thank you Jim
That does work... but is this an array?
I have been instructed not to use arrays.

You are correct. std::string is not an array. But, the same syntax used
for char arrays to get to the individual characters is the same syntax used
for std::string to get to individual characters. Which is the array element
syntax of [] and std::string actually stores it's data in a char pointer it
gets to internally using arrays.

When you use [] to get to an element, it is 0 bound instead of 1 bound for
the low value.

And what do you mean it doesn't work? Did you try it?
 
R

roberts.noah

aaron said:
Here is the actual question I've been presented with...

Write a program that reads a person's name from the keyboard in the
format First Middle Last. It should then (1) print each of the names on
a separate line and (2) print the person's initials on the fourth line.
Assume that each person has exactly 3 names, that the first name begins
in the first position on a line (there are no leading blanks) and that
the names are separated from each other by a single blank.

Do not use arrays in this assignment.

Have you told your instructor that you do not understand the material
in his lectures and the book well enough to do this assignment? If you
need help you should be asking your instructor. Hire a tutor if you
need to; this service is quite common in college and in fact that is
how I fed myself during those years. The assignment is quite easy and
your inability to figure it out indicates that you don't understand the
material in the class and reading materials either because the concepts
are difficult for you (this is pretty normal actually because
programming requires a certain way of thinking that people are not used
to) or you didn't listen/read. If the former, you really need to get a
tutor and/or approach your instructor because it only gets worse (my
experience indicates at least half of the students drop out of any
introductory programming class after midterms), if the later then read
the damn book.
 
A

aaron

I'll respond...but just to let you know, you're not helping
- I have emailed my instructor... he hasn't responded yet
- Once I reach the point of understanding then this assignment will be
easy (for me)
 
L

Luke Meyers

aaron said:
That does work... but is this an array?

No, but the same convention (called "zero-indexing") typically applies
to other data types supporting the subscript operator (operator[]), and
to indexed collections in general within all of computer science. We
like zero.
I have been instructed not to use arrays.

Make sure to thank your instructor for this. Where is this being
taught?

Luke

P.S. Not that you shouldn't know how to use arrays; they just shouldn't
be taught until later.
 
T

Tom

Here is the actual question I've been presented with...

Write a program that reads a person's name from the keyboard in the
format First Middle Last. It should then (1) print each of the names on
a separate line and (2) print the person's initials on the fourth line.
Assume that each person has exactly 3 names, that the first name begins
in the first position on a line (there are no leading blanks) and that
the names are separated from each other by a single blank.

Do not use arrays in this assignment.

I suggest to do exactly as the assignment calls.

Strings are arrays as far as I am concerned.

I won't write your code for you because you need to learn by
doing/playing with it.

The following is sloppy psuedo code, but should get you on the way
without using arrays or strings.

So ....

Read a character.
Store First Initial.
print character
while(character != ' ')
{
read a character;
print a character;
}
print line return
Read a character.
Store Second Initial.
print character
while(character != ' ')
{
read a character;
print a character;
}
print line return
Read a character.
Store Third Initial.
print character
while(character != ' ')
{
read a character;
print a character;
}
print line return
print 1'st, 2'nd, 3'rd initials

-----------------

I hope this is helpful.
If I was the instructor and you used strings... zero credit.

-- Tom
 
R

roberts.noah

aaron said:
I'll respond...but just to let you know, you're not helping

Time will tell, but I don't think you can be helped.
- I have emailed my instructor... he hasn't responded yet

In the mean time why not try writing it? Here, I'll get you started:

int main()
{
// PROGRAM GOES HERE.
}
- Once I reach the point of understanding then this assignment will be
easy (for me)

I've got a lot of experience helping people learn programming and I can
tell you without a doubt that it is the ones that try to do it that
make it. The ones that just ask what to do as soon as they get the
problem just don't make it. Part of learning how to program is
learning how to answer that question.

When I was tutoring if the student didn't have something to show they
tried I dropped them; it's just a waste of my time.
 
B

Bo Persson

Alf P. Steinbach said:
* benben:
Well if first is "hello" then first[0] will be 'h'.

I recommend the at function, first.at(0), as mentioned.

Especially for a beginner.

We should perhaps mention the difference, error checking. The at()
function verifies that the parameter is within the current size of the
string. The [] operator does not.

Using first.at(1000) will get you an out_of_range error. If you use
first[1000], we don't know what will happen, because that is
explicitly not defined by the standard. It just runs ahead without
looking!

The slight advantage of using [] is that not checking the index saves
you some (very small) amount of time. If your program runs millions of
times a day, that might be worth the risk!
To the OP: no, std::string is not an array; using std::string is
probably what your instructor meant by no arrays.

Agree.


Bo Persson
 
T

Thomas J. Gritzan

In the mean time why not try writing it? Here, I'll get you started:

int main()
{
// PROGRAM GOES HERE.
}

This seems to be a misunderstanding. The OP did already started to
program, but just posted his assignment. Since he did not quote the
context, you think he was asking for doing his homework.

@aaron:

Please, http://learn.to/quote !

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top