**疫情期间学习C++
4-1
需要使用cin.get()设置读取位数,避免空格导致无法读取多个词
#include <iostream>
struct info_people //define structure
{
char first_name[20];
char last_name[20];
char grade;
unsigned int age;
};
int main()
{
using namespace std;
info_people* pd = new info_people;
cout << "What is your first name?";
cin.get(pd->first_name, 20);
cout << endl << "What is your last name?";
cin >> (*pd).last_name;
cout << endl << "What letter grade do you deserve?";
cin >> (*pd).grade;
cout << endl << "What is your age?";
cin >> pd->age;
cout << endl << "Name: " << (*pd).first_name << ", " << (*pd).last_name << endl;
cout << "Grade: " << char((*pd).grade + 1) << endl;
cout << "Age: " << (*pd).age << endl;
delete pd;
return 0;
}
4-2
getline(cin,name)用于读取一行的string
#include <iostream>
#include <string>
int main()
{
using namespace std;
string name;
string dessert;
cout << "Enter your name:\n";
getline(cin, name);
cout << "Enter your favourite dessert:\n";
getline(cin, dessert);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
4-3
char设置长度,没空格可以直接cin
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
const int size = 15;
char first_name[size];
char last_name[size];
string dessert;
cout << "Enter your fisrt name: ";
cin >> first_name;
cout << "\nEnter your last name: ";
cin >> last_name;
cout << "\nHere's the information in a single string: " << first_name << ", " << last_name << endl;
return 0;
}
4-4
不含空格同样可以直接cin
#include <iostream>
#include <string>
int main()
{
using namespace std;
string first_name;
string last_name;
string dessert;
cout << "Enter your fisrt name: ";
cin >> first_name;
cout << "\nEnter your last name: ";
cin >> last_name;
cout << "\nHere's the information in a single string: " << first_name << ", " << last_name << endl;
return 0;
}
4-5
#include <iostream>
struct CandyBar
{
char brand[20];
float weight;
unsigned int cal;
};
int main()
{
using namespace std;
CandyBar snack =
{
"Mocha Munch", 2.3, 350
};
cout << "Brand: " << snack.brand << "\nWeight: " << snack.weight << "\nCal: " << snack.cal << endl;
return 0;
}
4-6
不知道题目的意思理解错了没有。。。
#include <iostream>
struct CandyBar
{
char brand[20];
float weight;
unsigned int cal;
};
int main()
{
using namespace std;
CandyBar snack;
cout << "Enter the brand: ";
cin >> snack.brand;
cout << "\nEnter the weight: ";
cin >> snack.weight;
cout << "\nEnter the cal: ";
cin >> snack.cal;
cout << "Brand: " << snack.brand << "\nWeight: " << snack.weight << "\nCal: " << snack.cal << endl;
return 0;
}
4-7
#include <iostream>
#include <string>
struct Pissa_Info
{
std::string brand;
float diameter;
float weight;
};
int main()
{
using namespace std;
Pissa_Info pissa;
cout << "Enter the brand: ";
getline(cin, pissa.brand);
cout << endl << "Enter the diameter: ";
cin >> pissa.diameter;
cout << endl << "Enter the weight: ";
cin >> pissa.weight;
cout << "Brand: " << pissa.brand << "\nDiameter: " << pissa.diameter << "\nWeight: " << pissa.weight<< endl;
return 0;
}
4-8
cin数字之后,要使用getline的话需要先用cin.get()跳过换行符
#include <iostream>
#include <string>
using namespace std;
struct Pissa_Info
{
string brand;
float diameter;
float weight;
};
int main()
{
Pissa_Info* pd = new Pissa_Info;
cout << "Enter the diameter: ";
cin >> pd->diameter;
cin.get();
cout << endl << "Enter the brand: ";
getline(cin, pd->brand);
cout << endl << "Enter the weight: ";
cin >> pd->weight;
cout << "Brand: " << (*pd).brand << "\nDiameter: " << (*pd).diameter << "\nWeight: " << (*pd).weight<< endl;
delete pd;
return 0;
}
4-9
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string brand;
float weight;
unsigned int cal;
};
int main()
{
CandyBar* pd = new CandyBar;
cout << "Enter the brand: ";
getline(cin, pd->brand);
cout << "\nEnter the weight: ";
cin >> pd->weight;
cout << "\nEnter the cal: ";
cin >> pd->cal;
cout << "Brand: " << (*pd).brand << "\nWeight: " << (*pd).weight << "\nCal: " << (*pd).cal << endl;
delete pd;
return 0;
}
4-10
#include
#include
int main()
{
using namespace std;
array<float, 3> grade;
cout << "The first time finish 40m: ";
cin >> grade[0];
cout << "\nThe second time finish 40m: ";
cin >> grade[1];
cout << "\nThe third time finish 40m: ";
cin >> grade[2];
cout << "Average time is " << (grade[0] + grade[1] + grade[2]) / 3 << endl;
return 0;
}