zoukankan      html  css  js  c++  java
  • C++primer plus第六版课后编程题答案8.3

    8.3

    #include <iostream>
    #include <cctype>
    #include<string>
    using namespace std;
    const int MAX=50;
    void change(string &s);
    void main83()
    {
    	string s="chenggSHIguang";
    	//char ch='';
    	change(s);
    	while(s!="q"&&(s.length()!=1))
    	{
    		cout<<"Next string (q to quit):"<<endl;
    		getline(cin,s);
    		change(s);
    	}
    	cout<<"Bye"<<endl;
    	//cin.get();
    	system("pause");
    
    }
    void change(string &s)
    {
    	int size=s.length();
    
    	cout<<s<<"
    size="<<size<<endl;
    	//char ch;
    	for(int i=0;i<size;i++)
    	{
    		//cout<<"i="<<i<<"
    si="<<s[i]<<endl;
    		s[i]=toupper(s[i]);
    	}
    	cout<<s<<endl;
    
    }

    我在DEV c++里面却不会出问题,实在是不知道为什么这么神奇

    下面是我在DEV c里面测试的源代码

    #include<iostream>
    #include <string>//出问题时,使用的是cstring,但换成string一样出问题
    using namespace std;
    struct stringy{
    	char *str;
    	int ct;
    };
    void set(stringy &beany,const char t[]);
    void show(const stringy &beany,int count=0);
    int main()
    {
    	stringy beany;
    	char testing[]="Reality isn't what it used to be.";
    	//char testing[]="Reality";//what it used to be.";
    	set(beany,testing);
    	show(beany);
    	show(beany,2);
    	
    	if(beany.str!=NULL)	//总会引起中断,说是堆损坏
    	{
    		delete []beany.str;
    		cout<<"no null"<<endl;
    	}
    	else 
    		cout<<"str is null";
    
    	system("pause");
    	
    
    
    }
    
    void set(stringy &beany,const char t[])
    {
    	//delete beany.str;
    	int size=sizeof(t);//这里出了问题,长度测试错误,当长度超过八个字符是,直接出错!
    	cout<<"size="<<size<<endl;
    	beany.str=new char[size+1];
    	strcpy(beany.str,t);
    	int sizeofStr=sizeof(beany.str);
    	cout<<"Sizeofstr="<<sizeofStr<<endl;
    	beany.ct=size;
    }
    void show(const stringy &beany,int count)
    {
    	cout<<"Stringy show"<<endl;
    	if(count!=0)
    	{
    		for(int j=0;j<count;j++)
    		{
    			//cout<<"in stringy here!"<<j<<endl;
    			//cout<<"Stringy show"<<j<<endl;
    			cout<<"str="<<beany.str<<endl;
    		};
    	}
    	else
    		cout<<"str="<<beany.str<<endl;
    	
    }
    




  • 相关阅读:
    MySQL回顾
    mysql多表查询
    通过JDBC进行简单的增删改查(以MySQL为例)
    如何正确学习JavaScript
    List集合遍历时修改元素出现并发修改异常总结
    国内有哪些质量高的JAVA社区?
    【题解】【链表】【Leetcode】Add Two Numbers
    【题解】【字符串】【Leetcode】Valid Palindrome
    【题解】【DP】【Leetcode】Climbing Stairs
    【题解】【数组】【Leetcode】Merge Sorted Array
  • 原文地址:https://www.cnblogs.com/qq84435/p/3664873.html
Copyright © 2011-2022 走看看