Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C+中分隔数字+; 我正在从我的C++书籍中做练习,我不知道如何修复它。我应该从用户那里得到一个int,并按照输入的顺序显示各个数字。例如,12345将显示1 2 3 4 5。7365将显示7 3 6 5。我已经编写了大部分代码,但是有一个逻辑错误,我无法理解。这是我的密码: int main() { int number = 0; int digit = 0; int temp = 0; int counter = 0; int sum = 0; int divisor = 0; cout << "Please enter a nonzero number."; cin >> number; cout << "\nThe number you entered was " << number; // Determine the number of digits temp = number; while (temp != 0) { temp = temp / 10; counter++; } cout << "\nThere are " << counter << " digits in your number."; // Separate the digits temp = number; cout << "\nSeparating the digits\n"; do { divisor = (pow(10.0, --counter)); digit = temp / divisor; temp = temp % divisor; cout << digit << " "; sum = sum + digit; } while (counter != 0); cout << "\nThe sum of the number is " << sum; return 0; } intmain() { 整数=0; 整数位数=0; 内部温度=0; int计数器=0; 整数和=0; 整数除数=0; 数量; cout_C++ - Fatal编程技术网

在C+中分隔数字+; 我正在从我的C++书籍中做练习,我不知道如何修复它。我应该从用户那里得到一个int,并按照输入的顺序显示各个数字。例如,12345将显示1 2 3 4 5。7365将显示7 3 6 5。我已经编写了大部分代码,但是有一个逻辑错误,我无法理解。这是我的密码: int main() { int number = 0; int digit = 0; int temp = 0; int counter = 0; int sum = 0; int divisor = 0; cout << "Please enter a nonzero number."; cin >> number; cout << "\nThe number you entered was " << number; // Determine the number of digits temp = number; while (temp != 0) { temp = temp / 10; counter++; } cout << "\nThere are " << counter << " digits in your number."; // Separate the digits temp = number; cout << "\nSeparating the digits\n"; do { divisor = (pow(10.0, --counter)); digit = temp / divisor; temp = temp % divisor; cout << digit << " "; sum = sum + digit; } while (counter != 0); cout << "\nThe sum of the number is " << sum; return 0; } intmain() { 整数=0; 整数位数=0; 内部温度=0; int计数器=0; 整数和=0; 整数除数=0; 数量; cout

在C+中分隔数字+; 我正在从我的C++书籍中做练习,我不知道如何修复它。我应该从用户那里得到一个int,并按照输入的顺序显示各个数字。例如,12345将显示1 2 3 4 5。7365将显示7 3 6 5。我已经编写了大部分代码,但是有一个逻辑错误,我无法理解。这是我的密码: int main() { int number = 0; int digit = 0; int temp = 0; int counter = 0; int sum = 0; int divisor = 0; cout << "Please enter a nonzero number."; cin >> number; cout << "\nThe number you entered was " << number; // Determine the number of digits temp = number; while (temp != 0) { temp = temp / 10; counter++; } cout << "\nThere are " << counter << " digits in your number."; // Separate the digits temp = number; cout << "\nSeparating the digits\n"; do { divisor = (pow(10.0, --counter)); digit = temp / divisor; temp = temp % divisor; cout << digit << " "; sum = sum + digit; } while (counter != 0); cout << "\nThe sum of the number is " << sum; return 0; } intmain() { 整数=0; 整数位数=0; 内部温度=0; int计数器=0; 整数和=0; 整数除数=0; 数量; cout,c++,C++,这里有一个版本: // If the number is only one digit, print it. // Otherwise, print all digits except the last, then print the last. void digits(int x) { if (x < 10){ cout << x; } else{ digits(x / 10); cout << " "

这里有一个版本:

// If the number is only one digit, print it.  
// Otherwise, print all digits except the last, then print the last.

void digits(int x)
{
   if (x < 10){
      cout << x;
   }
   else{
      digits(x / 10);
      cout << " " << x % 10;
   }  
}
//如果数字只有一位,请打印它。
//否则,请打印除最后一位以外的所有数字,然后打印最后一位。
无效数字(整数x)
{
if(x<10){

cout谢谢大家的帮助:-)我的代码在另一个编译器中运行良好,所以我想这只是netbeans的一个小故障。

旁注:我不建议使用
pow
来计算
10
的幂次。太复杂了:转换为字符串,插入分隔符,打印。更简单。@Deduplicator:转换为字符串?为什么不是读字符串吗?@KerrekSB只是为了好玩?也许他以后会有另一个数字来源?当然,如果他只是从字符串转换过来,最好将两者都折叠。(另外,读字符串或数字会读取不同的输入。)我已经尝试过手动调试,看起来一切都正常。书中的练习说使用整数,所以我不能使用字符串。别自欺欺人了。编译器出现问题的可能性接近于0。Netbeans使用的编译器是g++,许多公司和个人在行业中都在商业上使用它s、 如果编译器在一个简单的程序上真的有错误,那么成千上万的程序员就会报告它。