My first C++ project!!!!!!!!

Pages: 12
So guys, I am a math major freshmen in college. I am currently in intro to programming(CS 100), which is taught in C++. We had 20 students initially; now--one month remaining--there are about 5. Anyway, I had no prior experience in programming about 3 months ago, and I have only been practicing programming for hw, as I have little time remaining after completing hw for gen. eds. and major-specific courses. Today, I decided to test out my knowledge and skills to create a program that will estimate your age based on the input in this format: day/month/year. This program, written in about 45 minutes, assumes, 365 days in a year, 30 days in a month. This program is impressive because it separates the input string birth date into it factors: days, months, and year. Anyway, do you think I have learned enough C++ to move on to advanced courses, such as Advanced programming, etc. instead of taking CS 101? Thank you guys.

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

using namespace std;

void findYear(string &);
void findDay (string &);
void findMonth(string &);
int findAge (string, string, string);
int currentYear, currentDay, currentMonth;
int main()
{
string birthday;
cout << "Enter birthday: " << endl; // format : 01/26/1994
cin >> birthday;
cout << "What's the current year?" << endl;
cin >> currentYear;
cout << "What's the current day?" << endl;
cin >> currentDay;
cout << "What's the current month?" << endl;
cin >> currentMonth;
string year = birthday;
string day = birthday;
string month = birthday;
findYear(year);
findDay(day);
findMonth(month);
findAge(year, day, month);

system("pause");
return 0;

}


void findYear(string &year)
{
int slashpos = year.find('/');
int i = 0;
string temp2 = "";
while(year.at(year.length()-1-i)!='/')
{
temp2 += year.at(year.length()-1-i);
i++;
}
string rtemp2 = "";
for(int k = 0; k < temp2.length(); k++)
{
rtemp2 += temp2.at(temp2.length()-1-k);
year = rtemp2;
}

}


void findDay (string &day)
{
string tempday1 = "";
string temp2 = "";
int i = 0;
tempday1 = day.substr(0,day.rfind('/'));
while(tempday1.at(tempday1.length()-1-i)!='/')
{
temp2 += tempday1.at(tempday1.length()-1-i);
i++;
}
string holdtemp2 = "";
for (int k = 0; k < temp2.length(); k++)
{
holdtemp2 += temp2.at(temp2.length()-1-k);
}
day = holdtemp2;
}


void findMonth(string &month)
{
string tempmonth = month.substr(0, month.rfind('/'));
tempmonth = tempmonth.substr(0, tempmonth.rfind('/'));
month = tempmonth;
}


int findAge(string year, string day, string month)
{
int year1;
stringstream(year) >> year1;
int day1;
stringstream(day) >> day1;
int month1;
stringstream(month) >> month1;
cout << "Your month is " << month1 << ", day is " << day1 << ", and year is " << year1 << "." << endl << endl;
int totalYear, totalDay, totalMonth;
int totYeartoDay, totDaystoDay, totMonthstoDay;
totYeartoDay = year1 * 365;
totDaystoDay = day1;
totMonthstoDay = month1 * 30;
int totDays1 = totYeartoDay + totDaystoDay + totMonthstoDay;
totalYear = currentYear * 365;
totalDay = currentDay;
totalMonth = currentMonth * 12;
int tottotal = totalYear + totalDay + totalMonth;
int finaltotalDays = tottotal - totDays1;
totalYear = finaltotalDays/365;
totalMonth = (finaltotalDays%365)/30;
totalDay = ((finaltotalDays%365)%30);
cout << "You were born " << totalYear << " years, " << totalMonth << " months, and " << totalDay << " days ago.\n" << endl;

return 0;
}
Last edited on
closed account (N36fSL3A)
Well code tags would help us analyze your code.
@Lumpkin
Okay, now, start analyzing his 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
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>

using namespace std;

void findYear(string &);
void findDay (string &);
void findMonth(string &);
int findAge (string, string, string);
int currentYear, currentDay, currentMonth;
int main()
{
	string birthday;
	cout << "Enter birthday: " << endl; // format : 01/26/1994
	cin >> birthday;
	cout << "What's the current year?" << endl;
	cin >> currentYear;
	cout << "What's the current day?" << endl;
	cin >> currentDay;
	cout << "What's the current month?" << endl;
	cin >> currentMonth;
	string year = birthday;
	string day = birthday;
	string month = birthday;
	findYear(year);
	findDay(day);
	findMonth(month);
	findAge(year, day, month);

	system("pause");
	return 0;
}


void findYear(string &year)
{
	int slashpos = year.find('/');
	int i = 0;
	string temp2 = "";
	while(year.at(year.length()-1-i)!='/')
	{
		temp2 += year.at(year.length()-1-i);
		i++;
	}
	string rtemp2 = "";
	for(int k = 0; k < temp2.length(); k++)
	{
		rtemp2 += temp2.at(temp2.length()-1-k);
		year = rtemp2; 
	}
}


void findDay (string &day)
{
	string tempday1 = "";
	string temp2 = "";
	int i = 0;
	tempday1 = day.substr(0,day.rfind('/')); 
	while(tempday1.at(tempday1.length()-1-i)!='/')
	{
		temp2 += tempday1.at(tempday1.length()-1-i);
		i++;
	}
	string holdtemp2 = "";
	for (int k = 0; k < temp2.length(); k++)
	{
		holdtemp2 += temp2.at(temp2.length()-1-k);
	}
	day = holdtemp2;
}


void findMonth(string &month)
{
	string tempmonth = month.substr(0, month.rfind('/'));
	tempmonth = tempmonth.substr(0, tempmonth.rfind('/'));
	month = tempmonth;
}


int findAge(string year, string day, string month)
{
	int year1;
	stringstream(year) >> year1;
	int day1;
	stringstream(day) >> day1;
	int month1;
	stringstream(month) >> month1;
	cout << "Your month is " << month1 << ", day is " << day1 << ", and year is " << year1 << "." << endl << endl;
	int totalYear, totalDay, totalMonth;
	int totYeartoDay, totDaystoDay, totMonthstoDay;
	totYeartoDay = year1 * 365;
	totDaystoDay = day1;
	totMonthstoDay = month1 * 30;
	int totDays1 = totYeartoDay + totDaystoDay + totMonthstoDay;
	totalYear = currentYear * 365;
	totalDay = currentDay;
	totalMonth = currentMonth * 12;
	int tottotal = totalYear + totalDay + totalMonth;
	int finaltotalDays = tottotal - totDays1;
	totalYear = finaltotalDays/365;
	totalMonth = (finaltotalDays%365)/30;
	totalDay = ((finaltotalDays%365)%30);
	cout << "You were born " << totalYear << " years, " << totalMonth << " months, and " << totalDay << " days ago.\n" << endl;

	return 0;
}
closed account (Dy7SLyTq)
the only thing (and this is me guessing what is taught in advanced class) is make it all into a class and give it a nice interface. i would also suggest writing a container of some sort, like Array or vector, because that cements in a lot of good skills
Thanks DTSCode--just what I needed to know!
closed account (Dy7SLyTq)
no problem
Btw, were you really born in 1996, making you 17 years old?
Last edited on
closed account (Dy7SLyTq)
yeah. im more of a self taught programmer (some few online stuff) and i am no where (let me repeat no where) near the best, but i have gotten pretty good by writing code every day and paying a lot of attention on this site. the members here can teach you alot
Thanks man. Also, it's very impressive that u know so much already.hopefully, we older folks can catch up.
closed account (N36fSL3A)
Dengit. I was gonna say that DTS. :(

I'd probably stop using using and just prefix the standard libraries with std::. Some programmers really rage when you do that.

It should help with working in groups. But I guess it works both ways, some like to use using and will rage when you don't.
closed account (Dy7SLyTq)
expanding on what lumpkin said: using namespace is generally a bad idea because it will bring in everything when you dont neccesarily need it like if i did using namespace std and included iostream i would have (this is a short list and by no means exhaustive)
clog
cerr
cout
cin
printf (i think)
scanf (" ")
ostream
istream

and i probably dont need all of those. there are two solutions:
using std::clog;
using std::cerr;
//... etc etc

or just prefix something with std:: when you call it. ie:
std::cout<<"Hello, world!"<< std::endl;

@fred: were you talking about learning classes or my mini bio?
closed account (N36fSL3A)
@fred: were you talking about learning classes or my mini bio?
Huh? I don't understand.

PS: I'm called Fred by some on this forum, that was my previous name.
closed account (Dy7SLyTq)
when you said
Dengit. I was gonna say that DTS. :(
what were you referring to?
thanks guys. i knew there was a reason why the code compiled slowly.
closed account (N36fSL3A)
I was actually refering to your first post:

the only thing (and this is me guessing what is taught in advanced class) is make it all into a class and give it a nice interface. i would also suggest writing a container of some sort, like Array or vector, because that cements in a lot of good skills
I was going to reply basically the same thing, but you beat me to it.
closed account (Dy7SLyTq)
oh...
Erm, how slow does it compile with the using directive? I don't think it should be noticeably slow even when you are using it.

Good start though :)

There are many ways in which it can be improved, but it all depends on how much you now at this point. Are you familiar with structs?

It certainly beats the hell out of my first program. It was a flying spaghetti monster of goto from hell.
closed account (N36fSL3A)
I think it doesn't actually make the program compile slower, (the compiler would probably just leave out unused portions,) but it's just a matter of preference.
I would say to never use using namespace std; for the reasons DTSCode mentioned. If you don't want to do std:: throughout the code I would say use using std::cin; using std::cout; using std::endl; // etc as it saves you a lot of typing and doesn't flood the namespace with the entire standard objects.

[EDIT]
Though, learncpp.com does something that I think is a little annoying and overkill.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

class NAME{
      using namespace std;
      //....
};

void funct(...)
{
      using namespace std;
      //......
}

int main()
{
      using namespace std;
      // .......
      return 0;
}
Last edited on by closed account z6A9GNh0
I'm EXTREMELY glad to see a new programmer that knows a thing or two about keeping good indenting style. Follow everything that DTSCode has been saying, and you are well on your way to learning how to program.

And when I say well on your way, I mean you've just started. I've been programming for around 3 years and I'm still learning something new every day.
Pages: 12