Category Archives: #C++ programming

C++ Programming Maze

This is a final project of mine from my C++ class.  Its currently a working program but feel free to make changes if you must and enjoy!  Oh, if you decide to copy the code you have to like the page.

rant away

 

#include <iostream> //programmer rant4u
#include <string>  //
#include <iomanip> //
#include <cmath>

using namespace std;

//declare array for game board and fill it    // gameboard
char gameBoard[20][20] =
{ {‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘, ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘}
, {‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’, ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘, ‘-‘ , ‘-‘ , ‘#’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’, ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘O’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’ , ‘-‘ , ‘|’, ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’}
, {‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’, ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’, ‘#’ , ‘|’ , ‘#’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘#’ , ‘|’ , ‘-‘, ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘-‘ , ‘|’ , ‘#’, ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘|’ , ‘|’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘-‘ , ‘-‘ , ‘|’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘-‘ , ‘-‘ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘-‘ , ‘-‘ , ‘#’ , ‘-‘ , ‘|’}
, {‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’}
, {‘|’ , ‘X’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘, ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘}};

void up(int &, int &); // pass the players position to all
void down(int &, int &); // functions by reference
void left(int &, int &);
void right(int &, int &);   //declaring functions

int main()
{
char ch1;                    // variables
int playerCOL = 1; // variable to hold players x coord remember higher numbers move right
int playerROWS= 18; // variable to hold players y coord remember higher numbers move down
bool game = false; // bool to hold tell when the game is over

// at start of program clear the screen and output the map
do            // starting the do while loop
{
system(“cls”);             //clearing the screen
for(int i = 0; i < 20; i++)           //refreshing the output screen
{
for(int j = 0; j < 20; j++)
{
cout << gameBoard[i][j] << ” “;
}
cout <<endl;
}
cout <<“Your currently at location ” << playerROWS << ” ” << playerCOL <<endl;   //asking the user for input
cout << “Which direction would you like to move:  WSAD ?” <<endl;
cin >> ch1;      //input

if (ch1 == ‘w’)                   //series of if checks, then calls the function of movement up
{
up(playerCOL, playerROWS);
}
else if (ch1 == ‘a’)
{
left(playerCOL, playerROWS);
}
else if (ch1 == ‘s’)
{
down(playerCOL, playerROWS);
}
else if (ch1 == ‘d’)
{
right(playerCOL, playerROWS);      // end of directional checks
}
else
{
cout <<“error”<<endl;            //if the user enters wrong letter for direction
}
if(playerCOL == 1 && playerROWS == 4)   //if the player reaches the ‘O’ and wins
{
game = true;
}
}
while(game == false);          //triggers end of game

cout << ” Congrats you have won the game”<<endl;         //display when you win
system(“pause”);
return 0;
} // end main
void up(int & COL, int & ROWS) // function that moves the player in the up direction
{
if(gameBoard[ROWS-1][COL] == ‘#’)           //moves your marker up
{
ROWS–;
gameBoard[ROWS][COL] = ‘X’;
gameBoard[ROWS+1][COL] = ‘#’;
}
else
{
cout <<“Bad Move” <<endl;            //displaying your bad move if you cant move there
}

} // end up()
void down(int & COL, int & ROWS) // function that moves the player in the down direction
{

if(gameBoard[ROWS+1][COL] == ‘#’ ||gameBoard[ROWS+1][COL] ==’O’)   //moves your marker down
{
ROWS++;
gameBoard[ROWS][COL] = ‘X’;
gameBoard[ROWS-1][COL] = ‘#’;
}
else
{
cout <<“Bad Move” <<endl;           //displaying your bad move if you cant move there
}

} // end down()

void left(int & COL, int & ROWS) // function that moves the player in the left direction
{

if(gameBoard[ROWS][COL-1] == ‘#’)   //moves your marker left
{
COL–;
gameBoard[ROWS][COL] = ‘X’;
gameBoard[ROWS][COL+1] = ‘#’;

}
else
{
cout <<“Bad Move” <<endl;           //displaying your bad move if you cant move there
}

} // end left()

void right(int & COL, int & ROWS) // function that moves the player in the right direction
{
if(gameBoard[ROWS][COL+1] == ‘#’)          //moves your marker right
{
COL++;
gameBoard[ROWS][COL] = ‘X’;
gameBoard[ROWS][COL-1] = ‘#’;

}
else
{
cout <<“Bad Move” <<endl;    //displaying your bad move if you cant move there
}

} // end right()

Calculating the sum of the difference of two intergers

    // Programmer:
// Program purpose: Lab_on_For_Loops 3/14/12

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
   int num1 = 0,   //assignment for intgers and bool
      num2 = 0,
      result,
      sum = 0,
      number1;
   bool notevil = false;

   cout <<”       1.) Get the sum of two numbers”<<endl; //starting menu
    cout <<”       2.) Exit Program”<<endl;

    cout <<“Enter a number from the menu to select”; //asking for a selection
    cout <<endl;
    cout <<endl;
    cin >> number1;

    switch (number1) // switches number one with a number
    {
      case 1: notevil = false; // switches number1 to true
         break;
      case 2: notevil = true;
         break;
    }

  
    while(notevil == false) // if #1 is selected go into the “if” statement
   {
      num1 = num2 = sum = 0;

      cout <<“Please enter your first number”<<endl;// ask user for first number
      cin >> num1;
   
   while(num2 < num1)
   {
      cout <<“Please enter your second number”<<endl;// ask user for second number
      cin >> num2;                                  
   }

   for(result = num2 – num1; result >= 0; result–) //declaring result as the difference between 2 numbers
      {
     
      sum += result + num1; // calculations to add the difference of the numbers
     
      }

   cout << sum <<endl; //displays teh difference of your 2 numbers

   cout <<”       1.) Get the sum of two numbers”<<endl; //starting menu
    cout <<”       2.) Exit Program”<<endl;

    cout <<“Enter a number from the menu to select”; //asking for a selection
    cout <<endl;
    cout <<endl;
    cin >> number1;

    switch (number1) // switches number one with a number
    {
      case 1: notevil = false; // switches number1 to true
         break;
      case 2: notevil = true;
         break;
    }

  }
 
   
            
   
   system(“pause”);
   return 0;
}

   
            
 

While Loops

#include <iostream> //Programmer Rant4u
#include <string> //using while loops
#include <cmath> // 3/12/12
using namespace std;
int main()
{
char ch1, ch2;
int number1, num1;
string word;
bool word2 = false;

cout <<“1.) Would you like to play with bools?”<<endl; //starting menu
cout <<“2.) Would you like to exit the program?”<<endl;

cout <<“Enter a number from the menu to select”; //asking for a selection
cout <<endl;
cout <<endl;
cin >> number1;

switch (number1) // switches number one with a number
{
case ‘1’: number1 = 1;
break;
case ‘2’: number1 = 2;
break;

}

if (number1 == 1) // executes when 1 is selected
{
cout << “You have selected to play with bools, here we go!!”<<endl; //output if 1 is slected
cout <<“Is it true that you can watch the world turn? (y or N)”<<endl;
cin >> ch2;

while(ch2 == ‘y’) //enter the loop when the user beilieves you can watch teh earth turn
{

cout <<“I believe that is incorrect try again (y or n)”<<endl; // give them another shot
cin >> ch2;
ch2 = toupper(ch2);
switch (ch2) //swith char to upper n lower
{
case ‘Y’: ch2 = ‘y’; //actually this does nothing
break;
case ‘N’: ch2 = ‘n’;
break;

}
if (ch2 == ‘n’) //if they admit they cant watch the earth turn
{
cout << “your right congrats”<<endl; //ends the program when they admit they cant
system (“pause”);
return 0;
}
else
{
cout << “try again please”<<endl; //asking them to try again
cin >> ch2;
}

}

}

if(number1 == 2) //selection from the menu
{
cout <<” you can exit the program after you enter the name of this class”<<endl; //output
cin >> word;

while (word != “c++”) //if tehy enter the name of the class correctly
{

if(word == “c++”)
{
cout << “you may now exit, cause your soo samrt”; //they can exit if they get it right
}
else
{
cout <<“that is not the name of the class…”<<endl; // asking for the right answer
cout <<“lets try it again:)”<<endl;
cout <<” you can exit the program after you enter the name of this class”<<endl;
cin >> word;
}
}

}

system (“pause”);
return 0; //end of program
}

C++ Making a Menu

#include <iostream> //Programmer: N/A
#include <string> //MidTerm Assignment 3/10/12
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    double radius, area, length1, length2, base, height; //declares variables throughout the program
    float num1, num2, num1result1, num2result2;
    string verb, place, action, descrip, circle, square, triangle;
    string word1result1, word2result2;
    char chX;
    string object1, word1, word2;
   
    cout <<“***************************************”<< endl; // this is the menu
    cout <<“*\tPlease select an option       *”<<endl;
    cout <<“*                                     *”<<endl;
    cout <<“*     1: Calculate area of a shape    *”<<endl;
    cout <<“*     2: Do a small mad lib           *”<<endl;
    cout <<“*     3: Swap some variables          *”<<endl;
    cout <<“*     4: Exit program                 *”<<endl;
    cout <<“*                                     *”<<endl;
    cout <<”    What is your choice:” <<endl;
    cin >> chX;
    switch(chX)
    {
        case’1′ : chX = ‘1’;// switching the char(chX) with 4 different selections
        break;
        case’2′ : chX = ‘2’;
        break;
        case’3′ : chX = ‘3’;
        break;
        case’4′ : chX = ‘4’;
        break;
        default: cout <<“you gave the wrong input, Run the program again”<<endl;
    }
    system(“cls”);
   
        if(chX == ‘1’) //user input “1” will run this section
        {
            cout <<“enter an object: circle, square, triangle: “; //selecting an object
            cin >> object1;
           
            if(object1 == “circle”) //will caluclate the area of a circle
            {
            cout <<endl;
            cout <<“Enter the radius: “;
            cin >> radius;
            cout << endl;
            area = 3.14 * pow(radius, 2);
            cout  << “The area is: ” << setprecision(10) << area <<endl; //setprecision is NOT working
            }
           
            if(object1 == “square”) //will calcualte the area of a square
            {
            cout <<endl;
            cout <<“Enter the length of both sides: “;
            cin >> length1 >> length2;
            cout << endl;
            area = length1 * length2;
            cout  << “The area is: ” << setprecision(10) << area <<endl; //I do NOT like setprecision
            }
           
            if(object1 == “triangle”)//will calculate the area of a triangle
            {
            cout <<endl;
            cout <<“Enter the base and the height: “;
            cin >> base >> height;
            cout << endl;
            area = .5 * base * height;
            cout  << “The area is: ” << setprecision(10) << area <<endl; //I have tried everything to
            } // fix this precision error, conculsion = I dont like setprecision and it does not like me
           
            else
            {
            cout <<endl; //if the user input something other than the shapes listed will out put this
            cout <<“You gave the wrong input. Run the program again”<<endl;
            }
            cout << “Press enter to exit.”; //custom exit of the program
            cin.ignore();
            cin.get();
            return 0;
        }
       
        if(chX == ‘2’) //if # 2 is selected do the following lines of code
        {
            cout << endl;
            cout <<“Enter a verb: “;
            cin >> verb; //user input a verb
            cout << endl;
            cout <<“Enter a place: “;
            cin >> place; //user input a place
            cout << endl;
            cout <<“Enter an action : “;
            cin >> action; //user input an action
            cout << endl;
            cout <<“Enter a description : “;
            cin >> descrip; // user input a description
            cout << endl;
            cout <<“This ” << verb << ” test at ” << place << ” is too ” << descrip <<“!”<<endl;
            cout <<“It can go ” << action << ” off a cliff!”<<endl; //combines users inputs
       
            cout << “Press enter to exit.”; //custom exit of the program
            cin.ignore();
            cin.get();
            return 0;
        }
       
        if(chX == ‘3’) //when #3 is selected the following lines of code will run
        {
            cout <<“Enter a number, two words ad another number”<<endl; //asking for input
            cin >> num1 >> word1 >> word2 >> num2; //the actual input part
            cout <<“Word one is: ” << word1 <<endl; //telling the user what they just typed
            cout <<“Word two is: ” << word2 <<endl;
            cout <<“Number one is: ” <<num1 <<endl;
            cout <<“Number two is: ” <<num2 <<endl;
            cout <<“switching!!!”<<endl;
           
            word1result1 = word1; //swaps inputs around
            word2result2 = word2;
            num2result2 = num2;
            num1result1 =num1;
           
            cout <<“word one is now: ” << word2result2 <<endl; //displays the swap for the user
            cout <<“word two is now: ” << word1result1 <<endl;
            cout <<“number one is now: ” << num2result2 <<endl;
            cout <<“Number two is now: ” << num1result1 <<endl;
           
            cout << “Press enter to exit.”; //custom exit of the program
            cin.ignore();
            cin.get();
            return 0;
        }
        if(chX == ‘4’) // the following lines of code will run if #4 is selected
        {
            cout << “Press enter to exit.”; //custom exit of the program
            cin.ignore();
            cin.get();
            return 0;
        }
        else(chX > 4);//if any number greater than 4 is enter this will outout
        {
            cout << “you gave the wrong input, Run the program again”<<endl;
        }
       
       
    system (“pause”);
    return 0;
}

C++ MidTerm first page

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    cout <<“***************************************”<< endl;
    cout <<“*\tPlease select an option       *”<<endl;
    cout <<“*                                     *”<<endl;
    cout <<“*     1: Calculate area of a shape    *”<<endl;
    cout <<“*     2: Do a small mad lib           *”<<endl;
    cout <<“*     3: Swap some variables          *”<<endl;
    cout <<“*     4: Exit program                 *”<<endl;
    cout <<“*                                     *”<<endl;
    cout <<”    What is your choice:” <<endl;

    system (“pause”);
    return 0;
}

C++ MidTerm

I will be posting my program for the mid term.  I assume that it will have some problems in it.  If anyone has input on how to resolve these issues, please don’t hesitate to comment.  The program is due 3/12/12.  I will get started on the code 3/10/12.  Thanks in advance.