求指导!!strlen测字符串长度总是9,为什么啊??

亓明昊 发布于 2011/11/02 02:02
阅读 550
收藏 0
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<iomanip.h>
#include<ctype.h>

void main(void)
{
 char a[1024];
 long double b[1024];
 int i;
 int j = 0;
 int c = 0;
 int charLength;
 int counter = 0;
 int counter1 = 0;
 int max = 0;
 
 cout<<"输入串:(Enter结束输入。)"<<endl;
 cin.getline(a,'\n');
 charLength=strlen(a);
 
 for( i = 0; i < charLength; i++)
 {
  while( isdigit( a[i] ) )
  {
   c = ( a[i] - 48 ) + (c * 10);
   i++;
   counter++;
  }
  b[j] = c;
  c = 0;
  j++;
  
  if( counter > max )
  {
   max = counter;
   counter = 0;
  }
  else
   counter = 0;
 }
 counter1 = j;
 
 cout<<"输出结果:"<<endl;
 for( j = 0; j < counter1; j++ )
 {
  cout<<setw(max)<<resetiosflags(ios::left);
  cout<<setiosflags(ios::right)<<b[j]<<endl;
 }
 cout<<endl;
}

strlen测字符串长度总是9,为什么啊? 求大神指导啊,万分感谢啊!!
加载中
0
k
kmaau

Try:
cin.getline(a, 1024);

This is because the '\n' is Line Feed, which is 0x0A or 10 in decimal. Therefore, getline will read (10-1) characters which is 9 only.

亓明昊
亓明昊
Thank you very much . I'd tried "cin.getline(a,1024,'\n')", the problem had been solved.
0
亓明昊
亓明昊

Thank you very much . I'd tried "cin.getline(a,1024,'\n')", the problem had been solved.

返回顶部
顶部