GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Saturday, March 12, 2011

CPP - Input Prompt

For get some limited of cin, use:
string name;
cin.width(10);  // limits the name just for 10 characters
cin >> name;

To get some characters, use
char name[50];
cin.get(name, 50);
cout << name << endl;

Or use getline()
char name[50];
cin.getline(name, 50);
cout << name << endl;

Here's a sample of the works:
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char* argv[]){
    system("color 2f");
   
    typedef struct{
        char name[20];
        char city[20];
        int age;
    }data;

    data you;
    cout << "Enter your personal information below: " << endl;
    cout << "Enter your name: ";
    cin.getline(you.name, 20);
    cout << "Enter your city: ";
    cin.getline(you.city, 20);
    cout << "Enter your age: ";
    cin >> you.age;
    cout << "\nYour personal information is like this one: " << endl;
    cout << "Name: " << you.name << endl;
    cout << "City: " << you.city << endl;
    cout << "Age : " << you.age << " years old" << endl;

    return 0;
}

Share/Bookmark

No comments:

Post a Comment