0% found this document useful (0 votes)
106 views

C++ Midterm 1 Notes

This C++ program demonstrates: 1) Different ways to add strings together and convert between data types like strings and numbers. 2) Using arrays to store multiple values of the same type and accessing individual elements. 3) A simple text-based game that uses goto statements to direct program flow between different parts labeled A, B, C, etc. based on user input.

Uploaded by

gar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

C++ Midterm 1 Notes

This C++ program demonstrates: 1) Different ways to add strings together and convert between data types like strings and numbers. 2) Using arrays to store multiple values of the same type and accessing individual elements. 3) A simple text-based game that uses goto statements to direct program flow between different parts labeled A, B, C, etc. based on user input.

Uploaded by

gar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream> #include <iostream>

#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);

int main(){ string x = "dog"; // from char[]


to string
int year1,year2,month1,month2,day1,day2,totalDays; string y = "cat";
cout << x+y << endl; // + on 2
char slash; strings
// cout << "dog"+"cat" << endl; // +
cout<<"Enter start date (no spaces) (mm/dd/yyyy): \n "; on 2 c-string (char[])

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 day(int year1,int year2,int month1,int month2,int day1, int


day2){

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;

// not diff between 3 var vs. 1 array size 3 int main()


int main() {
{ int top;
cout << "sum to what?\n";
int x[3]; // [ ] means size cin >> top;
x[0] = 1; // [ ] go to that spot
x[1] = 2;// [ ] go to that spot int sum = 0;
x[2] = 8;// [ ] go to that spot
for(int i=1; i< top; i++)
cout << "middle element is: " << {
x[1] << endl;// [ ] go to that spot cout << i << "+";
int sum =0; sum = sum + i;
for(int i=0; i < 3; i++) }
{
sum = sum + x[i]; cout << top;
} sum = sum + top;
cout << "sum is: " << sum << endl;
cout << " = " << sum;
int x0 = 1; }
int x1 = 2;
int x2 = 8;

cout << "middle element is: " << x1


<< endl;

//~ 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;
}

cout << " = " << sum << endl;

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(){

using namespace std;


void realswap(int &x, int &y); //string equation;
int main(){ double a, b;
int a; char ope;
int b; cout << "Enter an equation: ";
char ope; cin >> a >> ope >> b;
cout << "enter a decimal" << endl; cout << a << " " << ope << " " << b
cin >> a >> ope >> b; << " = ";
cout << "before swap: a=" << a << ", b= switch (ope){
" << b << "\n";
swap(a,b); case '+':
cout << a << ope << b << endl; if(a+b==0){cout << a + b << endl;
return 0; cout << "Wow looks like your IQ" <<
} endl;}
void realswap(int &x, int &y){ else{cout << a + b << endl;}
int temp; break;
temp=x;
x=y; case '-':
y=temp; if(a-b==0){cout << a - b << endl;
} cout << "Wow looks like your IQ" <<
endl;}
else{cout << a - b << endl;}
break;
#include <iostream>
#include <cmath> case '*':
if(a*b==0){cout << a * b << endl;
cout << "Wow looks like your IQ" <<
using namespace std;
endl;}
int main(){
else{cout << a*b << endl;}
double num;
break;
double new_guess;
case '/':
double guess=2, old_guess;
if (b != 0){cout << a/b << endl;}
cout << "What number do you want to square
else{cout << "Can't divide by zero
root?" << endl;
you idiot" << endl;}
cin >> num;
    
if(num > 0){
break;
//for (int i=0; i<20; i++){
default:
cout << "Thats not math you idiot" <<
endl;}
  return (0);}
while(!((new_guess <= 1.01*old_guess) &&
(new_guess >= .99*old_guess))){
new_guess=(guess + (num / guess)) / 2;
old_guess = guess;
guess=new_guess;
}

cout << "The square root is " <<


new_guess << endl;
}
return(0);
}
int convertYear(int year){

#include <iostream> int total=0;

#include <cstdlib> while(year>0){

using namespace std; if(isLeapYear(year)==true){total=total+366;}

else{total=total+365;}

void randomchar(int a){ year--;}

if(a==0){cout << 0;} return(total);}

else if(a==1){cout << 1;} bool isLeapYear(int year){

else if(a==2){cout << '.';} if(year%4==0){

else{cout <<' ';} if(year%100==0){

} if(year%400==0){return(1);}

int main(){ else{return(0);}

srand(time(0)); }

cout << "What do you want printed vertically? \n"; else{return(1);}

char word=' '; }

while(word != '0'){ return(0);

cin >> word; }

for(int i=0;i<9;i++){ int convertMonth(int year, int month){

int x=rand()%4; int total=0;

randomchar(x); while(month>0){

} if(month==2){

cout << word; if(isLeapYear(year) == true){total=total+29;}

for(int i=0; i<9; i++){ else{total=total+28;}

int x=rand()%4; }

randomchar(x); else if(month%2!=0 && month<8){total=total+31;}

} else if(month%2==0 && month<8){total=total+30;}

else if(month%2==0 && month>=8){total=total+31;}

else if(month%2!=0 && month>=8){total=total+30;}

month--;}

return(total);}

You might also like