Thread: In Need of Help!

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    27

    In Need of Help!

    Code:
    //******************************************************************
    // PrintName program
    // This program prints a name in two different formats
    //******************************************************************
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    string FIRST;    // Person's first name
    string LAST;      // Person's last name
    char   MIDDLE;        // Person's middle initial
    ifstream inPut;       // Data File
    ofstream outPut;
    int main()
    {
    	inPut.open("names.dat");
    	outPut.open ("output.dat");
    	if ( !inPut )
    	{
             cout << "Can't open the input file.";
             return 1;
        }
     
        inPut >> FIRST >> LAST >> MIDDLE;
        
        while (inPut)
        {
              string firstLast;    // Name in first-last format
              string lastFirst;    // Name in last-first format
    
              firstLast = FIRST + " " + LAST;
        
              outPut << "Name in first-last format is " << firstLast << endl;
                        
              lastFirst = LAST + ", " + FIRST + ", ";
              outPut << "Name in last-first-initial format is ";
              outPut << lastFirst << MIDDLE << '.' << endl;
              
              inPut >> FIRST >> LAST >> MIDDLE;
    }
        inPut.close();
        
        return 0;
    }
    Above is my program. The idea of it is to plug in names provided in a file, and have them outputted in Last-First, and Last-First-Middle Initial.

    Problem: Two of the names in my file (provided by my teacher) have no middle initial, and so the program reads the first letter of the next name as the other person's middle initial. How do I rectify this program so that my program does not continue on to the next line of data? Below are my input values for names.


    George Bush W
    Madhavi Gandhi D
    Dan Berk G
    Ameka Johnson P
    Rita Hayward C
    Clint Eastwood B
    Sara-Jessica Parker
    Larry Kind L
    Rani Mukharji

    This is my current output:

    Name in first-last format is George Bush
    Name in last-first-initial format is Bush, George, W.
    Name in first-last format is Madhavi Gandhi
    Name in last-first-initial format is Gandhi, Madhavi, D.
    Name in first-last format is Dan Berk
    Name in last-first-initial format is Berk, Dan, G.
    Name in first-last format is Ameka Johnson
    Name in last-first-initial format is Johnson, Ameka, P.
    Name in first-last format is Rita Hayward
    Name in last-first-initial format is Hayward, Rita, C.
    Name in first-last format is Clint Eastwood
    Name in last-first-initial format is Eastwood, Clint, B.
    Name in first-last format is Sara-Jessica Parker
    Name in last-first-initial format is Parker, Sara-Jessica, L.
    Name in first-last format is arry Kind
    Name in last-first-initial format is Kind, arry, L.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Is this what we do? Repost topics we've already been given solutions to? NO ONE IS GOING TO WRITE YOUR ASSIGNMENT FOR YOU, so don't bother.

    http://cboard.cprogramming.com/showthread.php?t=76083
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    27
    Quote Originally Posted by SlyMaelstrom
    Is this what we do? Repost topics we've already been given solutions to? NO ONE IS GOING TO WRITE YOUR ASSIGNMENT FOR YOU, so don't bother.

    http://cboard.cprogramming.com/showthread.php?t=76083
    I'm not asking for someone to do the program for me, I had asked you to please give me a little more clarification on how to use the commands you had shown.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I feel I gave you as much info as I can give you without writing it. Everything you need is right in front of you in that post. Just put the pieces together.

    It's not a new trick the "Ah... can you clarify better" post until someone eventually writes it for you. I told you how to do it, I gave you an example of how it's used. Now you have to finish it. It's homework man... obviously, your teacher thinks you should know enough to do it. So the more we have to give you, the less it says for the amount of attention or interest you gave to the class.

    ...and by the way, and this is pretty much a global message board rule... never, under any circumstances, should you have the same post twice in one day.
    Last edited by SlyMaelstrom; 02-21-2006 at 09:35 PM.
    Sent from my iPadŽ

  5. #5
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Sly, is this even legal code?

    Code:
    while(cin.get(var1) == ' ');

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    27
    OK, if I read this right, var1 should become MIDDLE, yes? but the program I'm using gives me an error of: "no match for 'operator=' in '(&std::cin)->std::basic_istream<_CharT, _Traits>::get [with _CharT = char, _Traits = std::char_traits<char>](((char&)(&MIDDLE))) = ' '' "

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by MadCow257
    Sly, is this even legal code?

    Code:
    while(cin.get(var1) == ' ');
    Ah no it isn't. I was just testing you, you see. And also doing a few other things at the time.

    Code:
    while(cin.get(var1) && var1 != ' ')
    Does that suit you better?
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    27
    I obviously don't get what is to be done, but I thank you all for your help anyway.

Popular pages Recent additions subscribe to a feed