separate cin values with : colon

HI,i am beginner in c++,i got stuck trying to separate values entered with :.
for eg:

cout << "Pls enter name\n";
cin >> Name;
data << Name;

cout << "Pls enter age\n";
cin >> age;
data << age;

i would need the output to be John:34
do i need getline()?

thanks!
yes, use getline like this:
1
2
string Name;
getline ( cin, Name, ':' ); // reads from the beginning of the line until the first colon 

See
http://www.cplusplus.com/forum/articles/6046/
http://www.cplusplus.com/articles/numb_to_text/
If you need to read the age into numeric variable with getline
hi,thanks for the reply.however when i run the terminal,when i entered the name and press enter,it just entered to next line and how prompt for enter age..
please post your exact code and use the source code tag to the right in the format options. is this what your trying to do ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::cin;
using std::string;

int main(int argc, char **argv)
{
    cout << "Enter you name : ";
    string name;
    cin >> name;

    cout << "Enter your age : ";
    int age;
    cin >> age;

    cout << name << ":" << age;

    return 0;
}

Last edited on
Well,i am trying to store cin data entered into a text file..each data entered will be separated by a :

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
string ItemDesc,DatePurchased;
fstream data;

data.open ("data.txt");
cout << "Pls enter item description\n";
cin >> ItemDesc;
data << ItemDesc;
cout << "Pls enter Date purchased\n";
cin >> DatePurchased;
data << DatePurchased;

so my code is like in data.txt will display cheese:20jan2011
Topic archived. No new replies allowed.