VC++中一个字符串输入和结束问题。我的代码有严重错误,那位大哥帮我解决下。

来源:百度知道 编辑:UC知道 时间:2024/05/31 23:16:15
#include <iostream.h>
#include <stdio.h>
void main()
{
char a[50];
char c;
int i(-1);
cout<<"enter the string:";
while ((c=getchar())!=char(13))
{
a[++i]=c;}
cout<<a;
}
while ((c=getchar())!=char(13)) 最后哪个反括号,原来是EN文的,我粘贴过来掉了补上的。我觉得程序这样写有问题,不是语法上的。我觉得是逻辑上的.
#include <stdio.h>
#include <iostream.h>
void main()
{
int i=0;
char a[50];
char c;
printf("enter the string:");
//cout<<"enter the string"<<endl;
while ((c=getchar())!='\n')
{
a[i]=c;
i++;
}
cout<<a;
}
我这样改了就对了。但不知道为什么用cout<<"enter the string:";就不行,用printf就可以

while ((c=getchar())!=char(13))
这一行的最后一个括号是中文状态的,改一下即可

#include <stdio.h>
#include <iostream.h>
void main()
{
int i=0;
char a[50];
char c;
printf("enter the string:");
//cout<<"enter the string"<<endl;
while ((c=getchar())!='\n')
{
a[i]=c;
i++;
}
cout<<a; //此处有问题,数组不能这样输出。
}