如何在每次循环迭代期间清除字符串中的值?

  • 本文关键字:字符串 清除 迭代 循环 c++
  • 更新时间 :
  • 英文 :


我想在我的原始数字上附加一个不同的值b,但是在循环的一次迭代之后(每次cout之后),将原始num值和decVal值刷新为空字符串和0。我该怎么做呢?例如,不是返回001 = 1,011 = 3,而是得到001 = 1,001011 = 4。

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void main()
{
int n, decVal;
int i = 7;
string num, b, originalnum; 
cin >> n;
for (x = 0; x < n; ++i)
{
cin >> num;
cin.get(b);
cin.ignore();
originalnum += b;
decVal += atoi(&b) * pow(2, i);
i -= 1;
cout << "The decimal equivalent of " << originalnum << " = " << decVal << endl;
}
}

如果我明白你在问什么,你想要能够重复输入的位数和输出的字符串持有的位和十进制值的表示。您希望在decVal的不同迭代之间重置所有变量。为了使这成为可能,除了修复一些错误外,您只需要将当前正在做的事情包装在另一个循环中,以便对于外部循环的每次迭代,您处理所有您喜欢的位并提出最终的decVal以输出。

在查看解决方案之前,正如上面评论中提到的,void main()仅适用于独立环境(例如,没有操作系统,如微控制器等)。否则,允许的main声明为int main (void)int main (int argc, char *argv[])(您将看到它们用等价的char **argv写成)。参见main()在C和c++中应该返回什么?

虽然方便,但您还需要阅读为什么"使用命名空间std;"被认为是不好的实践?现在你已经理解了'n'std::endl的区别了,我们来看看c++: " std::endl " vs " n "

对于每个输入,您必须始终做的一件事是,在尝试输入之后和使用由输入填充的变量之前,通过检查流状态来验证它是否成功。如果发生匹配失败,failbit将被设置,您必须在尝试下一次输入之前清除错误位。您可以以任何喜欢的方式处理错误,结束程序,或者清除错误位并删除导致.ignore()问题的字符(下面显示了这两种方法的示例)。从本质上讲,您至少需要这样的内容:

if (!(std::cin >> n)) {             /* validate EVERY user input */
std::cerr << "invalid unsigned input -- exiting.n";
return 1;
}

(注意:您不应该只调用.ignore(),而应该使用它的完整和正确的调用,为任何特定情况删除最多数量的值。当从文件-蒸汽中读取时,即:std::cin.ignore (std::numeric_limits<std::streamsize>::max(), 'n');)

现在对于你的程序设置,你基本上可以在代码周围使用一个连续的循环,类似于你所拥有的,并提供一个机制,当用户完成输入值时退出外部循环。例如,使用一个简单的while(1)循环,并让用户输入n的有效数字以外的任何内容,您可以这样做:

while (1) {                             /* outer loop - loop continually */
size_t n;                           /* declare variables within loop */
uint32_t decVal = 0;
std::string originalnum {}; 

std::cout << "nenter no. of bits ('q' to quit): ";
if (!(std::cin >> n)) {             /* validate EVERY user input */
std::cerr << "invalid unsigned input -- exiting.n";
return 1;
}

注意:所有的变量都是在循环中声明的,所以对于每次迭代,变量都是声明和初始化的。上面用户被告知按下'q'来退出——但是可以输入任何不是有效数字来退出。这将简单地退出程序,并将控制权返回给调用进程。

看看你的代码,真的不需要pow()函数从用户输入的01中计算decVal,相反,你可以简单地将decVal声明为unsigned值(下面使用uint32_t的精确值),并简单地使用Left-Shift按当前循环计数器的位数移动每个输入。

使用上面描述的左移来计算decVal(确保decVal在声明时初始化为零),并将用户输入添加到字符串的前面以保持LSB一阶,您可以这样做:

#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
#include <climits>
int main (void)
{
while (1) {                             /* outer loop - loop continually */
size_t n;                           /* declare variables within loop */
uint32_t decVal = 0;
std::string originalnum {}; 

std::cout << "nenter no. of bits ('q' to quit): ";
if (!(std::cin >> n)) {             /* validate EVERY user input */
std::cerr << "invalid unsigned input -- exiting.n";
return 1;
}
if (n > sizeof (uint32_t) * CHAR_BIT) { /* validate EVERY value in range */
std::cerr << "  error: n exceeds no. of bits avaialble.n";
return 1;
}

for (size_t i = 0; i < n;) {        /* inner loop, process bits */
uint32_t bit;
std::cout << " bit" << std::setw(2) << i << ": ";
if (!(std::cin >> bit) || bit > 1) {
std::cerr << "  error: invalid bit input.n";
std::cin.clear();
std::cin.ignore (std::numeric_limits<std::streamsize>::max(), 'n');
continue;
}
decVal += bit << i;                     /* no need for pow(), left-shift */
originalnum.insert(0, 1, bit + '0');    /* build originalnum each iteration */
i++;                                    /* increemnt on good input */
}
/* result */
std:: cout << "The decimal equivalent of " << originalnum << 
" = " << decVal << 'n';
}
}

(注意:数字限制——cppreference.com显示了使用的每个限制所需的头文件)

使用/输出示例

下面的输入和输出与您所显示的类似,但添加了提示以指导用户,并实现了错误处理以确保仅将有效的01作为输入:

$ ./bin/decval
enter no. of bits ('q' to quit): 3
bit 0: 1
bit 1: 1
bit 2: 0
The decimal equivalent of 011 = 3
enter no. of bits ('q' to quit): 3
bit 0: 1
bit 1: 0
bit 2: 0
The decimal equivalent of 001 = 1
enter no. of bits ('q' to quit): 3
bit 0: 1
bit 1: 0
bit 2: pickles
error: invalid bit input.
bit 2: 1
The decimal equivalent of 101 = 5
enter no. of bits ('q' to quit): q
invalid unsigned input -- exiting.

看一遍,如果你还有问题,或者我是否误解了你的意图,请告诉我。

直接清除

后面的字符串
cout << ... << endl;

试试这个

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void main()
{
int n, decVal;
int i = 7;
string num, b, originalnum; 
cin >> n;
for (x = 0; x < n; ++i)
{
cin >> num;
cin.get(b);
cin.ignore();
originalnum += b
decVal += atoi(&b) * pow(2, i);
i -= 1
cout << "The decimal equivalent of " << originalnum << " = " << decVal << endl;
originalnum = "";
n = 0;
}
}

最新更新