#include <iostream>
int main()
{
using namespace std;
const int ArSize = 20;
char first[ArSize];
char last[ArSize];
char grade;
int age;
cout << "What is your first name?";
cin.getline(first, 20);
cout << "What is your last name?";
cin.getline(last, 20);
cout << "What letter grade do you deserve?";
cin >> grade;
cout << "What is your age?";
cin >> age;
cout << "Name: " << last << ", " << first << endl;
cout << "Grade: " << char(grade + 1) << endl;
cout << "Age: " << age << endl;
return 0;
}
待解决问题:
- 关于输入带空格的问题?
2. [1]
#include <iostream>
#include <string>
int main()
{
using namespace std;
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 delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
3.
#include <iostream>
int main()
{
using namespace std;
const int ArSize = 20;
char first[ArSize];
char last[ArSize];
cout << "Enter your first name:";
cin.getline(first, ArSize);
cout << "Enter your last name:";
cin.getline(last, ArSize);
cout << "Here's the information in a single string:" << last << ", " << first << endl;
return 0;
}
#include <iostream>
#include <string>
int main()
{
using namespace std;
string first;
string last;
cout << "Enter your first name:";
getline(cin, first);
cout << "Enter your last name:";
getline(cin, last);
cout << "Here's the information in a single string:" << last << ", " << first << endl;
return 0;
}
#include <iostream>
int main()
{
using namespace std;
struct CandyBar
{
char brand[20];
double weight;
int calorie;
};
CandyBar snack = {"Mocha Munch", weight: 2.3, .calorie = 350}; //brand: {'M', 'o', 'c', 'h', 'a', ' ', 'M', 'u', 'n', 'c', 'h'}.
cout << "brand:" << snack.brand << endl;
cout << "weight:" << snack.weight << endl;
cout << "calorie:" << snack.calorie << endl;
return 0;
}
6.
#include <iostream>
int main()
{
using namespace std;
struct CandyBar
{
char brand[20];
double weight;
int calorie;
};
CandyBar snack[3] =
{
{"Mocha Munch", 2.3, 350},
{"dabaitu",2.8, 300},
{"Alpine", 2.2, 320}
};
cout << "brand0:" << snack[0].brand << endl;
cout << "weight0:" << snack[0].weight << endl;
cout << "calorie0:" << snack[0].calorie << endl;
cout << "brand1:" << snack[1].brand << endl;
cout << "weight1:" << snack[1].weight << endl;
cout << "calorie1:" << snack[1].calorie << endl;
cout << "brand2:" << snack[2].brand << endl;
cout << "weight2:" << snack[2].weight << endl;
cout << "calorie2:" << snack[2].calorie << endl;
return 0;
}
7.
#include <iostream>
int main()
{
using namespace std;
struct Pizza
{
char company[20];
double diameter;
double weight;
};
Pizza snack;
cout << "Please enter the company of the pizza:";
cin.getline(snack.company, 20);
cout << "Please enter the diameter of the pizza:";
cin >> snack.diameter;
cout << "Please enter the weight of the pizza:";
cin >> snack.weight;
cout << "company: " << snack.company << endl;
cout << "diameter: " << snack.diameter << endl;
cout << "weight: " << snack.weight << endl;
return 0;
}
待解决问题:
1.输出的company分成了两行?
#include <iostream>
int main()
{
using namespace std;
struct Pizza
{
char company[20];
double diameter;
double weight;
};
Pizza *ps = new Pizza;
cout << "Please enter the diameter of the pizza:";
cin >> (*ps).diameter;
cin.get();
cout << "Please enter the company of the pizza:";
cin.getline(ps->company, 20);
cout << "Please enter the weight of the pizza:";
cin >> ps->weight;
cout << "diameter: " << (*ps).diameter << endl;
cout << "company: " << ps->company << endl;
cout << "weight: " << ps->weight << endl;
return 0;
}
9.
#include <iostream>
int main()
{
using namespace std;
struct CandyBar
{
char brand[20];
double weight;
int calorie;
};
CandyBar *ps = new CandyBar [3]; //CandyBar *ps = new CandyBar;这么写也能正常运行。
cout << "Please enter the brand0 of the candy:";
cin.getline(ps[0].brand,20);
cout << "Please enter the weight0 of the candy:";
cin >> ps[0].weight;
cout << "Please enter the caorie0 of the candy:";
cin >> ps[0].calorie;
cout << "Please enter the brand1 of the candy:";
cin.get();
cin.getline(ps[1].brand,20);
cout << "Please enter the weight1 of the candy:";
cin >> ps[1].weight;
cout << "Please enter the caorie1 of the candy:";
cin >> ps[1].calorie;
cout << "Please enter the brand2 of the candy:";
cin.get();
cin.getline(ps[2].brand,20);
cout << "Please enter the weight2 of the candy:";
cin >> ps[2].weight;
cout << "Please enter the caorie2 of the candy:";
cin >> ps[2].calorie;
cout << "brand0:" << ps[0].brand << endl;
cout << "weight0:" << ps[0].weight << endl;
cout << "calorie0:" << ps[0].calorie << endl;
cout << "brand1:" << ps[1].brand << endl;
cout << "weight1:" << ps[1].weight << endl;
cout << "calorie1:" << ps[1].calorie << endl;
cout << "brand2:" << ps[2].brand << endl;
cout << "weight2:" << ps[2].weight << endl;
cout << "calorie2:" << ps[2].calorie << endl;
return 0;
}
待解决问题:
- 声明的动态数组元素个数小于实际的输入个数?
参考资料:
[1] 4.3.3 string类I/O