Starting from:

$25

Conditionals_Lab 3 Solution

Lab 03 – Conditionals

Goals:

·         Use of conditionals

Development Environment: csegrid (Centos), g++ (all students must use csegrid)

Skills: declare variables, if and switch statements, use of and (&&), or (||), not (!)

Deliverables:1) This lab with3 screen shots, 2) lastnameFirstLab03.cpp

 

Part I – Skills Practice (5 pts)

·         Make a directory called lab03.  Create a file called lastnamefirstLab03a.cpp

·         Place the following text in the file (Do not cut and paste. You will learn more by typing it in.  If you do cut and paste all of the single and double quotes will be incorrect)

#include<iostream//needed for cin and cout

#include<string//needed to use string, and getline

usingnamespacestd; //needed for string and cin and cout

 

intmain()

{

     string name;

     char go, menu = 'z';

     cout<<"What is your full name (e.g. Tom Smith)?\n";

     getline(cin, name);

     cout<<"\nWould you like to continue (y or n) ?\n";

     cin go;

     if (go == 'y' || go == 'Y')

     {

           cout<<"A. Print A\n";

           cout<<"B. Print B\n";

           cin menu;

           switch (menu)

           {

           case'a':

           case'A':

                cout<<"A";

                break;

           case'b':

           case'B':

                cout<<"B";

                break;

           default:

                cout<<"not A or B";

           }//switch

     }//if

     else

           cout<<"Goodbye";

 

     return 0;

}

·         Save the file, exit out.  At the command prompt type (the -o is a little Oh not a zero), the lastname and first is your lastname and first (what ever you saved the file as)

g++ -o lab03a lastnamefirstlab03a.cpp

·         If you get errors, go back and fix them
If it compiles successfully, run it with
./lab03a

Ø  Take a screen shot of the successful output and place it below.  For a Windows 10 screen shot: Alt key + PrtSc key.  Then Ctrl + V to paste.  For Mac: Shift + Command + 4.  You will not hand in the code for Lab03a. You will not credit unless you have a successful screen shot with Your name in the output.

 

Part II –Troubleshooting Conditionals (5)

The two most common mistakes in conditionals are:

·         using = instead of ==

·         forgetting to put the break; statement in between cases.

 

·         Create a file called lastnamefirstinitialLab03b.cpp

#include<iostream//needed for cin and cout

#include<string//needed to use string, and getline

usingnamespacestd; //needed for string and cin and cout

 

intmain()

{

     int x = 5, y = 10;

     if (x = 20)

           cout<<"Used = instead of ==, making an aways true assignment statement\n";

     cout<<"because no { } the if conditional only covers one line.  This line is always printed\n";

 

     switch (x)

     {

     case 5: cout<<"five\n";

     case 10: cout<<"This line also prints because there is no break statement";

     }//switch

}//main

 

Ø  run it and take a screen shot of the errors.  Place the screen shot below.

 

Ø  Using the code above, change the (x=20) to (x==20), then add a break; after “five\n”;

take a screen shot.  Place the screen shot below

 

Part III-Rock Paper Scissors outline/pseudo-code/algorithms (5 pts)

·         Work with your lab partner to write an outline to complete the program in section IV.  Use plain English for your outline like this example

Making Peanut Butter and Jelly Sandwiches  

//Ask customer if they want both peanut butter and jelly

     //If peanut butter and jelly

           //Begin If

           //Spread peanut butter on bread

           //Spread jelly on bread

           //End If

     //Or else if just peanut butter

          //Begin If

           //Spread peanut butter on bread

           //End If

     Ask user if there are more sandwiches to make

·         Note use indenting to put actions under your loops and conditionals.  Note begin and end statements for blocks

 

Part IV -Rock Paper Scissors implementation. (10 pts)

·         Write a program to score the Rock-Paper-Scissors game.  Each of two users type in either R, P, or S.  The program then announces the winner as well as the basis for determining the winner: Paper covers rock, Rock breaks scissors, Scissors cut paper, or tie game.  Be sure to allow users to use lowercase as well as uppercase letters.  Your program should include a loop that lets the user play again until the user exits.

·         Call the program yourlastnameFirstinitialLab03.cpp (e.g. augustinetLab03b.cpp).  Every program you write in lab should have the following block at the top in comments.  Make sure to fill in the Name, Class, Description and Lab Partner.  Ensure your status is accurate.

/* Name:
Class: CSCI 1411-00X
Description:

Lab Partner:
Status: successfully compiled and run on csegrid /*

 

·         You should work with your lab partner on this, but each of you must write your own code.

·         Implement the program in part I, placing the actual code under each appropriate line of the outline.

 

 

·         Ensure your code is well documented, then ensure Part IVcode runs.

Ø  Add a screenshot of the output below

 

·         Ensure the screen shots from Part I, Part II, and Part IV are included then hand in this lab and yourlastnameLab01.cpp file into canvas.

More products