跟着小钱的足迹学习C++
从昨天我就一直在看这题点击打开链接,看了一下小钱的做法,然后学习了很多书上讲的很浅的知识。自己也用自己的方法做了出来
#include <iostream>
#include <string>
using namespace std;
int main()
{
char *firstname;
char *lastname;
char *grade;
int age;
string s;
firstname=new char[strlen(s.c_str())+1];
cout<<"what is your name ? ";
cin>>s;
strcpy(firstname,s.c_str());
cout<<"what is your last name ? ";
cin>>s;
lastname=new char[strlen(s.c_str())+1];
strcpy(lastname,s.c_str());
cout<<"what letter grade do you deserve ? ";
cin>>s;
grade=new char[strlen(s.c_str())];
strcpy(grade,s.c_str());
cout<<"what your age ? ";
cin>>s;
age=atoi(s.substr().c_str());
cout<<age<<endl;
//strcpy(age,s.c_str()); 如果把等级复制按照这样将会出错 error C2664:
//cout<<age<<endl; “strcpy”: 不能将参数 1 从“int”转换为“char *”
cout<<"name "<<lastname<<" , "<<firstname<<endl;
cout<<"Grade "<<char(*grade+1)<<endl;
}
虽然有点模仿,但是还有自己的学习过程。从中部分懂了new,delete,c_str(),strlen()、strcpy()
真的很有用,因为我还是新手,这些是慢慢学习C++的过程