Adding/Subtracting Limit

Hello ! I am new to C++ and I currently have a project that needs to calculate a number. My program needs to add and subtract numbers but reset it's counting when it reaches to a certain number. for example I would like to add 20 and 8 but my program resets it's counting when it reaches to 24 so it means that when the number exceed to 24 the program will continue to compute but continue it on 0. so the answer will be 20 + 8 = 4
Ok,
Use this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main()
{
cout << "Enter two numbers" << endl;
int num = 0;
cin >> num;
int num2;
cin >> num2;
if(num + num2 > 23)
{
cout << -(24 - (num + num2)) << endl;
}
else
cout << num + num2 << endl;
return 0;
}


Does this help?
well that code doesn't help me tho. but thanks anyway :D
this is my current program. the problem still occurs to me and i don't know what to do to stop it from exceeding to 24 :(
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
#include <iostream>
using namespace std;
int main ()
{
	int h,m,s;
	int choice;
	
		cout << "Enter hour : "<< endl;
		cin>> h;
		cout << "Enter minute : " <<endl;
		cin>> m;
		cout <<"Enter second : "<<endl;
		cin>> s;

		cout << "Select time to be added ";
		
 
    cout << "[1] Singapore \t[2] Hawai";
	cin >> choice;
	switch (choice)
		{
			
	case 1:
			
			cout<<"\n\n\t\t" <<h<<":"<<m<<":"<<s;
			cout << "\t\tCurrent Time";
			cout << "\n\n\t\t" << (h+8)<<":"<<m<<":"<<s;
			cout << "\t\tSingapore ";
			cout <<endl;
			break;
	case 2:
			
			cout<<"\n\n\t\t" <<h<<":"<<m<<":"<<s;
			cout << "\t\tCurrent Time";
			cout << "\n\n\t\t" << (h-10)<<":"<<m<<":"<<s;
			cout << "\t\tHawai ";
			cout <<endl;
			break;
	}
	
system("pause");
return 0;
}


Enter hour : 
23
Enter minute : 
10
Enter second : 
10
Select time to be added [1] Singapore 	[2] Hawai
1


		23:10:10		Current Time

		31:10:10		Singapore 
You could try using the % operator. e.g.
1
2
3
4
5
cout<<"\n\n\t\t" <<h<<":"<<m<<":"<<s;
			cout << "\t\tCurrent Time";
			cout << "\n\n\t\t" << (h+8) % 24<<":"<<m<<":"<<s;
			cout << "\t\tSingapore ";
			cout <<endl;

Also please try not to double post as the suggestions on this page are actually very useful http://www.cplusplus.com/forum/general/186395/
Hope this can help, good luck :D.
Last edited on
Thank you !! Really appreciate it. Your respond totally save my project. Big thanks bro. ow and btw. sorry about that. Newbie here hehe
Last edited on
No problem, glad to help:D.
Topic archived. No new replies allowed.