1、
#include
int main(int argc, char* argv[])
{
char firstName[50] = {0};
char lastName[50] = {0};
char grade;
int age;
std::cout << "What is your first name? ";
std::cin.getline(firstName, 49);
std::cout << "What is your last name? ";
std::cin.getline(lastName, 49);
std::cout << "What letter grade do you deserve? ";
std::cin >> grade;
std::cout << "What is your age? ";
std::cin >> age;
std::cout << "Name: "<< lastName << ", "<< firstName < return 0; } 2、 //instr2.cpp --- reading more than one word with getline int main() 3、 #ifndef _AFXDLL #include int main() CString firstName(first); return 0; 4、 #include int main() std::cout << "Here's the information in a single string: " << firstName << ", " << latstName << std::endl; return 0; 5、 #include struct CandyBar{ int main() return 0; 6、 #include struct CandyBar{ int main() 7、 #include struct Pizza{ int main() std::cout << "Here's the information of pizza: " << std::endl << "company is : " << pizza.company << std::endl 8、 #include struct Pizza{ int main() std::cout << "Here's the information of pizza: " << std::endl << "company is : " << pizza->company << std::endl 9、感觉前3个for循环输入是大量的重复代码,想将其做成两层循环或者写成接口,但是貌似除了函数模板又没有更简单易行的方法,暂且搁置吧,等二刷再优化。 10、
#include
#include
{
std::string name;
std::string dessert;
std::cout << "Enter your name:"<< std::endl;
getline(std::cin, name);//reads through newline
std::cout << "Enter your favorite dessert:";
getline(std::cin, dessert);
std::cout << "I have some delicious " << dessert;
std::cout << " for you, " << name << std::endl;
return 0;
}
#define _AFXDLL
#endif
#include
#include
{
char first[100] = {0};
char last[100] = {0};
std::cout << "Enter your first name: ";
std::cin.getline(first, 100);
std::cout << "Enter your last name: ";
std::cin.getline(last, 100);
CString lastName(last);
CString name(firstName + ", " + lastName);
std::cout << "Here's the information in a single string: ";
std::wcout << (LPCTSTR)name << std::endl;
}
#include
{
std::string firstName;
std::string latstName;
std::cout << "Enter your first name: ";
getline(std::cin, firstName);
std::cout << "Enter your last name: ";
getline(std::cin, latstName);
}
#include
std::string brand;
double weight;
int calorie;
};
{
CandyBar snack = { "Mocha Munch", 2.3, 350 };
std::cout << "Here's the information in snack: " << std::endl << "brand is : " << snack.brand << std::endl
<< "weight : " << snack.weight << std::endl << "calorie : "<< snack.calorie << std::endl;
}
#include
std::string brand;
double weight;
int calorie;
};
{
CandyBar snack[3] = { {"Mocha Munch", 2.3, 350}, {"www", 3.7, 345}, {"vdf", 0.3, 250}};
for(int i =0 ; i < 3; i++)
{
std::cout << "Here's the information in snack[ " << i << "] : "<< std::endl << "brand is : " << snack[i].brand << std::endl
<< "weight : " << snack[i].weight << std::endl << "calorie : "<< snack[i].calorie << std::endl;
}
return 0;
}
#include
std::string company;
double diameter;
double weight;
};
{
Pizza pizza;
std::cout << "Please enter compay of this pizza: ";
getline(std::cin, pizza.company);
std::cout << "Please enter diameter of this pizza: ";
std::cin >> pizza.diameter;
std::cout << "Please enter weight of this pizza: ";
std::cin >> pizza.weight;
<< "diameter : " << pizza.diameter << std::endl << "weight : "<< pizza.weight << std::endl;
return 0;
}
#include
std::string company;
double diameter;
double weight;
};
{
Pizza *pizza = new Pizza;
std::cout << "Please enter diameter of this pizza: ";
std::cin >> pizza->diameter;
std::cin.get(); //如果不加std::cin.get()将无法输入公司名
std::cout << "Please enter compay of this pizza: ";
getline(std::cin, pizza->company);
std::cout << "Please enter weight of this pizza: ";
std::cin >> pizza->weight;
<< "diameter : " << pizza->diameter << std::endl << "weight : "<< pizza->weight << std::endl;
delete pizza;
pizza = NULL;
return 0;
} 1 #include
29 for(int i =0 ; i < 3; i++)
30 {
31 std::cout << "Here's the information in pCandyBar[ " << i << "] : "<< std::endl << "brand is : " << pCandyBar[i].brand << std::endl
32 << "weight : " << pCandyBar[i].weight << std::endl << "calorie : "<< pCandyBar[i].calorie << std::endl;
33 }
34 delete [] pCandyBar;
35 return 0;
36 } 1 #include