对于C++中的每个循环,如何增加变量1?

对于C++中的每个循环,如何增加变量1?,c++,C++,我已经做了一个基本的do-while循环,我想在屏幕的左侧和右侧输出carhire和voucheno。如果用户希望重复该过程,则凭证编号应增加1。另一件事是我的while表达式有什么问题, 它表示在=标记之前需要一个表达式 使用for循环或在循环外声明变量。请注意,for循环中的条件实际上可以是任何条件,它不需要查看其他两个表达式使用的相同变量 char processanother = 'y'; for (unsigned short voucherno=0; processanot

我已经做了一个基本的do-while循环,我想在屏幕的左侧和右侧输出carhire和voucheno。如果用户希望重复该过程,则凭证编号应增加1。另一件事是我的while表达式有什么问题, 它表示在=标记之前需要一个表达式

使用for循环或在循环外声明变量。请注意,for循环中的条件实际上可以是任何条件,它不需要查看其他两个表达式使用的相同变量

char processanother = 'y';
for (unsigned short voucherno=0;
     processanother=='y' || processanother =='Y';
     ++voucherno) {
  std::cout << ...
  std::cin >> processanother;
}
按照编写代码的方式,每次迭代都会创建一个新变量voucheno


而@qwr所说的:操作员是!=,不是。但是我相信你还是想要==无论如何。

如果你在do while循环中定义voucheno,那么voucheno是一个局部变量。每个循环都定义为0。所以你不会得到实际的计数。因此,在do-while循环之前定义voucheno

在C++中,如果你想,如果两个变量相等,则使用= =运算符。如果要测试它们是否不同,请使用!=运算符而不是!=.!==这是违法的

unsigned short voucherno=0;
do {

    char processanother;
    cout<<"CAR HIRE"<<setw(4)<<setfill('0')<<"Voucher Number:"<<voucherno++;
    cout<<"Repeat again to test the loop Y/N?";
    cin>>processanother;
}
while(processanother=='y'||process=='Y');
您的代码中有两个错误。 1.voucherno变量是在边循环中声明的,所以它不会显示递增的值,每次在循环中它都会被声明并由零赋值,所以它会显示为零 2.在条件中的另一个错误,C++中没有任何操作符!如你所用。如果要检查相等,则使用==;如果要检查不相等,则使用!=


谢谢

让我们在外面再做一次改变!==去=@你是说Voucheno global
unsigned short voucherno=0;
do {

    char processanother;
    cout<<"CAR HIRE"<<setw(4)<<setfill('0')<<"Voucher Number:"<<voucherno++;
    cout<<"Repeat again to test the loop Y/N?";
    cin>>processanother;
}
while(processanother=='y'||process=='Y');