CAN YOU CHECK MY assignment PLZZ

Pages: 12
hello everybody ...
" i am beginner"
i have an assignment and i did my best but i feel there is something wrong ...

here is the assignment question!! i couldn't do the time as hh:mm:ss because i will have problem to calculate the time any one can help me with that i will be grateful


Write a program to ask user to input an acceleration (km/hour) and the riding time (hh:mm:ss)
and determine which gear the bicycle is in at that time. Print the velocity and the gear number to
the screen. Limit the maximum velocity to 100km/hr. For any velocities that are on the border of
a gear change, use the lower gear. Your program should allow the user to repeat this calculation
as often as the user wishes.


and this is my code ..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include<cstdlib>

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... */
void input(double &accel,double &time)
{
   cout<<"What was your Accelaration in (km/hour)  :  ";
    cin>>accel;

    cout<<"\nWhat was your riding time  ";
    cin>>time;


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

    return Velocity;
}


int main ()
{
    double accel;
    double time;
    char choice;

splash();

do
    {

        input(accel,time);

        if (Velocity(accel,time)>=0 && Velocity(accel,time)<4)
        {
            cout<<"\nyour Velocity was    "<<Velocity<<endl;
            cout<<" YOUR GEAR IS : 1";
        }
        else if (Velocity(accel,time) >=4 && Velocity(accel,time)<5)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,time)<<endl;
           cout<<" YOUR GEAR IS : 2";
        }
         else if (Velocity(accel,time) >=5 && Velocity(accel,time)<7)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,time)<<endl;
           cout<<" YOUR GEAR IS : 3";
        }
         else if (Velocity(accel,time) >=7 && Velocity(accel,time)<10)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,time)<<endl;
           cout<<" YOUR GEAR IS : 4";
        }
         else if (Velocity(accel,time) >=10 && Velocity(accel,time)<12)
        {
            cout<<"\nyour Velocity was    "<<Velocity(accel,time)<<endl;
           cout<<" YOUR GEAR IS : 5";
        }

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

		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;

}








hellooo
does that actually work?
You want to take riding time as input in the format hh:mm:ss, right? So read it in as a string, and then use
time = hh + (mm/60) + (ss/3600);

Make sure to have certain conditions, such as mm and ss <=60, and hh<=24 and all...

Caprico : i tried to do as you said but the problem is how to put ':' between them ...

1
2
3
4
5
6
7
8
9
       string in;
    stringstream ss;

cout<<"\nWhen was your riding time in HH:MM:SS  :  ";
getline(cin,in);

        ss.str(in);
        
        cout<<ss;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string time;
int ss, mm, hh;

cout<<"\nWhat was your riding time in HH:MM:SS  :  ";
gets(time);

ss=time[7]+10*time[6];
mm=time[3]+10*time[4];
hh=time[1]+10*time[0];

while (ss>=60)                //This is necessary only if you want to display
{                                      //the time again in the above format, but properly...
   ss=ss-60;
   mm++;
}

while (mm>=60)              //Same as above comment...
{
   mm=mm-60;
   hh++;
}

time=hh+mm/60+ss/3600;    //in hours 


The only issue in this is that the user must have enough sense as to input the string as indicated. Everything will work fine then...
Last edited on
okay ... but the problem with "gets(time)" its show : 'gets' was not declared in this scope
so i used getline(cin,time).. is it right

the second thing when i use your code and when i enter the time it shows me a lot of symbols

like "→,. %,*"
???

and can you explain what did you do here
ss=time[7]+10*time[6];
mm=time[3]+10*time[4];
hh=time[1]+10*time[0];

i am sorry if i am askin a lot of question :(
Last edited on
Nah, don't be sorry, that is what the forums are for.. its good that you're trying to clarify and learn.. :)
yeah, gets() requires string.h... sorry if I didn't mention that.. if it doesn't work, use ur getline, dt'll also do...
ohk, so a string is basically a character array, right, so suppose this | | represents a memory block of the array and the number below it is the number or address we've assigned. When the user enters the string, it will get stored as:

|h|0 |h|1 |:|2 |m|3 |m|4 |:|5 |s|6 |s|7


so i accesses time[6] and time[7] to get total number of seconds, and so on... i skipped time[2], time[5], as they have colons... get it now...?
Last edited on
ooh finally i understand what is the string thank you maan


but when i enter the time it shows me a lot of symbols (( the result will still string)
like i entered 12:12:12

it shows " %" not a number ... ..
???
i think we need to change the ss ,hh,mm from string to stream before we apply the time equation !!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void main()
{
	clrscr();

	char time[8];
	int h=0, m=0, s=0;

	cout<<"Enter time: ";
	gets(time);

	h=(time[1]+(10*(time[0])))-528;
	m=(time[4]+(10*(time[3])))-528;
	s=(time[7]+(10*(time[6])))-528;

	cout<<"The time is: "<<time[0]<<time[1]<<":"<<time[3]<<time[4]<<":"<<time[6]<<time[7]; //OR:
	cout<<"\nThe time is: "<<h<<":"<<m<<":"<<s;
	getch();
}


Check this... It works fine.. Use it in your code... :)
Never use gets http://linux.die.net/man/3/gets (bugs)
1
2
3
int h,m,s;
char d;
cin>>h>>d>>m>>d>>s;

¿why 528? There are functions that will convert a 'string' to a number, don't reinvent the square wheel.

1
2
3
4
5
6
7
/*while (ss>=60)
{
   ss=ss-60;
   mm++;
}*/
mm += ss/60; //assuming hh, mm, ss are integers
ss %= 60;


@TV5: Try to use an array to calculate the gear
int limit[] = {4,5,7,10,12,15,17,21,/*...*/};
FANTASTIC its totally worked ....

last thing that i want to know why we need to but "-528" what is the secret behind it
..!
because if we didnt put it the result always be more than 500 and it the Ntime will be in integer always how to make it show in decimal ...
i am really grateful ... you save my life in last min i need to submit this assignment 2omorrow morning and i finished all because of you... thanks alot

my new code is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<cstdlib>
using namespace std;
int main()
{


	char time[8];
	int h=0, m=0, s=0;

	cout<<"Enter time: ";
	gets(time);

	h=(time[1]+(10*(time[0])))-528;
	m=(time[4]+(10*(time[3])))-528;
	s=(time[7]+(10*(time[6])))-528;


if(
        (time[1]+(10*(time[0])))-528>=0 && (time[1]+(10*(time[0])))-528 <=60&&
        (time[4]+(10*(time[3])))-528>=0 && (time[4]+(10*(time[3])))-528 <=60&&
        (time[7]+(10*(time[6])))-528>=0 && (time[7]+(10*(time[6])))-528 <=60
   )
   {

    cout<<"The time is: "<<time[0]<<time[1]<<":"<<time[3]<<time[4]<<":"<<time[6]<<time[7]<<endl; //OR:
    //cout<<"\nThe time is: "<<h<<":"<<m<<":"<<s;
    double ntime=h+m/60+s/3600;
    cout<<"/nyour time is"<<ntime;

//-----> the rest of my code will goes here
    }

else
    {
    cout<< "Your time is Invalid";
    }
}
Last edited on
double ntime=h+m/60+s/3600;¿are you sure? Integer division returns an integer.
ne555: At this level, I don't thin being memory efficient or speed efficient makes much of a difference... Marks are not given to memory-efficient programs, they are given to simple, correct, compilable programs...

TV5: The thing is, the string is a character array. so it stores 1, 2, 3.... as characters and not numbers, and their character values are as such:
0-48
1-49
3-50
...

So for example, the calc for 11 would be: 49 + 10*49=539... Subtract 528, and you get back 11... So that's the "secret" behind it.. If I could put in a little more time, the code would be cleaner and less amateurish and could have helped you design the code better, but I have my Sems coming up, so.... :) Anyways, you're very welcome!!! :)
WHT!!!!

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#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;
}



Last edited on
I don't understand? What exactly happened? Plz explain properly.. at what point did your code stop working?
not its worked ... but its shows like this

What was your Accelaration in (km/hour) : <your input>


What was your riding time in hr.min : your time is not valid
the the programme exit...

its suppose to enter the time as hh:mm:ss ---> then apply this code
1
2
3
4
5
6
if(
        h>=0 && h <=60&&
        m>=0 && m <=60&&
        s>=0 && s <=60
   )
//----> the rest of the code  


if not show
invalid time 
and exit
but it always show invalid time automatically after input the accelration
Last edited on
So the problem is in your if condition... Act it should be this:

1
2
3
4
if (h>=0&&m>=0&&s>=0&&h<24&&m<=60&&s<=60)
{
   //the code
}

Keep the entire if condition in one line...
Pages: 12