第4章復(fù)合類型-創(chuàng)新互聯(lián)

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
//猜測應(yīng)用到指針的運算,不確定答案

創(chuàng)新互聯(lián)建站長期為上1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為洮北企業(yè)提供專業(yè)的網(wǎng)站設(shè)計制作、網(wǎng)站設(shè)計,洮北網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
#includeint main()
{using namespace std;
	char firname[15], lastname[10], lettergrade, outgrade;
	unsigned int age;
	cout<< "What is your first name? ";
	cin >>firname;
	cout<< "What is your lastname? ";
	cin >>lastname;
	cout<< "What letter grade do you deserve? ";
	cin >>lettergrade;
	switch (lettergrade) {case 'A':
			outgrade = 'B';
		case'B':
			outgrade = 'C';
		case'C':
			outgrade = 'D';
		case'D':
			outgrade = 'A';
	}
	cout<< "What is your age? ";
	cin >>age;
	cout<< "Name: "<< lastname<< ", "<< firname<< endl
		<< "Grade: "<< outgrade<< endl
		<< "Age: "<< age<< endl;
	return 0;
}

2023/1/16更新如下:

#include#includeint main()
{using namespace std;
	string firname, lastname;
	char lettergrade;
	unsigned int age;
	cout<< "What is your first name? ";
	cin >>firname;
	cout<< "What is your lastname? ";
	cin >>lastname;
	cout<< "What letter grade do you deserve? ";
	cin >>lettergrade;
	cout<< "What is your age? ";
	cin >>age;
	cout<< "Name: "<< lastname<< ", "<< firname<< endl
		<< "Grade: "<< ++lettergrade<< endl
		<< "Age: "<< age<< endl;
	return 0;
}
二 cin.getline(name,ArSize);報錯(待解)

在這里插入圖片描述

//instr2.cpp -- reading more than one word with getline
#include#includeint main()
{using namespace std;
	const int ArSize = 20;
	string name, dessert;
	cout<< "Enter your name:\n";
	getline(cin,name);
	cout<< "Enter your favorite dessert:\n";
	getline(cin,dessert);
	cout<< "I hace some delious "<< dessert
		<< " for you, "<< name<< ".\n";
	return 0;
}

使用cin.getline(name,ArSize);會報錯,待解;截圖如下
在這里插入圖片描述
在這里插入圖片描述

三 cstring

在這里插入圖片描述
在這里插入圖片描述

#include#includeint main()
{using namespace std;
	char firstname[15],lastname[10];
	cout<< "Enter your first name: ";
	cin.getline(firstname, 15);
	cout<< "Enter your last name: ";
	cin.getline(lastname,15);
	cout<< "Here's the infoamation in a single: "<< lastname<< ", "<< firstname<
四 string

在這里插入圖片描述

#include#includeint main()
{using namespace std;
	string firstname, lastname;
	cout<< "Enter your first name: ";
	cin >>firstname;
	cout<< "Enter your lastname: ";
	cin >>lastname;
	cout<< "Here's the information in a single string: "<< lastname<< ", "<< firstname;

	return 0;
}
五 struct

在這里插入圖片描述

#includeusing namespace std;

struct CandyBar {string brand;
		float weight;
		int calorie;
	};

int main()
{CandyBar snack{"Mocha Munch",2.3,350 };
	cout<< snack.brand<< endl<< snack.weight<< endl<< snack.calorie;

	return 0;
}
六 struct+數(shù)組⑤

在這里插入圖片描述

#includeusing namespace std;

struct CandyBar {string brand;
		float weight;
		int calorie;
	};

int main()
{CandyBar snack[3]
	{{"Mocha Munch",2.3,350 },
		{"Rose Water",1.2,600},
		{"Apple grow",3.0,250}
	};

	cout<< snack[0].brand<< ", "<< snack[0].weight<< ", "<< snack[0].calorie<
七 struct

在這里插入圖片描述
在這里插入圖片描述

#includeusing namespace std;

struct pizza{string company;
	float length;
	float weight;
};

int main()
{pizza quest1;
	cout<< "Enter the name of pizza company: ";
	cin >>quest1.company;
	cout<< "Enter the length of pizza(cm): ";
	cin >>quest1.length;
	cout<< "Enter the weight of pizza(g): ";
	cin >>quest1.weight;
	
	cout<< "Company: "<< quest1.company<< endl
		<< "length: "<< quest1.length<<"cm"<
八 new⑦

在這里插入圖片描述

#includeusing namespace std;

struct pizza {string company;
	float length;
	float weight;
};

int main()
{pizza *quest1=new pizza;
	cout<< "Enter the length of pizza(cm): ";
	cin >>quest1->length;
	cout<< "Enter the name of pizza company: ";
	cin >>quest1->company;
	cout<< "Enter the weight of pizza(g): ";
	cin >>quest1->weight;

	cout<< "Company: "<< quest1->company<< endl
		<< "length: "<< quest1->length<< "cm"<< endl
		<< "weight: "<< quest1->weight<< "g";
	delete quest1;

	return 0;
}
九 new⑥

在這里插入圖片描述

#includeusing namespace std;

struct CandyBar {string brand;
	float weight;
	int calorie;
};

int main()
{CandyBar* snack = new CandyBar[3]
	{{"Mocha Munch",2.3,350 },
		{"Rose Water",1.2,600},
		{"Apple grow",3.0,250}
	};

	cout<< snack[0].brand<< ", "<< snack[0].weight<< ", "<< snack[0].calorie<< endl;
	cout<< snack[1].brand<< ", "<< snack[1].weight<< ", "<< snack[1].calorie<< endl;
	cout<< snack[2].brand<< ", "<< snack[2].weight<< ", "<< snack[2].calorie<< endl;
	delete [] snack;

	return 0;
}
十 array

在這里插入圖片描述

#include#include

int main() {using namespace std;
	arrayscore;
	cout<< "Enter the first score(s/100m): ";
	cin >>score[0];
	cout<< "Enter the second score(s/100m): ";
	cin >>score[1];
	cout<< "Enter the third score(s/100m): ";
	cin >>score[2];
	cout<< "Times: 3"<< endl
		<< "Average score: "<< (score[0] + score[1] + score[2]) / 3<< "s/100m";

	return 0;
}

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)站名稱:第4章復(fù)合類型-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://bm7419.com/article22/cecocc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、定制開發(fā)、域名注冊、網(wǎng)頁設(shè)計公司網(wǎng)站收錄、移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)