WHT!!!!

i dont know whats wrong but i tried to to Merge both codes
the if - statement dont show ...

#include <iostream>
#include<cstdlib>
#include<stdio.h>

using namespace std;

/** Showing the introduction screen and the user maual... */
void splash(void) {
	cout << "   ********************************************************\n";
	cout << "   *                                                      *\n";
    cout << "   *            Velocity And Gear Programme ..            * \n";
    cout << "   *         By:*****\n";
    cout << "   *                                                      *\n";
	cout << "   ********************************************************\n\n";

	cout<<endl ;
	system ("PAUSE");
	system ("cls");
	cout << "****************************************************************\n";
	cout << "*               USER MANUAL                                    *\n";
    cout << "* FIRST :The programme will ask you to input the accelration   * \n";
    cout << "* SECOND : The programme will ask you to input the time        *\n";
    cout << "* THIRED : The programe will calcluate the velocity            *\n";
	cout << "****************************************************************\n\n";

    system ("PAUSE");
	system ("cls");
}
/** Showing the introduction screen and the user maual... */

double Velocity(double accel,double ntime)// to calcluate the Volcity
{
    double Velocity = accel*ntime;

    return Velocity;
}


int main ()
{
    double accel;
    double ntime;
    char choice;
    char time[8];
	double h=0, m=0, s=0;
    splash();

do
        {
            cout<<"What was your Accelaration in (km/hour)  :  ";
    cin>>accel;

    cout<<"\nWhat was your riding time in hr.min :  ";
    gets(time);

	h=(time[1]+(10*(time[0])))-528;
	m=(time[4]+(10*(time[3])))-528;
	s=(time[7]+(10*(time[6])))-528;
//------>start if for the time..
if(
        h>=0 && h <=60&&
        m>=0 && m <=60&&
        s>=0 && s <=60
   )

    {

    cout<<"YOUR time is: "<<time[0]<<time[1]<<":"<<time[3]<<time[4]<<":"<<time[6]<<time[7]<<endl; //OR:

    double ntime=h+m/60+s/3600;

    cout<<"/n your time is"<<ntime;
   //-------->the if for velocity..
   Velocity(accel,ntime);
        if (Velocity(accel,ntime)>=0 && Velocity(accel,ntime)<4)
        {
            cout<<"\nyour Velocity was    "<<Velocity<<endl;
            cout<<" YOUR GEAR IS : 1";
        }
        else if (Velocity(accel,ntime) >=4 && Velocity(accel,ntime)<5)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,ntime)<<endl;
           cout<<" YOUR GEAR IS : 2";
        }
         else if (Velocity(accel,ntime) >=5 && Velocity(accel,ntime)<7)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,ntime)<<endl;
           cout<<" YOUR GEAR IS : 3";
        }
         else if (Velocity(accel,ntime) >=7 && Velocity(accel,ntime)<10)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,ntime)<<endl;
           cout<<" YOUR GEAR IS : 4";
        }
         else if (Velocity(accel,ntime) >=10 && Velocity(accel,ntime)<12)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,ntime)<<endl;
           cout<<" YOUR GEAR IS : 5";
        }

         else if (Velocity(accel,ntime) >=12 && Velocity(accel,ntime)<15)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,ntime)<<endl;
           cout<<" YOUR GEAR IS : 6";
        }
         else if (Velocity(accel,ntime) >=15 && Velocity(accel,ntime)<17)
        {
            cout<<"\nyour volcity was    "<<Velocity(accel,ntime)<<endl;
           cout<<" YOUR GEAR IS : 7";
        }
         else if (Velocity(accel,ntime) >=17 && Velocity(accel,ntime)<21)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,ntime)<<endl;
           cout<<" YOUR GEAR IS : 8";
        }
         else if (Velocity(accel,ntime) >=21 && Velocity(accel,ntime)<24)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,ntime)<<endl;
           cout<<" YOUR GEAR IS : 9";
        }
         else if (Velocity(accel,ntime) >=24 && Velocity(accel,ntime)<28)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,ntime)<<endl;
           cout<<" YOUR GEAR IS : 10";
        }
         else if (Velocity(accel,ntime) >=28 && Velocity(accel,ntime)<35)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,ntime)<<endl;
           cout<<" YOUR GEAR IS : 11";
        }
         else if (Velocity(accel,ntime) >=35 && Velocity(accel,ntime)<40)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,ntime)<<endl;
           cout<<" YOUR GEAR IS : 12";
        }
         else if (Velocity(accel,ntime) >=40 && Velocity(accel,ntime)<48)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,ntime)<<endl;
           cout<<" YOUR GEAR IS : 13";
        }
         else if (Velocity(accel,ntime) >=48 && Velocity(accel,ntime)<55)
        {
            cout<<"\nyour Velocity was   "<<Velocity(accel,ntime)<<endl;
           cout<<"YOUR GEAR IS : 14";
        }
         else if (Velocity(accel,ntime) >=55 && Velocity(accel,ntime)<100)
        {
           cout<<"\nYour Velocity was    "<<Velocity(accel,ntime)<<endl;
           cout<<"YOUR GEAR IS : 15";
        }
         else if (Velocity(accel,ntime) >100 )
        {
           cout<<" The Maximmum Volcity that the programme can calcluate is 100 km/hr... SORRY!";
        }
        else if (Velocity(accel,ntime) <0 )
        {
           cout<<" ERROR!!";
        }
        }
//------->end of if velocity

else// else for if time
{
cout<< "Your time is Invalid";
}
        cout<<"\n\nDo you wish to continue? (Y/N)";
		cin>>choice;
		cout<<"\n\n\n----------------------------\n\n\n";
        system("cls");
}while(choice == 'Y'|| choice == 'y');
return 0;
}

Recommended Answers

All 3 Replies

There are a number of issues with this code that I can see, with the foremost being with the way you are getting the time. As it is, you are reading in the time as an 8-character array, using gets() (which is deprecated as being unsafe, but that that's a side issue), then using three of the numerals read in (assuming their positions are correct) as if they were integers. This simply will not work as you're expecting it to.

I can also recommend simplifying the code for showing the velocity and the gear by using a lookup table for the gear ranges.

this is the first semester for me in IT ...
so we didnt study a lot of thing c++ ...... so i used 8-character array arrays because i need the user to enter the time as HH:MM:SS
and what can i use instead of gets()..

Try this:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

const int GEARS = 15;

int toTwoDigitTime(char a, char b)
{
    int tens = (a - 0x30) * 10;
    int ones = (b - 0x30);
    return tens + ones;
}


/** Showing the introduction screen and the user manual... */
void splash(void)
{
    cout << "   ********************************************************\n";
    cout << "   *                                                      *\n";
    cout << "   *            Velocity And Gear Programme ..            * \n";
    cout << "   *         By:****                                      *\n";
    cout << "   *                                                      *\n";
    cout << "   ********************************************************\n\n";

    cout<<endl ;
    system ("PAUSE");
    system ("cls");
    cout << "****************************************************************\n";
    cout << "*               USER MANUAL                                    *\n";
    cout << "* FIRST : The programme will ask you to input the acceleration * \n";
    cout << "* SECOND: The programme will ask you to input the time         *\n";
    cout << "* THIRD : The programe will calculate the velocity             *\n";
    cout << "****************************************************************\n\n";

    system ("PAUSE");
    system ("cls");
}
/** Showing the introduction screen and the user manual... */

double Velocity(double accel, double ntime)// to calcluate the Volcity
{
    return accel * ntime;
}

int main ()
{
    double accel;
    char choice;
    string time;
    double h = 0, m = 0, s = 0;
    splash();

    do
    {
        cout<<"What was your acceleration in (km/hour):  ";
        cin>>accel;
        cin.ignore();
        cout<<"\nWhat was your riding time in hr.min: ";
        cin >> time;
        cin.ignore();

        if (!(isdigit(time[0]) && isdigit(time[1]) &&
                isdigit(time[3]) && isdigit(time[4]) &&
                isdigit(time[6]) && isdigit(time[7])))
        {
            cout << "Your time is invalid" << endl;
            break;
        }

        h = toTwoDigitTime(time[0], time[1]);
        m = toTwoDigitTime(time[3], time[4]);
        s = toTwoDigitTime(time[6], time[7]);


//------>start if for the time..
        if(
            h>=0 && h <=60 &&
            m>=0 && m <=60 &&
            s>=0 && s <=60
        )

        {
            cout<<"YOUR time is: "<<time[0]<<time[1]<<":"<<time[3]<<time[4]<<":"<<time[6]<<time[7]<<endl; //OR:

            double ntime = h + m/60 + s/3600;

            //-------->the if for velocity..
            double v = Velocity(accel, ntime);

            if (v < 0)
            {
                cout<<"ERROR!!" << endl;
            }
            else if (v > 100 )
            {
                cout<<"The maximum velocity that the programme can calcluate is 100 km/hr... SORRY!";
            }
            else
            {
                cout << "\nyour velocity was    "<< v << endl;

                // lookup table for the gears
                int gear[] = {0, 4, 7, 10, 12, 15, 17, 21, 24, 28, 35, 40, 48, 55, 100};

                for (int i = 0; i < GEARS - 1; i++)
                {
                    if ( v > gear[i] && v < gear[i + 1])
                    {
                        cout << "You are in gear " << i + 1 << endl;
                        break;
                    }
                }
            }
        }
//------->end of if velocity

        else// else for if time
        {
            cout<< "Your time is Invalid" << endl;
        }
        cout<<"\n\nDo you wish to continue? (Y/N)" << endl;
        cin>>choice;
        cin.ignore();
        cout<<"\n\n\n----------------------------\n\n\n";
        system("cls");
    }
    while(choice == 'Y'|| choice == 'y');
    return 0;
}

Let me know if you need an explanation on any of this code.

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.