hello guys ...
i have a problem with my code here
its basically to ask the user to input the time as HH:MM:SS then i calcluate the time as
time=hh+mm/60+ss/3600

so first i used string so i will be able to store ':'..
now how to convert this string into
time=hh+mm/60+ss/3600

with these code the result will still string it shows only simple ...

this is my code

int main()
{
    string time;
int ss, mm, hh;

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

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


time=hh+mm/60+ss/3600;
cout<<time;
}
ss=time[7]+10*time[6];
mm=time[3]+10*time[4];
hh=time[1]+10*time[0];

This won't work because the digits are characters. You need to subtract '0' from them to get the numeric representation:

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

Note that I also fixed the equations to do what you intended.

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

This is completely nonsensical because you're trying to assign the value of an integer to a string. There's no automatic conversion, so you'll get garbage.

Finally, the calculation is confusing because minutes and seconds will always be 0. The largest value of mm and ss will be 59, assuming you're validating the usual hh:mm:ss format. 59/60 is 0, and 59/3600 is 0, so the result of hh+mm/60+ss/3600 is equivalent to hh even with the largest possible values of mm and ss :

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string time("10:59:59");
    int ss, mm, hh;

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

    cout << hh + mm / 60 + ss / 3600 << '\n';
}
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.