Author Topic: String Input gets Bypassed HELP  (Read 10046 times)

rip111buddy

  • Guest
String Input gets Bypassed HELP
« on: December 20, 2011, 06:29:34 pm »
I HAVE THIS CODE....but unfortunately when entering values,  the 'Destination' string gets bypassed and jumps over to distance. PLEASE HELPPP !!!

Code: [Select]
#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 :
Code: [Select]
#include <string>
string dest;         //in the class; private: section
getline(cin, dest);

OUTPUT IMAGE :

« Last Edit: December 20, 2011, 06:39:47 pm by rip111buddy »

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: String Input gets Bypassed HELP
« Reply #1 on: December 20, 2011, 07:09:24 pm »
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/

Offline jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 7265
    • Jens' unofficial debian-repository for the Code::Blocks - IDE
Re: String Input gets Bypassed HELP
« Reply #2 on: December 20, 2011, 07:19:28 pm »
You violate our forum rules.
When registering here, you accepted to respect the rules.

Topic locked !