Conitinue without loop?

Hi c++ forum, I'm new to c++ and I'm having problems, I tried my best to search this forums for any helpful solutions but it seems that I don't know how to make them work.

So my problem is I want to set condition to int d(days) and y(years) but since I set a condition on int m(month) I dont know how to set condition to the said integers.
Also the integer age is not working properly since it does not subtract the value of y to the value of cy.


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
#include <iostream>

using namespace std;

int main(){

	int m,d,y;
	int age;
	const int cy = 2018;
	age = (cy - y);
	
	cout<<"Enter month in numerical: ";
	cin>>m;
		
	cout<<"Enter day in numerical: ";
	cin>>d;

	cout<<"Enter year in numerical: ";
	cin>>y;
	
	if (m == 1){
		cout<<"January "<<d<<", "<<y<<" is now "<< age <<endl;
	}
	else if (m == 2){
		cout<<"February"<<d<<", "<<y<<" is now "<< age <<endl;
	}
	else if (m == 3){
		cout<<"March"<<d<<", "<<y<<" is now "<< age <<endl;
	}
	else if (m == 4){
		cout<<"April"<<d<<", "<<y<<" is now "<< age <<endl;
	}
	else if (m == 5){
		cout<<"May"<<d<<", "<<y<<" is now "<< age <<endl;
	}
	else if (m == 6){
		cout<<"June"<<d<<", "<<y<<" is now "<< age <<endl;
	}
	else if (m == 7){
		cout<<"July"<<d<<", "<<y<<" is now "<< age <<endl;	
	}
	else if (m == 8){
		cout<<"August"<<d<<", "<<y<<" is now "<< age <<endl;
	}
	else if (m == 9){
		cout<<"september"<<d<<", "<<y<<" is now "<< age <<endl;
	}
	else if (m == 10){
		cout<<"October"<<d<<", "<<y<<" is now "<< age <<endl;
	}
	else if (m == 11){
		cout<<"November"<<d<<", "<<y<<" is now "<< age <<endl;	
	}
	else if (m == 12){
		cout<<"December"<<d<<", "<<y<<" is now "<< age <<endl;
	}
	else 
		cout<<"You have entered invalid value!";
		


return 0;
}


PS: sry if my code's a little bit messy.
Last edited on
Are you trying to check if m, d and y have correct input by the user? also what is the purpose of age = (cy - y);? Since it is defined before you input y it serves no purpose.

Also what do you mean by "continue without loop"?
Last edited on
The purpose of age = (cy - y) so that it can calculate the user's age since cy = 2018 and minus the year the user inputted.

What I meant in "continue without loop", is there anyway you can you can continue without using the loop statements cause I want to input condition on d and y. If you tried changing the value of m it goes to the "else" , I want to do it on d and y too. I tried researching and I came across to continue statement but for does continue to work the codes needs to be in loop statement. I want to do it like this
1
2
3
4
5
6
7
cout<<"Enter month in numerical: ";
	cin>>m;
       if(d < 31){
         continue;
       else
           cout<<"You have entered invalid value!";
}


I know it doesn't work that way that's why Im asking is there any other way for that to work?
Last edited on
Okay so I understand that your program's purpose is to calculate the user's age according to his birth date.

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
#include <iostream>

using namespace std;



int main()
{
	int m, d, y;
		
	int age;
	
	const int CY = 2018; //By convention, constants are often all caps
	
	//age = (cy - y);
	
	//Get month
	do
	{
		cout << "\n\nEnter month in numerical: ";
		cin >> m;
		
		if (m < 1 || m > 12)
			cout << "You have entered invalid value!";
	}
	while (m < 1 || m > 12);
	
	//Get day
	do
	{
		cout << "\n\nEnter day in numerical: ";
		cin >> d;
		
		if (d < 1 || d > 31)
			cout << "You have entered invalid value!";
		
		
	}
	while (d < 1 || d > 31);

	//Get year
	cout << "\n\nEnter year in numerical: ";
	cin >> y;
	
	
	//Calculate age
	age = ( CY - y);
	
	
	//Display
	switch (m)
	{
		case 1:
			cout << "\nJanuary " << d << ", "<< y << " is now " << age << endl;
			break;
		case 2:
			cout << "\nFebruary " << d << ", "<< y << " is now " << age << endl;
			break;
		case 3:
			cout << "\nMarch " << d << ", "<< y << " is now " << age << endl;
			break;
		case 4:
			cout << "\nApril " << d << ", "<< y << " is now " << age << endl;
			break;
		case 5:
			cout << "\nMay " << d << ", " << y << " is now " << age << endl;
			break;
		case 6:
			cout << "\nJune " << d << ", " << y << " is now " << age << endl;
			break;
		case 7:
			cout << "\nJuly " << d << ", " << y << " is now " << age << endl;
			break;
		case 8:
			cout << "\nAugust " << d << ", " << y << " is now " << age << endl;
			break;
		case 9:
			cout << "\nSeptember " << d << ", " << y << " is now " << age << endl;
			break;
		case 10:
			cout << "\nOctober " << d << ", " << y << " is now " << age << endl;
			break;
		case 11:
			cout << "\nNovember " << d << ", " << y << " is now " << age << endl;
			break;
		case 12:
			cout << "\nDecember " << d << ", " << y << " is now " << age << endl;
			break;
	}
		

system("PAUSE");

return 0;
}



First, if you want to calculate the age you need to calculate it after you've set year and month, otherwise year and month will have a random value and you won't get what you want.

Second by using a do-while loop, on each input for y and d, you can individually verify input until it is correct.
Here's some reference for do-while loop: https://en.cppreference.com/w/cpp/language/do

Third, instead of using many if-else statement, which is very tedious, you can use a switch statement, as shown from line 56 to 94.
Here again some reference: https://en.cppreference.com/w/cpp/language/switch


Edit: You can make your program even more efficient by displaying the info with a function and having names of months in an Enum for instance but I won't bother you with that since it's not what you asked :)

Hope this helps
Last edited on
1. This was very helpful because I thought that you can instantly put the formula so that you can just look up to it later lol.

In the case of 2, I understand loop statements but I'm trying if the code can work without using it. That's why I asked if we can set condition without using the loop statement

In the case of 3, I use else-if statements so that the "else" part can print "You have entered invalid value" in case you put invalid value on m and d.

I want to make it work without looping. For example
1
2
3
4
5
 
          if (m == 1){
		cout<<"January "<<d<<", "<<y<<" is now "<< age <<endl;
          else 
		cout<<"You have entered invalid value!";


The "else" part only works if you put wrong value in m, So if you put any value on d it will print the normal print with the same value you put in d.
How can you make it work that if you put wrong value in d it still print the "else" part.



Anyways thanks, that was very helpful and you given far more than I asked. :))
Last edited on
1. This was very helpful as I thought you can instantly put the formula so that you can just look up to it later lol.


If you want to do that you'd need some sort of function like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int calculateAge(int  CY, int y)
{
    return (CY - y);
}

int main()
{
    int y, d, m, age;
    const int CY;

    //your logic

    age = calculateAge(CY, y);

    //...

    return 0;
}



In the case of 2, I understand loop statements but I'm trying if the code can work without using it. That's why I asked if we can set condition without using the loop statement


It can work but you'll only get one chance to get input from user.
1
2
3
4
5
6
7
8
9
do
	{
		cout << "\n\nEnter month in numerical: ";
		cin >> m;
		
		if (m < 1 || m > 12)
			cout << "You have entered invalid value!";
	}
	while (m < 1 || m > 12);


If you look at the loop, it will ask the user to re-enter input as long as month is inferior to 1 and higher than 12. Displaying an error message when it's wrong. I'm pretty sure that this is what you're mentioning.

In the case of 3, I use else-if statements so that the "else" part can print "You have entered invalid value" in case you put invalid value on m and d.


Try to run this piece of code I tweaked for you and try to input any number lower than 1 for month and day or higher than 31 for day and higher than 12 for month. You'll be prompted with an error message and asked to enter input again.
Last edited on
Topic archived. No new replies allowed.