本文共 6842 字,大约阅读时间需要 22 分钟。
What is your first name? Betty SueWhat is your last name? YeweWhat letter grade do you deserve? BWhat is your age? 22Name: Yewe, Betty SueGrade: CAge: 22
注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A、B或C,所以不必担心D和F之间的空档。
#include "iostream"using namespace std;const int nsize = 20;struct student{ char name[nsize]; char last_name[nsize]; char grade; int age;};void display(student);int main(){ cout << "what is your name? " ; student dyg; cin.getline(dyg.name, nsize); cout << "what is your last_name? " ; cin.getline(dyg.last_name, nsize); cout << "what is your grade? "; cin >> dyg.grade; cout << "what is your age? " ; cin >> dyg.age; display(dyg); return 0;}void display(student lz){ cout << "name: " << lz.name <<"," << lz.last_name << endl; cout << "grade: " << char(lz.grade + 1) << endl; cout << "age: " << lz.age << endl;}
//程序清单4.4:// instr2.cpp -- reading more than one word with getline#includeint main(){ using namespace std; const int ArSize = 20; char name[ArSize]; char dessert[ArSize]; cout << "Enter your name:\n"; cin.getline(name, ArSize); // reads through newline cout << "Enter your favorite dessert:\n"; cin.getline(dessert, ArSize); cout << "I have some delicious " << dessert; cout << " for you, " << name << ".\n"; return 0; }
#include "iostream"#include "string"using namespace std;int main(){ string name, dessert; cout << "enter your name: "<< endl; getline(cin, name); cout << "enter your favorite dessert: "<< endl; getline(cin, dessert); cout << "i have some delicious " << dessert; cout << " for you,"<<
Enter your first name: FlipEnter your last name: FlemingHere’s the information in a single string: Fleming, Flip
#include "iostream"#include "cstring"using namespace std;const int nsize = 20;int main(){ char fname[nsize]; char lname[nsize]; char name[2*nsize+1]; cout << "enter your first name: "; cin.getline(fname, nsize); cout << "enter your last name: "; cin.getline(lname, nsize); strncpy(name, fname, nsize); strcat(name, ","); strncat(name,lname,nsize); name[2*nsize] = '\0'; cout << "here's the information in a single string: " << name << endl; return 0;}
Enter your first name: FlipEnter your last name: FlemingHere’s the information in a single string: Fleming, Flip
#include "iostream"#include "string"using namespace std;int main(){ string fname, lname, add, fullname; cout << "enter your first name: "; getline(cin, fname); cout << "enter your last name: "; getline(cin, lname); add = ","; fullname = fname + add + lname; cout << "here's the information in a single string: " << fullname << endl; return 0;}
#include "iostream"using namespace std;const int asize = 20;struct candybar { char brand[asize]; double weight; int calory;};int main(){ candybar snack = { "Mocha Munch", 2.3, 350}; cout << "here's the information of snack:" << endl; cout << "brand: " << snack.brand << endl; cout << "weight: " << snack.weight << endl; cout << "calory: " << snack.calory ; return 0;}
#include "iostream"using namespace std;const int asize = 20;struct candybar { char brand[asize]; double weight; int calory;};int main(){ candybar snack[3] = { { "A", 1.2, 3}, { "B", 4.5, 6}, { "C", 7.8, 9} }; for (int i = 0; i < 3;i++) { cout <<" brand:"<< snack[i].brand << endl <<" weight:"<< snack[i].weight << endl <<" calory:"<< snack[i].calory << endl<
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。
#include "iostream"#include "string"using namespace std;const int asize = 20;struct pizza{ char company[asize]; double diameter; double weight;};int main(){ pizza pie; cout << "what's the name of the pizza company?"; cin.getline(pie.company, asize); cout << "what's the diameter of the pizza?"; cin >> pie.diameter; cout << "what's the weight of the pizza?"; cin >> pie.weight; cout << "company: " << pie.company << endl; cout << "diameter: " << pie.diameter << endl; cout << "weight: " << pie.weight << endl; return 0;}
#include "iostream"#include "string"using namespace std;const int asize = 20;struct pizza{ char company[asize]; double diameter; double weight;};int main(){ pizza *pie = new pizza; cout << "what's the diameter of pizza? "; cin >> pie->diameter; cin.get(); cout << "what's the name of the pizza company? "; cin.get(pie->company, asize); cout << "what's the weight of the pizza ? "; cin >> pie->weight; cout << "company: " << pie->company << endl; cout << "diameter(inches): " << pie->diameter << endl; cout << "weight(ounce): " << pie->weight << endl; delete pie; return 0;}
#include "iostream"#include "string"using namespace std;struct candybar{ string brand; double weight; int calory;};int main(){ candybar *snack = new candybar[3]; snack[0].brand = "A"; snack[0].weight = 1.1; snack[0].calory = 600; snack[1].brand = "C"; snack[1].weight = 6.9; snack[1].calory = 900; snack[2].brand = "X"; snack[2].weight = 12.6; snack[2].calory = 1900; for (int i = 0; i < 3;i++) { cout << "brand: " << snack[i].brand << endl << "weight: " << snack[i].weight << endl << "calory: " << snack[i].calory << endl << endl; } delete[] snack; return 0;}
#include "iostream"#include "array"using namespace std;int main(){ arraygrade = { 0}; cout << "输入三次40米跑成绩: " << endl; cin >> grade[0] >> grade[1] >> grade[2] ; cout << "成绩1: " << grade[0] << endl; cout << "成绩2: " << grade[1] << endl; cout << "成绩3: " << grade[2] << endl; grade[3] = (grade[0] + grade[1] + grade[2]) / 3; cout << "平均成绩:"< << endl; return 0;}
转载地址:http://pfcki.baihongyu.com/