Chapter 4 programming problems of c++ primer plus

The fourth chapter

1 output object properties as required

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

struct man
{
    char first_name[20];
    char last_name[20];
    char grade[1];
    int age;
};

int main()
{
    man man1;
    cout << "What is your first name? : ";
    cin.getline(man1.first_name,20);
    cout << "What is yout last name? : ";
    cin.getline(man1.last_name,20);
    cout << "What letter grades do you deserve? : ";
    cin >> man1.grade;
    cout << "What is your age? : ";
    cin >> man1.age;
    cout << "Name: " << man1.last_name << ' ' << man1.first_name << endl;
    cout << "Grade: " <<  char(man1.grade[0]+1) << endl;
    cout << "Age: " << man1.age << endl;
    return 0;
}

2 rewrite the program with string 4.4

#include <iostream>
#include <string.h>

int main()
{
    using namespace std;
    const int ArSize = 20;
    string name;
    string dessert;

    cout << "Enter your name:\n";
    getline(cin,name);
    cout << "Enter your favorite dessert:\n";
    getline(cin,dessert);
    cout << "I have some delicions " << dessert;
    cout << " for you, " << name << " \n";
    return 0;
}

3. Enter the last name and first name, and combine them with comma and space to form a new string (using char)

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

int main()
{
    int i = 0;

    cout << "Input your first name: ";
    char firstname[21];
    cin >> firstname;
    
    cout << "Input your last name: ";
    char lastname[21];
    cin >> lastname;
    char name[42];
    
    for(i = 0; i < strlen(lastname); i++)
    {
        name[i] = lastname[i];
    }
    name[i] = ','; i++;
    name[i] = ' '; i++;

    for(int j = 0; j < strlen(firstname); j++)
    {
        name[i] = firstname[j];
        i++;
    }
      
    cout << "Your name is: " << name << endl;
    return 0;
}

4. Enter the last name and first name, and combine them with comma and space to form a new string (using string)

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

int main()
{
    cout << "Input your first name: ";
    string firstname;
    getline(cin,firstname);
    cout << "Input your last name: ";
    string lastname;
    getline(cin,lastname);
    lastname += ", ";
    lastname += firstname;
    cout << "Your name is: " << lastname << endl;
    return 0;
}

5 use structure to store the information of sugar block: initialize when declaring and output the information

#include <iostream>
using namespace std;

struct CandyBar
{
    string brand;
    double weight;
    int calories;
};


int main()
{
    CandyBar snack{"Micha Munch",2.3,350};
    cout << "The " << snack.brand 
         << "\'s weight is " << snack.weight
         << " and it has " << snack.calories
         << " calories" << endl; 
    return 0;
}

6 later in the previous question, create a CandyBar array with three elements, initialize it to the selected value, and display the contents of each structure

#include <iostream>
using namespace std;

struct CandyBar
{
    string brand;
    double weight;
    int calories;
};

int main()
{
    CandyBar snacks[3];
    for(int i = 0; i < 3; i++)
    {
        cout << "Input No." << i+1 << "Information for elements" << endl;
        cout << "Enter brand:";
        cin >> snacks[i].brand;
        cout << "Input weight:";
        cin >> snacks[i].weight;
        cout << "Heat input:";
        cin >> snacks[i].calories;
    }

    for(int i = 0; i < 3; i++)
    {
        cout << "The first" << i+1 << "Element" << endl; 
        cout << "brand: " << snacks[i].brand << endl;
        cout << "weight: " << snacks[i].weight << endl;
        cout << "Quantity of heat: " << snacks[i].calories << endl;

    }
    return 0;
}

Design pizza company information storage, use structure

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

struct Pizza
{
    char company[21] = "a";
    double diameter = 0;
    double weight = 0;
};

int main()
{
    Pizza pizzalst[3];
    for(int i = 0; i < 3; i++)
    {
        cout << "Input No." << i+1 << "Pizza information" << endl;
        cout << "Enter company:";
        cin.getline(pizzalst[i].company,20);
        cout << "Input diameter:";
        cin >> pizzalst[i].diameter;
        cin.get();
        cout << "Input weight:";
        cin >> pizzalst[i].weight;
        cin.get();
    }

    for(int i = 0; i < 3; i++)
    {
        cout << "The first" << i+1 << "Pizza" << endl; 
        cout << "company: " << pizzalst[i].company << endl;
        cout << "diameter: " << pizzalst[i].diameter << endl;
        cout << "weight: " << pizzalst[i].weight << endl;

    }
    return 0;
}

8 on the basis of 7, change the program to use new to allocate memory. In addition, let the program enter the pizza diameter before requesting the company name

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

struct Pizza
{
    char company[21] = "a";
    double diameter = 0;
    double weight = 0;
};

int main()
{
    Pizza *pizzalist = new Pizza[3];
    for(int i = 0; i < 3; i++)
    {
        cout << "Input No." << i+1 << "Pizza information" << endl;
        cout << "Input diameter:";
        cin >> pizzalist[i].diameter;
        cin.get();
        cout << "Enter company:";
        cin.getline(pizzalist[i].company,20);
        cout << "Input weight:";
        cin >> pizzalist[i].weight;
        cin.get();
    }

    for(int i = 0; i < 3; i++)
    {
        cout << "The first" << i+1 << "Pizza" << endl; 
        cout << "company: " << pizzalist[i].company << endl;
        cout << "diameter: " << pizzalist[i].diameter << endl;
        cout << "weight: " << pizzalist[i].weight << endl;

    }
    return 0;
}

9 on the basis of 6. Use new to allocate array dynamically

#include <iostream>
using namespace std;

struct CandyBar
{
    string brand;
    double weight;
    int calories;
};

int main()
{
    CandyBar *snacks = new CandyBar[3];
    for(int i = 0; i < 3; i++)
    {
        cout << "Input No." << i+1 << "Information for elements" << endl;
        cout << "Enter brand:";
        getline(cin,snacks[i].brand);
        cout << "Input weight:";
        cin >> snacks[i].weight;
        cin.get();
        cout << "Heat input:";
        cin >> snacks[i].calories;
        cin.get();
    }

    for(int i = 0; i < 3; i++)
    {
        cout << "The first" << i+1 << "Element" << endl; 
        cout << "brand: " << snacks[i].brand << endl;
        cout << "weight: " << snacks[i].weight << endl;
        cout << "Quantity of heat: " << snacks[i].calories << endl;

    }
    return 0;
}

10 input three run scores, display times and average scores, and use array storage

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


int main()
{
    cout << "Please enter the score of three runs:" << endl;
    array<double , 3>  scores;
    for(int i = 0; i < 3; i++)
    {
        cin >> scores[i];
    }
    double average = 0;
    average = (scores[0] + scores[1] + scores[2]) / 3;
    cout << "The average score is: " << average << endl;
    return 0;
}

Keywords: Programming

Added by kingman65 on Sat, 16 Nov 2019 20:00:40 +0200