编程很新,我找不到任何在线的基本解释或能够满足我需要的代码。我有一个相当长的程序(大约300行)它都有效。这是给出一个想法的结构:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
//code....
{
//code... etc...
}
}
我想让用户重复该程序。如果输入y,则重复int main直到再次询问相同的重复问题。否则cout&lt;&lt; “谢谢你,再见”;
答案 0 :(得分:2)
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
//using namespace std; <--- Don't use using namespace std, it pollutes the namespace
void repeat()
{
//... code to repeat
}
int main()
{
//code....
char answer;
while((std::cin >> answer) != 'y')
{
repeat();
}
}
答案 1 :(得分:1)
以下是一个简单解决方案的示例:
int main()
{
for (;;) // "infinite" loop (while (true) is also possible)
{
// stuff to be repeated here
cout << "Repeat? [y/n]" << endl;
char answer;
cin >> answer;
if (answer == 'n')
break; // exit loop
} // else repeat
cout << "Thank you, goodbye" << endl;
}
这是另一个:
int main()
{
bool repeat = true;
while (repeat)
{
// stuff to be repeated here
cout << "Repeat? [y/n]" << endl;
char answer;
cin >> answer;
repeat = answer == 'y';
}
cout << "Thank you, goodbye" << endl;
}
作为旁注,不要这样做:#include <stdlib.h>
。在C ++中,您应该在使用C标头时使用c
前缀头文件名:#include <cstdlib>
和#include <ctime>
。
答案 2 :(得分:0)
#include <iostream>
#include <conio.h>
using namespace std;
//Class
class DollarToRs {
public:
int Dollar;
int Rs;
int ToRs;
ConversionToRs() {
cout << "Enter the amount of Dollar: ";
cin >> Dollar;
ToRs = Dollar * 154;
cout << "This is the total amount in PKR: " << ToRs <<endl;
}
};
int main()
{
//Dollar Convertion Function
DollarToRs convert;
convert.ConversionToRs();
//For Repeating Program
int repeat;
int exit;
cout << "To repeat program enter 1" <<endl;
cin >> repeat;
while (repeat == 1) {
convert.ConversionToRs();
cout << "To repeat program enter 1" <<endl;
cin >> repeat;
}
exit =0;
if (exit == 0) {
}
getch();
return 0;
}