Question

In: Computer Science

Read Ch. 3. Using the C++ editor, type the following program: #include <iostream> #include <string> using...

  1. Read Ch. 3. Using the C++ editor, type the following program:

#include <iostream>

#include <string>

using namespace std;

int main ()

{

       //declarations

string name;

       //input

cout <<"Enter your first name" << endl;

       cin >> name;

       //output

       cout << "Hello " << name << "! " << endl;

       return 0;

}

  1. Compile and run. What does the cin statement do?
  2. What does the cout statement do?
  3. Is name a variable or a constant?
  4. What type is name?
  5. Add the following line to the declaration statements:

int age;

Add the following lines to your program:

cout << “Enter your age” << endl;

cin >> age;

Compile and Run.

What type is age? Describe this type.

  1. Add a line to print the age to the screen. Compile and Run.
  2. Add the following line to the declaration statements:

float bodyTemp;

              Add the lines to input and out your body temperature. Compile and run.

              What type is bodyTemp? Describe this type.

  1. Add the appropriate declaration, input and output statement for each of the following:

Height (hint, use two variables)

Weight

Sex (hint, use the char type)

Last name

  1. Turn in:
    1. The final program
    2. The final output

Answers to these questions only:

  1. What is the integer data type and how is it declared in C++?
  2. Give an example of where the integer type might be used.
  3. What is the real data type and how is it declared in C++?
  4. Give an example of where the real type might be used.
  5. What is the string data type and how is a string declared in C++?
  6. Give an example of where the string type might be used.
  7. What is the boolean type and how is an boolean declared in C++?
  8. Give an example of where the boolean type might be used.

Solutions

Expert Solution

Here is my detailed complete solution to your question. If you have any doubts regarding my answer please tell me in the comments. I would be very happy to help you.

CODE TO COPY:

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

int main ()

{
//declarations
string name,last_name,g;
int age;
float bodyTemp,height,weight;
char gender;
  
//input
cout <<"Enter your first name" << endl;
cin >> name;
cout <<"Enter your Last name" << endl;
cin >> last_name;
cout << "Enter your age"<< endl;
cin >> age;
cout << "Please Enter your Body Temperature (in °F)"<< endl;
cin>> bodyTemp;
cout <<"Enter your Gender (Type M for Male or F for Female or O for Others )" << endl;
cin >> gender;
cout <<"Enter your Height (in centimeters)" << endl;
cin >> height;
cout <<"Enter your Weight (in Kgs)" << endl;
cin >> weight;
  
if (gender == 'M')
g = "Male";
else if (gender == 'F')
g = "Female";
else
g = "Others";


//output
cout << "Hello " << name <<" "<<last_name<< "! " << endl;
cout << "Your Age is : "<< age << endl;
cout << "Your Body Temperature is : "<<bodyTemp<<"°F"<< endl;
cout << "Your Gender is : "<< g <<endl;
cout << "Your Height is : "<< height <<" cm"<<endl;
cout << "Your Weight is : "<< weight <<" kg"<<endl;
  

return 0;

}

SCREENSHOT OF CODE:

SCREENSHOT OF OUTPUT 1:

SCREENSHOT OF OUTPUT 2:

cin statement is used to take input from the user through the keyboard.

cout statement is used to print out the output on the screen.

'name' is a variable of data-type string that stores a string of characters.

name is of string data-type.

age is of integer type(int) because age will always be an integer number. This data-type can store integer numbers without decimals.

body temperature is of the float data type. Float data-type can store floating-point numbers up to 2 decimals.

//Answers to these questions only:

int is the name of the data type for storing integers. It is declared as - int variable_name. For example, int age.

an integer data type can be used where you want to store a number.  

real data type or floating data type is used to store a numerical value with decimals. It is declared as float variable_name. For example, float temparature.

real data type is used to store height, weight, temperature.

string data type is used to store text written within single-quotes or double-quotes. It is declared as string variable_name. For example, string name.

string data type is used to store names, address etc.

boolean data type is used to check whether the condition is true or false as it stores only 2 values true or false. It is declared as bool b = True or bool a = 10


Related Solutions

C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion:...
C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion: code steps 1 thru 4 then test then add requirement 5, then test, then add 6, then test etc. Add comments to display assignment //step 1., //step 2. etc. This program is to have no programmer created functions. Just do everything in main and make sure you comment each step so I can grade more easily. Also, this program will be expanded in Chapter...
this is cahce memory related c program....and not working or running properly??????? #include <iostream> #include <string>...
this is cahce memory related c program....and not working or running properly??????? #include <iostream> #include <string> #include <vector> #include <iomanip> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; int cash_type, block_size, cash_size,number_of_blocks=0;; int compulsry_misses=0, capcity_misses=0, conflict_misses=0; #define DRAM_SIZE (64*1024*1024) unsigned int m_w = 0xABABAB55; /* must not be zero, nor 0x464fffff */ unsigned int m_z = 0x05080902; /* must not be zero, nor 0x9068ffff */ unsigned int rand_() { m_z = 36969 * (m_z & 65535) + (m_z >>...
For C++ Consider the following code defining classes for assets and office equipment: #include<string> #include<iostream> using...
For C++ Consider the following code defining classes for assets and office equipment: #include<string> #include<iostream> using namespace std; // class definition for asset class Asset{ protected: int value; // value of asset in cents public: Asset(int value); // constructor int get_value(); // get the value }; Asset::Asset(int val){ // implementation of constructor value=val; } int Asset::get_value(){ // implementation of get_value return value; } // abstract class for office equipment class OfficeEquipment:public Asset{ public: OfficeEquipment(int val); // constructor virtual void use_item()...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits>...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits> #include <vector> using namespace std; // // CLASS: NODE // class Node{ public: int value = 0; // our node holds an integer Node *next = nullptr; // our node has a pointer to the next Node Node(int i){ // contructor for our Node class value = i; // store a copy of argument "i" in "value" next = nullptr; // be sure next...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace std; AVLTree::AVLTree() { root = NULL; } AVLTree::~AVLTree() { delete root; root = NULL; } // insert finds a position for x in the tree and places it there, rebalancing // as necessary. void AVLTree::insert(const string& x) { // YOUR IMPLEMENTATION GOES HERE } // remove finds x's position in the tree and removes it, rebalancing as // necessary. void AVLTree::remove(const string& x) {...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned char c1; unsigned char c2; unsigned char c3; unsigned char c4; unsigned long i1 (0xaabbccee); _asm { } cout.flags (ios::hex); cout << "results are " << (unsigned int) c1 << ", " << (unsigned int) c2 << ", " << (unsigned int) c3 << ", " << (unsigned int) c4 << endl; } Inside the block denoted by the _asm keyword, add code to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT