Tuesday, June 21, 2011

June 21

Been very  busy between research papers and presentations I had to make for school, I'll get a post on what ive been doing late this week or early next week.

Tuesday, June 7, 2011

We have been discussing arrays in the past week.
Our assignment was to create a program that took the first and last name of the user and output it in reverse order.



 
   

   
    cout << "please enter your first name: " << endl;
    char str1[30];
    cin >> str1;
   
   
    cout << "please enter your last name: " << endl;
    char str2[20];
    cin >> str2;
   
   
    cout << "Your name before manipulation: " << str1 << " " << str2 << endl;
   
   
    cout << "your name after: " << str2;
    strcpy(str2, str1);
    cout << " " << str2 << endl;
   
   
comments:
I could have eliminated 2 separate lines for the cin input, and just did,
cin >> str1 >> str2
char str1[30];
char str2[25];

or something close to that.
I also feel it kind of redundant the way we have to reverse order.
Cout str2 and then copy the value of str1 into str2 and output it again.
A better way I think is we could just cout >> str2 >> str1
This eliminates extra code, along with if you have to use str2 again latter on the in the program you can use it as the "real" value of str2
   
   
   
   
 

Wednesday, June 1, 2011

June 1!!!

We user defined simple data and string types(last week) this week we are working with arrays.

In Chapter 8, there is a discussion of the "Rock, Paper, Scissors" game. Discuss how this game works, and how a C++ program that models this game will use a variety of types.

(may have done this wrong)

First it defines the object rock, paper, and scissor.
After that the function prototypes are defined.

Displayrules are called upon entering the game and display the Cout statements on page 446. 

Objecttype gets the keystroke that the user entered through a switch structure. It uses both uppercase and lowercase to decide. To passes the object that was selected to gameresults.

Gameresults, this calls winningobject to decide how the game should go; or, the truth table for the game.

After the truth table is found, it proceeds through game results first to see if the game is a tie, then it outputs each player’s choice. It then compares each player’s objects that they chose to the winningobject, via the convertenum function, and outputs the winner.

The convert enum function converts each of the enum defined(rock, paper, scissors) to something that can be recognized by C++(a cout statement)

Display results outputs the number of games played and who many times each player has won a game, the wins for player one is stored in wCount1 and the wins for player 2 is stored in wCount2. The loop for incrementing each counting sequence is in the main body of the program.






comment by instructor: Great job in breaking this down here, with "enum" in this case, would this be a function? How would this be defined?




my response
If I understand your question correctly, no, the:
enum objectType {rock, paper, scissors}
is no a function.

enum does nothing more than create a user defined data type.
So instead of
char rock, paper, scissors;
you have enum ObjecType {rock...ect

This is the reason you have to have a new function to "read" the data in the enum. Which is where the convert enum function comes into play(on page 449)

To define an enum data type use the following code:
enum TypeName {value1, value2, ect}