473,729 Members | 2,235 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Beginner help!

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????

Feb 5 '06 #1
33 2402
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 5 '06 #2
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;

}

Feb 5 '06 #3

"aaron" <aa***@yadavis. com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
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.
Feb 5 '06 #4
Thank you Jim
That does work... but is this an array?
I have been instructed not to use arrays.

Feb 5 '06 #5
aaron wrote:
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
Feb 5 '06 #6
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 5 '06 #7
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.

Feb 5 '06 #8
"aaron" <aa***@yadavis. com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
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?
Feb 5 '06 #9

aaron wrote:
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.

Feb 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2871
by: Art | last post by:
NEWBIE ALERT! Esteemed List Participants and Lurkers: (System: P-II 350, 192 meg, Win98 SE, Python 2.2.3, wxPythonWIN32-2.4.1.2-Py22.exe) I'm having a lot of fun getting started with Python ... it is the most elegant and graceful language I have ever used (Fortran, Cobol, Basic, many assemblers, Forth, C, VB, etc.). I don't have the resources or the
3
2986
by: jvax | last post by:
Hi all, I hope I'm posting in the right NG... I have a data text file I want to read from a c++ program. the data file goes like this: 90 # number of balls 33 42 13
8
2378
by: Grrrbau | last post by:
I'm a beginner. I'm looking for a good C++ book. Someone told me about Lafore's "Object-Oriented Programming in C++". What do you think? Grrrbau
1
2868
by: LRW | last post by:
I was wondering if anyone could recommend some good beginner sites and tutorial sites for writting ASP.Net pages in C#. Things that especially help with datagrids!! And, are there additional newsgroups that focus on C# and ASP.Net? Thanks!! Liam
14
2291
by: z_learning_tester | last post by:
But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string? The example is usually something like: Turn this string "1,2,3,4,..." Into "...4,3,2,1" This one seems hard enough let alone trying to turn a string of space-seperated words around(is that even possible? a trick question
3
2190
by: William Foster | last post by:
Good evening all, Microsoft is really starting to annoy me as a new user. I am trying to convert my code from VBA (A very user friendly laguage with generally good help files) to Visual Studio 2005 Visual Basic (A poorly documented language). I am unable to find help for Visual Basic that tells me how to show the Browse Files/Folders dialog box in this system; help shows me 50,000,001 different articles but none of them relate to me,...
10
2423
by: See_Red_Run | last post by:
Hi, I am trying to figure out how to get started with PHP/MySQL. Everything I've read so far says to start with PHP first. I was expecting something like Visual Basic Express or some other type of free IDE. So I discovered that I needed to download a virtual server, so I downloaded OmniSecure and followed the set up instructions as far as I could figure them out. So here is where I'm stuck. 1) While trying to set up and configure...
1
1803
by: Blue_hatter | last post by:
Hey Guys, I'm a newbie to the whole C++ Programming thing as I think I said before in a post. The thing is, I have this idea that might help me to learn at a better pace than I am doing currently. I realise that I learn better if I explain things as I go along. My suggestion is, I start a tutorial as I learn the concepts of the language from the beginning though I'm not exactly a beginner, I've started c++ a couple of months ago and I...
10
2150
by: hamza612 | last post by:
I want to start learning how to program. But I dont know where to start. From what I've heard so far c++ is not a good lang. to learn as a beginner because its very complicated compared to others like python, ruby etc. I would like to know if there is a prerequisite to learning any computer language, is there something I have to learn before learning any computer language, like a basic or core?
22
18149
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php programmer and looking for a starting point in regards to practical projects to work on. What are some projects that beginner programmers usually start with? Please list a few that would be good for a beginner PHP programmer to
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.