c++ - 第一次迭代失败

标签 c++ arrays loops

我是 C++ 的新手,但从逻辑上讲,我认为这应该可行。

我有一个函数,它接受用户的输入来设置数组的内存分配:

void setarraynum(){
  string mystr;
  cout<<"Please enter the size of your array: ";
  getline(cin, mystr);
  stringstream(mystr)>>arraynum;
  array = new int [arraynum];
  cout<<"\n";
}

arraynum 和 array 是全局设置为:

int arraynum;
int * array;

然后我有一个接受数组输入的函数:

void setarray(){
  string mystr;
  cout<<"Please enter "<<arraynum<<" numbers:\n";
  for(int n=0; n<arraynum; n++){
    getline(cin, mystr);
    stringstream(mystr)>>array[n];
  };
cout<<"\n";
}

问题出现在输入数组编号时,数组的第一个实例 (array[0]) 自动设置为 0。就好像循环在不要求用户输入的情况下进行第一次迭代一样。然后它继续像往常一样询问用户。

有什么想法吗?

谢谢。

*编辑(根据要求,完整的代码):

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int arraynum;
int * array;
bool isin = false;
int input;
char order;

void insertionsortdesc(int a[]);
void insertionsortasc(int a[]);
void getarray(int a[]);
void isinarray(int a[], int b);

void setsearch();
void setarray();
void setarraynum();
void setorder();

int main(){

 cout<<"\n//// INPUT ////\n\n";

 setarraynum();
 setorder();
 setarray();
 setsearch();

 if(order=='a'){insertionsortasc(array);
 }else if(order=='d'){insertionsortdesc(array);};

 cout<<"//// OUTPUT ////\n\n";
 getarray(array);
 isinarray(array, input);

 return 0;
}

void setorder(){
 bool isvalid = false;

 while(!isvalid){
  cout<<"Ascending or Descending [a/d]: ";
  cin>>order;
  if(order=='a'||order=='d'){isvalid = true;
  }else{cout<<"Please enter a valid option!\n";};
 };

cout<<"\n";
}

void setarraynum(){
 string mystr;
 cout<<"Please enter the size of your array: ";
 getline(cin, mystr);
 stringstream(mystr)>>arraynum;
 array = new int [arraynum];
 cout<<"\n";
}

void setsearch(){
 string mystr;
 cout<<"Search for (int): ";
 getline(cin, mystr);
 stringstream(mystr)>>input;
 cout<<"\n";
}

void setarray(){
 string mystr;
 cout<<"Please enter "<<arraynum<<" numbers:\n";
 for(int n=0; n<arraynum; n++){
  getline(cin, mystr);
  stringstream(mystr)>>array[n];
 };
cout<<"\n";
}

void insertionsortdesc(int a[]){
 for(int n=1; n<arraynum; n++){
  int key = a[n];
  int j = n-1;
  while((j>=0)&&(a[j]<key)){
   a[j+1] = a[j];
   j -= 1;
  };
  a[j+1]=key;
 }
}

void insertionsortasc(int a[]){
 for(int n=1; n<arraynum; n++){
  int key = a[n];
  int j = n-1;
  while((j>=0)&&(a[j]>key)){
   a[j+1] = a[j];
   j -= 1;
  };
  a[j+1]=key;
 }
}

void isinarray(int a[], int b){
 for(int n=0; n<arraynum; n++){
  if(a[n] == b){
   isin = true;
   break;
  };
 };

 if(isin){
  cout<<b<<" is present in the given array.";
 }else{
  cout<<b<<" is not present in the given array.";
 };

 cout<<"\n\n";
}

void getarray(int a[]){
 cout<<"Sorted array sequence: ";
 for(int n=0; n<arraynum; n++){
  cout<<a[n]<<", ";
 };
 cout<<"\n\n";
}

最佳答案

我强烈建议您直接使用 cin >> arraynum; 而不是在 stringstream(mystr) >> arraynum; 范例中读取整数。

来自 C++ 引用 http://www.cplusplus.com/reference/string/string/getline/对于 getline

If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it.

这就是问题所在。 正如@Vaughn 所指出的,您在 setorder 函数中使用 cin >> order; ,这将在输入缓冲区中留下尾部 \n .

您的代码吸取的教训是,在处理 I/O 问题时,请确保在同一范例中调用函数。

关于c++ - 第一次迭代失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18479724/

相关文章:

c++ - strcpy() 没有正确复制 c++

php - 使用 for 循环将数组插入表中

javascript - 映射迭代器未定义 React.JS

java - 如何在Netbeans的变量窗口中跳转到特定的调试点

C - 数字中数字的位置

C++ 循环 - 流错误?

c++ - Code::Blocks 中的 MinGW 未链接静态 OpenSSL 库

c++ - 强制对象解除分配/超出范围以调用析构函数

c - 将 +/- 字母等级定义为常量。 C

r - 如何在 R 中构建一次汇总多个值的表