Tuesday, 13 March 2012

Problem Getline is solved

Getline function sometimes runs badly i mean it's work useless for us so i can help you about getline's useless

/***************************************************************************** 

   using getline function/getline fonksiyonun istenildiği gibi kullanılması

******************************************************************************/
#include<string>
#include<iostream>

using namespace std;

void main(){
string film_name,country;
bool oscar;
int duration,number_of_actors;
double imdb_rating;
string getliner;

cout << "Please enter film's name : " << "\n";
cin.ignore(0,'\n'); //not read space and nextline character
getline(cin,film_name);

cout  << "Please enter film's year : "<< endl;
cin >> year;

cout  << "Please enter film's duration : "<< endl;
cin >> duration;

cout  << "Please enter number of actors : "<< endl;
cin >> number_of_actors;
getline(cin,getliner);

cout  << "Please enter film's country : "<< "\n";
cin.ignore(0,'\n');  //not read space and nextline character
getline(cin,country);

cout  << "Does the film have Oscar 0 or 1 : "<< endl;
cin >> oscar;

cout  << "Please enter imdb rating of the film : "<< endl;
cin >> imdb_rating;
getline(cin,getliner);

cout << "Film added" << endl;

cin.clear();
}

3 comments:

  1. the program work as long as there is no loop.
    what of such case


    #include
    #include
    #define nb 3
    using namespace std;
    struct book
    {
    string name;
    string author;
    float price;
    };
    int main()
    {
    book cpp[nb];
    int i;
    cout<<"This program capture books details and display all books\n";
    cout<<"--------------------------------------------------------\n";
    for(i=0;i>cpp[i].price ;
    cin.clear();
    cout<<endl<<endl;
    }

    cout<<" Book Details \n";
    cout<<" -----------------------------------\n";
    cout<<" Title Author Price \n";
    cout<<" -----------------------------------\n";
    for(i=0;i<nb;i++)
    {
    cout<<cpp[i].name<<" "<<cpp[i].author<<" "<<cpp[i].price <<endl;
    }
    system("pause");
    return 0;
    }

    ReplyDelete
  2. The program above work when a dummy variable is introduced to get rid of trailing newline character left by cin

    this a sample
    #include
    #include
    #define nb 2
    using namespace std;
    struct book
    {
    string name;
    string author;
    float price;
    };
    int main()
    {
    book cpp[nb];
    string dummy;
    int i;
    cout<<"This program capture books details and display all books\n";
    cout<<"--------------------------------------------------------\n";
    for(i=0;i>cpp[i].price;
    getline(cin,dummy);
    cout<<endl<<endl;

    }

    cout<<" Book Details \n";
    cout<<" -----------------------------------\n";
    cout<<" Title Author Price \n";
    cout<<" -----------------------------------\n";
    for(i=0;i<nb;i++)
    {
    cout<<cpp[i].name<<" "<<cpp[i].author<<" "<<cpp[i].price <<endl;
    }
    system("pause");
    return 0;
    }

    ReplyDelete
  3. i think ,u should look again this line "for(i=0;i>cpp[i].price)". Something running wrongly like ">" or "<" if you rewrite code here clearly, i can help u better.

    ReplyDelete