C++ Midterm 1 Notes
C++ Midterm 1 Notes
#include <string>
#include <cmath> #include <cstring>
using namespace std;
using namespace std;
//TODO: ""+"", .c_str, stod(), atof(),
int day(int year1,int year2,int month1,int month2,int day1, int to_string()
day2);
int main()
int convertYear(int year); {
char word[] = "dog";
bool isLeapYear(int year);
// "dog" is c-string not a
"normal string"
int convertMonth(int year, int month);
cin>>month1>>slash>>day1>>slash>>year1;
cout << x.c_str() << endl;
cout<<"Enter end date (no spaces) (mm/dd/yyyy): \n ";
string num = "1235.32";
cin>>month2>>slash>>day2>>slash>>year2;
cout << stod(num)*2 << endl;
cout << atof("12.35")*2 << endl;
totalDays=day(year1,year2,month1,month2,day1,day2);
if(isLeapYear(year1)==true){totalDays=totalDays-2;}
string z = to_string(123.45);
if(totalDays<0){cout<<"Never..."; cout << z << endl;
exit(0);} return 0;
}
else{cout<<"You have to wait "<<totalDays<<" days";}
int totalDays=0;
month1=convertMonth(year1,month1);
month2=convertMonth(year2,month2);
year1=convertYear(year1);
year2=convertYear(year2);
totalDays=(year2+month2+day2)-
(year1+month1+day1);
return(totalDays);
}
#include <iostream> #include <iostream>
using namespace std; using namespace std;
//~ sum=0;
//~ for(int i=0; i < 3; i++)
//~ {
//~ sum = sum + xi; // "xi"
is a new variable
//~ }
string name[3];
name[0] = "James";
name[1] = "Edward";
name[2] = "Parker"; /*
#include <iostream>
return 0;
} using namespace std;
int main()
{
int sumTo;
cout << "Find the sum from 1 to
what value? ";
cin >> sumTo;
cout << 1;
int sum=1;
for(int i=2; i <= sumTo; i++)
{
cout << "+" << i;
sum += i;
}
return 0;
}
*/
void gotoC() #include <iostream>
{
cout << "starting C\n"; using namespace std;
char dir;
cout << "A rusty sword is sticking out void gotoA();
of a stone...\n"; void gotoB();
cout << "Do you want to go (p)ull it, void gotoC();
(r)un away or (t)ake a nap?\n"; void gotoD();
cin >> dir; void gotoE();
if(dir == 'p') int main()
{ {
gotoD(); gotoA();
} cout << "ending main\n";
else if(dir == 'r')
{ return 0;
gotoE(); }
}
else void gotoA()
{ {
gotoB(); cout << "starting A\n";
} char dir;
cout << "ending C\n"; cout << "A zombie pops out of the
ground and asks you for your SS
} number...\n";
void gotoD() cout << "Do you want to go (l)eft or
{ (r)ight?\n";
cout << "starting D\n"; cin >> dir;
char dir;
cout << "A unicorn starts charing at if(dir == 'l')
you very quickly...\n"; {
cout << "Do you want to dodge (l)eft, gotoC();
(r)ight or (d)uck?\n"; }
cin >> dir; else
{
if(dir == 'd') gotoE();
{ }
gotoD();
}
else if(dir == 'r') cout << "ending A\n";
{ }
gotoE(); void gotoB()
} {
else cout << "starting B\n";
{
gotoB(); char dir;
} cout << "You see a rabbit jumping at
cout << "ending D\n"; a rubber chicken\n";
cout << "Do you want to go (l)eft,
} (r)ight or (f)orward?\n";
cin >> dir;
void gotoE()
{ if(dir == 'l')
cout << "COntraguations!\n"; {
cout << "you exited the maze!\n"; gotoE();
}
return; else if(dir == 'r')
} {
gotoC();
}
else
{
gotoA();
}
cout << "ending B\n";
}
#include <iostream> using namespace std;
#include <cmath> int main(){
else{total=total+365;}
} if(year%400==0){return(1);}
srand(time(0)); }
randomchar(x); while(month>0){
} if(month==2){
int x=rand()%4; }
month--;}
return(total);}