关于C++问题,一个很简单的程序为啥总出问题

来源:百度知道 编辑:UC知道 时间:2024/09/22 01:27:41
#include<iostream>
using namespace std;
void n_chars(char,int);
int main()
{
int times;
char ch;
cout<<"Enter a character: ";
cin>>ch;
while(ch!='q')
{
cout<<"Enter an integer:";
cin>>times;
n_chars(ch,times);
cout<<"\nEnter another character or press the q key to quit: ";
cin>>ch;
}
cout<<"The value of times is "<<times<<".\n";
cout<<"Bye\n";
return 0;
}
void n_chars(char c,int n)
{
while((n--)<0)
cout<<c;
}

这个程序有意义吗??没有什么功能啊
如果就按你这上面不改的话,就是先让你输入一个字符和一个整数,只要不是q,就继续让你输入字符和整数。一旦输入q,就跳出循环,并输出最后输入的那个数字;
如果改一下你的程序,把whlie((n--)<0)改成while((n--)>0)的话,就是只要不输入q,就输出数组times和times次字符ch,如果第一次输入q,那么输出times=随机的一个垃圾数据,如果是中途输入q,那就输出最后输入的times

啥问题啊 你也不说

这里错了,改成
void n_chars(char c,int n)
{
while((n--)>0)
cout<<c;
}

#include<iostream>
using namespace std;
void n_chars(char,int);
int main()
{
int times;
char ch;
cout<<"Enter a character: ";
cin>>ch;
while(ch!='q')
{
cout<<"Enter an integer:";
cin>>times;
n_chars(ch,times);
cout<<"\nEnter another character or press the q key to quit: ";
cin>>ch;
}
cout<<"The value of times is "<<times<<".\n";
cout<<"Bye\n";
return 0;
}
<