![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2006
Location: Canada
Posts: 8
Rep Power: 0
![]() |
a very basic question...
ok i'm at the VERY beginning stages of learning how to program in C++. i'm studying a book on my own terms, so i don't really have anyone to guide me along.
i made an extremely simple little program that i intended to output the statement "Mr. John A Smith kicks ass!", but for some strange reason, the middle initial always comes out lower case, without spacing. I was sure that i had my head wrapped around this stage of my learning process, and was ready to move on to the next chapter, but for some reason i can't fix this problem. here's my code (i know i took unnecessary lengths to have it output the text. it's not the text that is the intended goal, it's the process) #include <iostream> #include <string> using namespace std; const string FIRSTNAME = "John"; const string LASTNAME = "Smith"; const char MIDDLE_INITIAL = 'A'; const string TITLE = "Mr"; int main() { string first; string middle; string last; string title; string fullname; first = FIRSTNAME + ' '; middle = MIDDLE_INITIAL + ' '; last = LASTNAME; title = TITLE + ". "; fullname = title + first + middle + last; cout << fullname + " kicks ass!" << endl; } |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2006
Posts: 137
Rep Power: 0
![]() |
#1: Use code tags...
It would be easier to do if you didn't take the hard way to do this... #include <iostream>
using namespace std;
int main()
{
cout << "Mr. John A Smith kicks ass!";
return 0;
}Thats all you need to do instead of what you have there. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Your problem is here, aron_h.
middle = MIDDLE_INITIAL + ' '; Try changing this to; middle = MIDDLE_INITIAL; middle += ' ' And, I second splinter9x's first comment: learn to use code tags. Read the "How to ask a question" sticky post at the top of this forum for more info. Other than that, splinter9x, you misunderstood the question. That's not a shot at you; you just didn't answer the question that was asked. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2006
Posts: 137
Rep Power: 0
![]() |
I know, all I was saying was that there was a easier approach to what he was doing. It took that program from 24 lines to 10 lines. Smaller program and easier to read/understand with the same output that he wanted.
|
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
You are mixing your types! Make "A" a one character string to make this exercise come out.
#include <iostream>
#include <string>
using namespace std;
const string FIRSTNAME = "John";
const string LASTNAME = "Smith";
const string MIDDLE_INITIAL = "A"; // one character string!!
const string TITLE = "Mr";
int main()
{
string first;
string middle;
string last;
string title;
string fullname;
first = FIRSTNAME + ' ';
middle = MIDDLE_INITIAL + ' ';
last = LASTNAME;
title = TITLE + ". ";
fullname = title + first + middle + last;
cout << fullname + " kicks ass!" << endl;
cin.get(); // wait
}[ code] your code here [/code] Removed the space between [ and c so it looks like [code] for this to work!
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jun 2006
Location: texas
Posts: 22
Rep Power: 0
![]() |
i like splinter's approach better...
|
|
|
|
|
|
#7 | |
|
Professional Programmer
|
What would happen if Mr. John A Smith changed his name? What if his wife ran the program and wanted her name to be outputted? She would have to re-compile the code to suit her needs.
I'm pretty sure the point of this little project is for the OP to understand input and output a bit more, so no, splinters way isn't really helping at all. I know you know that.
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Jun 2006
Posts: 137
Rep Power: 0
![]() |
He was just making a program to output text, not have input into the program. For simply just outputting text my way is the simplest and easiest...
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
His way is dealing with forming output from a set of variables, as one would do in outputting to a form letter. Your post is worthless as a learning exercise. You might as well recommend "Hello, World".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Jun 2006
Posts: 137
Rep Power: 0
![]() |
Not really, it outputs the same text that he wants to output. That is what he was asking about. The way I posted simply made the code simpler than what he already had.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|