Thread: Array (seperate inputs by comma)

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Array (seperate inputs by comma)

    I am new with C++.

    what I am attempting to do is to ask for 2 names seperated by a comma (ie. Smith, John). then cout as (John Smith). I have an idea but just have no clue how to write it...

    this is what I have so far...

    #include <stdio.h>
    #include <conio.h>
    #include <iostream.h>
    #include <string.h>

    main () {
    clrscr();

    char name[50];

    cout<<endl<<"Please enter your name (lastname, firstname): ";
    cin >>name;

    while (

    cout<<endl<<;

    cout<<endl<<"press any key to exit...";
    getch();

  2. #2
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    Why do they need to be separated by a comma? Normally what you're doing would be the other way around - ask for the first name and last name then output them as 'last, first'.

    You'd also only need <iostream> for that program (unless you were expecting anything other than alpha characters to be inputed).
    Last edited by BuzzBuzz; 04-02-2009 at 02:38 PM.
    Any help I give may be classified as:
    The Blind leading the Blind...
    Currently working through:
    "C++ Primer Plus"

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    I'm not sure but this is what was asked... I'm guessing I need to identify the comma and the space as a split point? I have no sweet clue how to do this... do you know the other way around (ask for the first name and last name then output them as 'last, first')?

  4. #4
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    The comma after the last name is what makes it a bit more difficult. If there was no comma (just a space), you could just do:

    Code:
    cin >> lastName;
    cin >> firstName;
    
    cout << firstName << " " << lastName << endl;
    If you must read it in with the comma coming after the last name, then you might have to use something like a delimiter. This will read all the characters until the delimiter is read in.

    Code:
    char lastName[256];
    cin.getline (lastName,256, ',')
    So, that should read all the characters into lastName until the comma is reached. I think the string method is something like:
    Code:
    string lastName;
    getline (cin,lastName, ',');
    Using a delimiter should work, although, there might be an easier method to implement. I'm sure another forum member will mention it if there is.
    IDE - Visual Studio 2005
    Windows XP Pro

  5. #5
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    Quote Originally Posted by chuckwag0n View Post
    I'm not sure but this is what was asked... I'm guessing I need to identify the comma and the space as a split point? I have no sweet clue how to do this... do you know the other way around (ask for the first name and last name then output them as 'last, first')?
    The "other" simpler way is a basic cin/cout program and would look like this:

    Code:
    #include <iostream>
    
    
    int main()
    {
        using namespace std;
    
        char name1[20], name2[20];
    
        cout << "Please enter your first name: ";
        cin >> name1;
        cout << "Please enter your last name: ";
        cin >> name2;
    
        cout << name2 << ", " << name1;
    
        return 0;
    }
    But if the assignment is to read the input as "Firstname, Lastname" then out from that then you'd need to take the input as a string and then manipulate it which is not the same as what is being done above. And "cin >>" does not accept ',' or ' '. What have you done so far on arrays?
    Last edited by BuzzBuzz; 04-03-2009 at 03:03 AM.
    Any help I give may be classified as:
    The Blind leading the Blind...
    Currently working through:
    "C++ Primer Plus"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Firstly, do not use char! Use proper C++ mechanics: that means use std::string.
    Code:
    #include <iostream>
    
    int main()
    {
        using namespace std;
    
        string first_name, last_name;
    
        cout << "Please enter your first name: ";
        cin >> first_name;
        cout << "Please enter your last name: ";
        cin >> last_name;
    
        cout << last_name << ", " << first_name;
    
        return 0;
    }
    chuckwag0n:
    You need to upgrade your compiler from the stone age. Try Visual Studio Express. It's free.
    Iostream.h is a header that no longer exists. Gone. Dead.
    Implicit main is not allowed in C++ either. It must have a return type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Quote Originally Posted by BuzzBuzz View Post
    But if the assignment is to read the input as "Firstname, Lastname" then out from that then you'd need to take the input as a string and then manipulate it which is not the same as what is being done above. And "cin >>" does not accept ',' or ' '.
    How does "cin >>" not accept ","?
    Example:
    Code:
    cout << "Please enter your name (last, first): " << endl;
    cin >> lastName;
    cin >> firstName;
    
    cout << lastName;
    The value for lastName will be "lastName," instead of "lastName"; the comma will be stored as part of the name. I think it would be easiest to do something like:
    Code:
    getline(cin, lastName, ',');
    cin.ignore();
    getline(cin, firstName);
    This is all assuming that the input must include the comma after the last name.
    IDE - Visual Studio 2005
    Windows XP Pro

  8. #8
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    Quote Originally Posted by Cpro View Post
    How does "cin >>" not accept ","?
    It does, that was a mistake on my part.
    Any help I give may be classified as:
    The Blind leading the Blind...
    Currently working through:
    "C++ Primer Plus"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM