#include<iostream>
#include<string>
using namespace std;
void n_chars(string, int);
int main()
{
int times;
string ch;
cout << "Enter a character: ";
getline(cin,ch);
while (ch != "qt")
{
cout << "Enter an integer: ";
cin >> times;
n_chars(ch, times);
cout << "\nEnter another character or press the "
"q_key to quit: ";
cin >> ch;
}
cout << "value: " << times << endl;
return 0;
}
void n_chars(string c, int n)
{
while (n-->0)
{
cout << c;
}
#include<string>
using namespace std;
void n_chars(string, int);
int main()
{
int times;
string ch;
cout << "Enter a character: ";
getline(cin,ch);
while (ch != "qt")
{
cout << "Enter an integer: ";
cin >> times;
n_chars(ch, times);
cout << "\nEnter another character or press the "
"q_key to quit: ";
cin >> ch;
}
cout << "value: " << times << endl;
return 0;
}
void n_chars(string c, int n)
{
while (n-->0)
{
cout << c;
}
}
特别注意,
以下有几点是与插入型不同的
string类型的使用是要#include<string>
string ch;
输入是用getline(cin,ch);
while判断是while(ch!=“qt")
而char型则简单多了。
#include<iostream>
#include<cstring>
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 << "value: " << times << endl;
return 0;
}
void n_chars(char c, int n)
{
while (n-->0)
{
cout << c;
}
}
一定要对比记忆,重点理解。