News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

String Input gets Bypassed HELP

Started by rip111buddy, December 20, 2011, 06:29:34 PM

Previous topic - Next topic

rip111buddy

I HAVE THIS CODE....but unfortunately when entering values,  the 'Destination' string gets bypassed and jumps over to distance. PLEASE HELPPP !!!

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

using namespace std;

class flight
{
   private:
       int fno;                //flight no
       char dest[20];          //destination
       float dist;             //distance
       float fuel;             //fuel required
       void calfuel();         //calculating fuel - function
   public:
       void getdata();

       void outdata();
};

void flight::outdata()
{
   calfuel();
   cout<<"Flight No. : "<<fno;
   cout<<"\nDestination : "<<dest;
   cout<<"\nDistance : "<<dist<<" kms";
   cout<<"\nFuel required : "<<fuel<<" kgs";
}

void flight::getdata()
{
   cout<<"Enter Flight No. : ";
   cin>>fno;
   cout<<"Enter Destination : ";
   gets(dest);
   cout<<"Enter Distance : ";
   cin>>dist;
}

void flight::calfuel()
{
   if(dist<=1000)
       fuel=500;
   else if(dist>1000 && dist<=2000)
       fuel=1100;
   else if(dist>2000 && dist<=3000)
       fuel=2200;
   else if(dist>3000)
   {
       cout<<"The flight cannot travel such huge distances, try 'via' flights.\n";
       fuel=0;
   }
}

int main()
{
   flight f1;
   f1.getdata();
   cout<<"\nPress any key to continue...";
   getch();
   system("cls");
   f1.outdata();
   cout<<"\nPress any key to exit.";
   getch();
   return 0;
}


P.S - I tried even tried this :

#include <string>
string dest;         //in the class; private: section
getline(cin, dest);


OUTPUT IMAGE :


Alpha

You are both in the wrong forums and definitely the wrong subforums (notice C::B DEVELOPMENT STRICTLY!).
Try someplace like http://www.cplusplus.com/forum/

Jenna

You violate our forum rules.
When registering here, you accepted to respect the rules.

Topic locked !