| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 459 人关注过本帖, 1 人收藏
标题:20位的数组,为什么输出是27(31)呢?
只看楼主 加入收藏
lxfeng1988
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2013-8-4
结帖率:100%
收藏(1)
已结贴  问题点数:16 回复次数:3 
20位的数组,为什么输出是27(31)呢?
程序清单:
//strtype4.cpp--line input
#include<iostream>
#include<string>  //make string class available
#include<cstring>  //C-style string library
int main()
{
    using namespace std;
    char charr[20];
    string str;
    cout<<"Length of string in charr before input: "
        <<strlen(charr)<<endl;
    cout<<"Length of string in str before input: "
        <<str.size()<<endl;
    cout<<"Enter a line of text:\n";
    cin.getline(charr,20);  //indicate maximum length
    cout<<"You entered: "<<charr<<endl;
    cout<<"Enter another line of text:\n";
    getline(cin,str);  //cin now an argument;no length specifier
    cout<<"Length of string in charr after input: "
        <<strlen(charr)<<endl;
    cout<<"Length of string in str after input: "
        <<str.size()<<endl;
    return 0;
}
请问输出结果“Length of string in charr before input”是什么(我觉得应该是20)呢?
书上给出的结果是27
而我敲进编译器给出的结果是31
求大神指导!!!
搜索更多相关主题的帖子: available indicate include before Enter 
2014-01-01 11:53
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:16 
cout<<"Length of string in charr before input: "
        <<strlen(charr)<<endl;  
你的charr 是没有初始化的,计算strlen 当然是错误的了

书上应该不是这样写吧

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2014-01-02 08:14
lxfeng1988
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2013-8-4
收藏
得分:0 
回复 2楼 yuccn
明白了,谢谢。
2014-01-03 08:57
华子hear
Rank: 3Rank: 3
来 自:陕西西安
等 级:论坛游侠
帖 子:57
专家分:104
注 册:2013-6-3
收藏
得分:0 
Length of string in charr before input: 3
Length of string in str before input: 0
Enter a line of text:




为什么我的是3呢  初始化要怎么弄 一旦初始化不是就成0了嘛?
2014-01-04 12:46
快速回复:20位的数组,为什么输出是27(31)呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018527 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved