习题选自:C++ Primer Plus(第六版) 内容仅供参考,如有错误,欢迎指正 ! cin使用空白(空格、制表符和换行符)来确定字符串的结束位置(空格、制表符和换行符仍会留在输入队列)。(这意味着cin在获取字符数组输入时只读取一个单词,读取该单词后,cin将该字符串放到数组中,并自动在结尾添加空字符。注意当输入的是数字的时候,输入流中会自动忽略空格回车等控制字符。只有当输入的是字符时,才会保留'\n'在输入流中。) getline()函数每次读取一行,他通过换行符来确定行尾,但不保存换行符,在存储的字符串的时候,它用空字符替换换行符(换行符不会留在输入队列)。 get()具体工作方式与getline()类似,但get并不在读取并丢弃换行符,而是将其留在输入队列中。(幸运的是get()有另外一种变体,使用不带任何参数的cin.get()调用读取下一个字符(即使是换行符),因此可以用它来处理换行符)
a. actor是由30个char组成的数组
b. betsie是由100个short组成的数组
c. chuck是由13个float组成的数组
d. dipsea是由64个long double组成的数组
char actor[];
short betsie[];
float chuck[];
long double dipsea[];
array<char,30>actor;
array<short,100>betsie;
arry<float,>chuck;
array<long double,64>dipsea;
int a[]={,,,,};
int a[]={,,,,};
int even=a[]+a[];
cout<<ideas[]<<endl;
char a[]="cheeseburger";
string a="Waldorf Salad";
struct fish{
char kind[];
int weight;
float length;
}
fish Xfish=
{
"xiaoyu";
;
20.5;
}
enum Response{No,Yes,Maybe};
double *p=&ted;
cout<<*p<<endl;
float *p=treacle;
cout<<p[]<<" "<<p[]<<endl;
//or cout<<*p<<" "<<*(p+10) ;
//use new
int num;
cout<<"Please input a positive integer: ";
cin>>num;
int *shu=new int[num];
//use vector
int num;
cout<<"Please input a number: ";
cin>>num;
vector<int>shu(num);
cout<<(int*)"Home of the jolly bytes";
有效,输出该字符串的地址。
fish yu=new fish{xiaoyu,,20.5};
cout<<yu.name<<endl;
cout<<yu.weight<<endl;
cout<<yu.length<<endl;
cin.getline(adress,80);
替换为:cin>>address;
将对程序带来什么影响?
使用cin>>address 将使得程序跳过空白,直到找到给空白字符为止。然后它将读取字符,直到再次遇到空白为止。因此,它将跳过数字输入的换行符,从未避免这种问题。另一方面,它值读取一个单词,而不是整行。
#include<vector>
#include<array>
#include<string>
const int str_num=;
std::vector<std::string>vstr(str_num);
std::array<<std::string,str_num>astr;
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<iostream>
#include<string>
int main()
{
using namespace std;
cout << "What your first name? ";
string first_name;
getline(cin,first_name) ;
cout << "What your last name? ";
string last_name;
getline(cin,last_name);
cout << "What letter grade do you deserve? ";
char my_grade;
cin >> my_grade ;
cout << "What is you age? ";
int age;
cin >> age ;
cout << "Name: " << last_name << "," << first_name << endl;
cout << "Grade: " << ++my_grade<< endl;
cout << "Age: " << age << endl;
system("pause");
return ;
}
#include <iostream>
#include<string>
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 ;
}
Enter your first name: Flip
Enter your last name: Fleaming
Here's the information in a single string: Fleming, Flip
#include<iostream>
#include<cstring>
//#pragma warning(disable:4996) //vs2017需要加上这句话,忽略4996警告
const int Strlen = ;
int main()
{
using namespace std;
char first_name[Strlen];
char last_name[Strlen];
char full_name[ * Strlen];
cout << "Enter your first name: ";
cin.get(first_name,Strlen).get();
cout << "Enter your last name:";
cin.get(last_name,Strlen).get();
strcpy(full_name,last_name);
strcat(full_name,", ");
strcat(full_name, first_name);
cout << "Here's the information in a single string: " << full_name << endl;
system("pause");
return ;
}
Enter your first name: Flip
Enter your last name: Fleaming
Here's the information in a single string: Fleming, Flip
#include<iostream>
#include<string>
int main()
{
using namespace std;
string first_name;
string last_name;
string full_name;
cout << "Enter your first name: ";
getline(cin,first_name);
cout << "Enter your last name:";
getline(cin, last_name);
full_name = last_name + ", " + first_name;
cout << "Here's the information in a single string: " << full_name << endl;
system("pause");
return ;
}
#include<iostream>
#include<string>
struct CandyBar {
std::string brand;
float weight;
int calorie;
};
int main()
{
using namespace std;
CandyBar snack = {"Mocha Munch",2.3,};
cout << "brand: " << snack.brand << endl;
cout << "weight: " << snack.weight << endl;
cout << "calorie: " << snack.calorie << endl;
system("pause");
return ;
}
#include<iostream>
#include<string>
struct CandyBar {
std::string brand;
float weight;
int calorie;
};
int main()
{
using namespace std;
CandyBar snack[] = {
{ "Mocha Munch",2.3, },
{ "Mocha Munch",2.3, },
{ "Mocha Munch",2.3, }
};
for (int i = ; i < ; i++)
{
cout << "brand: " << snack[i].brand << endl;
cout << "weight: " << snack[i].weight << endl;
cout << "calorie: " << snack[i].calorie << endl;
}
system("pause");
return ;
}
#include<iostream>
#include<string>
struct Pizza {
std::string company;
double diameter;
double weight;
};
int main()
{
using namespace std;
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 ;
}
#include<iostream>
#include<string>
struct Pizza {
std::string company;
double diameter;
double weight;
};
int main()
{
using namespace std;
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 ;
}
#include<iostream>
#include<string>
struct CandyBar {
std::string brand;
double weight;
int calorie;
};
int main()
{
using namespace std;
CandyBar *snack = new CandyBar[];
for (int i = ; i < ; i++)
snack[i]={"Mocha Munch", 2.3, };
for (int i = ; i < ; i++)
{
cout << "brand: " << snack[i].brand << endl;
cout << "weight: " << snack[i].weight << endl;
cout << "calorie: " << snack[i].calorie << endl;
}
delete[] snack;
system("pause");
return ;
}
#include<iostream>
#include<array>
int main()
{
using namespace std;
array<double, 3>grade;
cout << "Please input your first grade:";
cin >> grade[];
cout << "Please input your second grade:";
cin >> grade[];
cout << "Please input your third grade:";
cin >> grade[];
cout << "Average three times:" << (grade[] + grade[] + grade[]) / 3.0 << endl;
system("pause");
return ;
}