What is your first name? Betty Sue
What is your last name? Yewe
What letter grade do you deserve? B
What is your age? 22
Name:Yewe,Betty Sue
Grade:C
Age:22
#include
#include
using namespace std;
int mian()
{
cout<<"What is your first name?";
string first_Name;
//char char firstName[20];
//cin.getline(firstName,20);
getline(cin,first_Name);
cout<<"What is your last name?";
string last_Name;
//char lastName[20];
//cin.getline(lastName,20);
//注1
getline(cin,last_Name);
cout<<"What letter grade do you deserve?";
char deserve;
cin>>deserve;
cout<<"What is your age? "
int age;
cin>>age;
cout<<"Name:"<<last_Name<<","<<first_Name<<endl;
cout<<"Grade:"<<++deserve<<endl;
cout<<"Age:"<<age<<endl;
system("pause");
return 0;
}
注1: 其中,cin较为常用,但在接受“空格”、“回车”和“TAB”时都会结束。而本段代码采用cin.getline(),cin.getline(char * , int)函数,第一个参数为指向char的指针,通常为数组首地址;第二个参数为int型变量,来限制数组字符串长度。
#include
#include
int main()
{
using namespace std;
string name;
string dessert;
cout << "Enter your name:\n";
getline(cin, name); // reads through newline
cout << "Enter your favorite dessert:\n";
getline(cin,dessert);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
system("pause");
return 0;
}
Enter your first name: Flip
Enter your last name: Fleaming
Here's the information in a single string: Fleming, Flip
#include
#include
using namespace std;
const int Strlen = 20;
int main()
{
char firstName[Strlen];
char lastName[Strlen];
char fullName[2*Strlen+1];
cout<<"请输入你的名称:";
cin.getline(lastName,Strlen);
cout<<"请输入你的姓:";
cin.getline(firstName,Strlen);
strcpy(fullName,firstName);
strcat(fullName,",");
strcat(fullName,lastName);
cout<<"你的姓名是:"<<fullName<<endl;
system("pause");
return 0;
}
Enter your first name: Flip
Enter your last name: Fleaming
Here's the information in a single string: Fleming, Flip
#include
#include
int main()
{
string firstName;
string lastName;
string fullName;
cout<<"请输入你的名称:";
getline(cin,lastName);
cout<<"请输入你的姓氏:";
getline(cin,firstName);
fullName = firstName+","+lastName;
cout<<"你的姓名是:"<<fullName<<endl;
system("pause");
return 0;
}
#include
#include
using namespace std;
struct CandyBar {
string brand;
float weight;
int calorie;
};
int main()
{
CandyBar snack = {"Mocha Munch,",2.3,350};
cout << "brand: " << snack.brand << endl;
cout << "weight: " << snack.weight << endl;
cout << "calorie: " << snack.calorie << endl;
system("pause");
return 0;
}
#include
#include
using namespace std;
struct CandyBar {
string brand;
float weight;
int calorie;
};
int main()
{
CandyBar snack[3] = {
{ "Mocha Munch",2.3,350 },
{ "Mocha Munch",2.3,350 },
{ "Mocha Munch",2.3,350 }
};
for (int i = 0; i < 3; i++)
{
cout << "brand: " << snack[i].brand << endl;
cout << "weight: " << snack[i].weight << endl;
cout << "calorie: " << snack[i].calorie << endl;
}
system("pause");
return 0;
}
披萨饼公司的名称,可以有多个单词组成。
披萨饼的直径
披萨饼的重量
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。
#include
#include
using namespace std;
struct Pizza {
string company;
double diameter;
double weight;
};
int main()
{
Pizza pizza;
cout << "Please input pizza's company: ";
getline(cin, pizza.company);
cout << "Please input pizza's diameter: ";
cin>>pizza.diameter;
cout << "Please input pizza's weight: ";
cin>> pizza.weight;
cout << "Company: " << pizza.company << endl;
cout << "Diameter: " <<pizza.diameter << endl;
cout << "Weight: " << pizza.weight<< endl;
system("pause");
return 0;
}
#include
#include
using namespace std;
struct Pizza {
string company;
double diameter;
double weight;
};
int main()
{
Pizza *p=new Pizza;
cout << "Please input p's diameter: ";
cin >> p->diameter;
cin.get();
cout << "Please input p's company: ";
getline(cin, p->company);
cout << "Please input p's weight: ";
cin>>p->weight;
cout << "Company: " << p->company << endl;
cout << "Diameter: " <<p->diameter << endl;
cout << "Weight: " << p->weight<< endl;
system("pause");
return 0;
}
#include
#include
struct CandyBar {
std::string brand;
double weight;
int calorie;
};
int main()
{
using namespace std;
CandyBar *snack = new CandyBar[3];
for (int i = 0; i < 3; i++)
snack[i]={"Mocha Munch", 2.3, 350};
for (int i = 0; i < 3; i++)
{
cout << "brand: " << snack[i].brand << endl;
cout << "weight: " << snack[i].weight << endl;
cout << "calorie: " << snack[i].calorie << endl;
}
delete[] snack;
system("pause");
return 0;
}
#include
#include
int main()
{
using namespace std;
array<double, 3>grade;
for (int i = 0; i < 3; ++i)
{
cout << "Please input your first grade:";
cin >> grade[i];
}
cout << "Average three times:" << (grade[0] + grade[1] + grade[2]) / 3.0 << endl;
system("pause");
return 0;
}