someone help me pls...The 'Add' works fine..but when i try to open it,my full name didn't
show completely , such as 'Jack Sparrow' = 'Jack'.. help me pls.

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

using namespace std;

void main()
{
    string name;
    int age;
    int options =0;

    char filename[20];

    while (options!=3)
    {



    cout << "1.Save" <<endl;
    cout << "2.Open" <<endl;
    cout << "3.EXIT" <<endl;
    cout << "Options = ";
    cin >> options;
    system("cls");

    switch(options){

case 1:
        {
    cout << "Enter Name : ";
    cin.ignore();
    getline(cin,name);
    cout << "Enter Age : ";
    cin >> age;

    cout << "\nEnter the name of the file you want to create: ";
    cin >> filename;
    ofstream Students(filename, ios::out);
    Students << name << "\n" <<  age;
    cout << "Name = " <<name <<endl;
    cout << "Age = " <<age <<endl;
        }

    break;

    case 2:
        {    
            string name;

    cout << "Enter the name of the file you want to open: ";
    cin >> filename;
    ifstream Students(filename);

    Students >> name >> age;

    cout << "\nName: " <<name;
    cout << "\nAge:  " << age;
    cout << "\n\n";
        }
        break;
    }
    }
}

Recommended Answers

All 2 Replies

use getline(Students, name); instead of Students >> name;

Thanks so much...ur great.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.