Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 9th, 2006, 1:33 AM   #1
aron_h
Newbie
 
Join Date: Jun 2006
Location: Canada
Posts: 8
Rep Power: 0 aron_h is on a distinguished road
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;
}
aron_h is offline   Reply With Quote
Old Jun 9th, 2006, 2:35 AM   #2
splinter9x
Hobbyist Programmer
 
splinter9x's Avatar
 
Join Date: Jun 2006
Posts: 137
Rep Power: 0 splinter9x is an unknown quantity at this point
#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.
__________________
Visit my Blog
I support WINDOWS...
splinter9x is offline   Reply With Quote
Old Jun 9th, 2006, 3:07 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Your problem is here, aron_h.

middle = MIDDLE_INITIAL + ' ';
MIDDLE_INITIAL is a char 'A', not a string. MIDDLE_INITIAL + ' ' therefore evaluates to 'A' + ' '. With an ASCII character set, 'A' has an integral value 65 and ' ' has a value of 32. So, 'A' + ' ' expands to 97, which happens to the code for lower case 'a'. That single character value is then placed into middle.

Try changing this to;
 middle = MIDDLE_INITIAL;
 middle += ' '
The reason this works differently is that operations on strings work differently than they do on basic types like char (+ and += on strings append, rather than doing numerical or integral addition), and - as you have it in your code - the chars are added before assigning the result to 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.
grumpy is offline   Reply With Quote
Old Jun 9th, 2006, 4:09 AM   #4
splinter9x
Hobbyist Programmer
 
splinter9x's Avatar
 
Join Date: Jun 2006
Posts: 137
Rep Power: 0 splinter9x is an unknown quantity at this point
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.
__________________
Visit my Blog
I support WINDOWS...
splinter9x is offline   Reply With Quote
Old Jun 9th, 2006, 8:31 AM   #5
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

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
}
Please put your code in code tags
[ 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!
Dietrich is offline   Reply With Quote
Old Jun 9th, 2006, 5:21 PM   #6
imagikricei
Newbie
 
imagikricei's Avatar
 
Join Date: Jun 2006
Location: texas
Posts: 22
Rep Power: 0 imagikricei is on a distinguished road
i like splinter's approach better...
imagikricei is offline   Reply With Quote
Old Jun 9th, 2006, 5:30 PM   #7
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
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:
Originally Posted by Mohamed Jihad
Durka durka!
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!
jayme is offline   Reply With Quote
Old Jun 9th, 2006, 5:42 PM   #8
splinter9x
Hobbyist Programmer
 
splinter9x's Avatar
 
Join Date: Jun 2006
Posts: 137
Rep Power: 0 splinter9x is an unknown quantity at this point
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...
__________________
Visit my Blog
I support WINDOWS...
splinter9x is offline   Reply With Quote
Old Jun 9th, 2006, 5:57 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 9th, 2006, 6:00 PM   #10
splinter9x
Hobbyist Programmer
 
splinter9x's Avatar
 
Join Date: Jun 2006
Posts: 137
Rep Power: 0 splinter9x is an unknown quantity at this point
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.
__________________
Visit my Blog
I support WINDOWS...
splinter9x is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:29 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC