在循环中读取输入,直到在C+&#x2B中达到哨兵值。
我对C ++相对较新,我正在尝试编写一些要求用户输入数字并使用-999作为前哨值的代码来结束输入循环。他们输入的数字必须填充以后在二进制树中使用的数组。我遇到的问题是,如果输入-999,循环不会终止,我也不知道如何在不指定数组的大小的情况下将这些值读取到数组中。该程序必须根据给出多少输入来动态大小数组。我附上了代码。任何帮助将不胜感激!
#include <iostream>
#include <string.h>
//#include "binarySearchTree.h"
//#include "binaryTree.h"
using namespace std;
int i;
int n;
struct Node {
int data;
struct Node* left;
struct Node* right;
}
Node(int val)
{
data = val;
left = NULL;
right = NULL;
};
int main() {
//struct Node* root = new Node(1);
cout << "Enter values up to -999: \n";
int numbers[] = {};
for(int i =0; i!=999; i++){
cin >> numbers[i];
}
cout << "\nYour numbers are: ";
for(int j=0; j<=sizeof(numbers) ; j++){
cout << numbers[j] << " ";
}
return 0;
}
I'm relatively new to C++ and I'm trying to write some code that asks a user to enter numbers and use -999 as a sentinel value to end the input loop. The numbers they enter must populate an array to be used in a binary tree later. The issue I'm having is the loop doesn't terminate if -999 is entered, and I also don't know how to read those values into the array without specifying how large the array is. The program must dynamically size the array based on how many inputs were given. I have attached my code. Any help is greatly appreciated!
#include <iostream>
#include <string.h>
//#include "binarySearchTree.h"
//#include "binaryTree.h"
using namespace std;
int i;
int n;
struct Node {
int data;
struct Node* left;
struct Node* right;
}
Node(int val)
{
data = val;
left = NULL;
right = NULL;
};
int main() {
//struct Node* root = new Node(1);
cout << "Enter values up to -999: \n";
int numbers[] = {};
for(int i =0; i!=999; i++){
cin >> numbers[i];
}
cout << "\nYour numbers are: ";
for(int j=0; j<=sizeof(numbers) ; j++){
cout << numbers[j] << " ";
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个可以执行您想要的程序。
live demo
Here is a program that does what you want.
Live demo
除了所有其他问题之外,主要问题是:
您正在从标准输入中读取数组
数字
的第i-the元素。您将i
与999进行比较,这基本上没有意义。为什么要比较i
?为什么将其与999(而不是-999)进行比较?让我们尝试修复它。从一个空的无限循环开始:
读一个整数:
现在让我们检查一下我们是否设法阅读了某些内容,如果不让我们退出循环。
如果我们阅读了-999,我们还需要从循环中退出:
现在您想将其放在数字的第三位置,所以:
好的,现在我们有一个工作循环(希望如此)。您的代码中还有哪些其他问题?
您无法在没有C ++的编译时间大小的情况下定义数组。使用
std :: vector&lt;&gt;
。然后,
sizeof
运算符不做您认为做的事情。将其保存为(很多?)。使用std :: vector :: size()
。但是对于初学者来说,您可以假设1000个数字对每个人都足够(Bill Gates Docet),并将计数保留在变量i
中:Beside all other problems, the main problem is here:
You are reading from the standard input into the i-th element of the array
numbers
. You are comparingi
with 999, which basically makes no sense. Why comparingi
? And why comparing it with 999, instead of -999?Let's try to fix it. Start with an empty infinite loop:
Read an integer:
Now let's check if we managed to read something and if not let's exit from the loop.
We need to exit from the loop also if we read a -999:
Now you want to put it in the i-th position of numbers, so:
Ok, now we have a working loop (hopefully). What other problems you have in your code?
You cannot define arrays without a compile time size in C++. Use
std::vector<>
.Then, the
sizeof
operator doesn't do what you think it does. Save it for (much?) later. Usestd::vector::size()
. But for starters, you can assume that 1000 numbers will be enough for everyone (Bill Gates docet), and keep the count in variablei
:Switching to
std::vector<>
is much better. And learn Why is "using namespace std;" considered bad practice?:Finally, if you think that
while(true) {}
is ugly, you can use other versions of the same loop, e.g.: