c++简单代码中的字符串数组

String array in c++ simple code

本文关键字:字符串 数组 简单 代码 c++      更新时间:2023-10-16

这是我用c++编写的带有do-while循环的完整程序,它第一次运行正常,但当它循环时就不能正常运行。

这个程序将名称存储到一个数组中并打印出来。数组大小是50,所以我想在数组中存储50个名称。

任何帮助都将不胜感激。谢谢

#include <iostream>
#include <cmath>
#include <string>
using namespace std;
void start(int c);
string NameArray [50];
char response;
int main() {
  int count=1;  
  do {    
    count = count - 1;
    start(count);
    cout << "do you want to add another name? ";
    cin>> response;     
    count = count + 2;      
    cout<< endl;        
  } while (tolower(response)=='y');
  cout<< "program Ends" <<endl;        
  system ("pause");        
  return 0;     
}    
void start(int count) {         
  cout<< "Enter your First and Last name: ";        
  getline(cin, NameArray[count]);       
  cout<< NameArray[count] <<endl;       
  cout<< endl;          
}

cin>> response将检索输入的一个字符,并将换行符留在输入流中。这将导致您的start()函数在提示输入您的姓名后立即返回,而getline()将向NameArray返回一行空的输入。

您也可以使用getline()以字符串形式检索响应,然后检查行的第一个char

    //...
    cout << "do you want to add another name? ";
    std::string line;
    std::getline(std::cin, line);
    response = line[0];
    //...

其他人也提到了风格问题,但眼前的问题是:

  cout << "do you want to add another name? ";
  cin>> response;
  count = count + 2;      
  cout<< endl;        
} while (tolower(response)=='y');

start()函数中,使用getline()可以正确地读取完整的一行,但在这里,cin >> response;会在输入缓冲区中留下一行换行符和其他内容。相反,试试这个:

std::string response;
//...
  cout << "do you want to add another name? ";
  getline(cin, response);  // Changed here
  count = count + 2;      
  cout<< endl;        
} while (tolower(response[0])=='y'); // And changed here

首先使用显式循环和break,而不是do while,因为这可以避免缓冲区溢出并整理代码。我还添加了jxh响应中的修复程序。像这样的东西。(未测试)

#include <iostream>
#include <cmath>
#include <string>
using namespace std;
void start(int c);
string NameArray [50];
std::string response;
int main() {
  for(int count=0; count<50; ++count) {
    start(count);
    cout << "do you want to add another name? ";
    std::getline(cin,response);
    if (tolower(response[0])=='y') break;
  }
  cout<< "program Ends" <<endl;        
  system ("pause");        
  return 0;     
}    
void start(int count) {         
  cout<< "Enter your First and Last name: ";        
  std::getline(cin, NameArray[count]);       
  cout<< NameArray[count] <<endl;       
  cout<< endl;          
}

但实际上,固定长度数组是一个问题,我会通过使用向量来完全避免。那你就可以完全摆脱伯爵了。代码看起来更像这样:

#include <iostream>
#include <string>
#include <vector>
std::string ask_user_for_name();
std::vector<string> NameArray;
int main() {
  do {
    NameArray.push_back( ask_user_for_name() );
    std::string response;
    cout << "do you want to add another name? ";
    std::getline(cin,response);
    if (tolower(response[0])=='y') break;
  } while(true);
  cout<< "program Ends" <<endl;        
  system ("pause");        
  return 0;     
}    
std::string ask_user_for_name() {         
  cout<< "Enter your First and Last name: ";        
  std::string retval;
  getline(cin, retval);
  cout<<retval<<std::endl<<std::endl;
  return retval;       
}

伙计们把这件事搞得太复杂了。你打给cin的第二个电话正在读取一条换行符并立即返回。最快的修复方法是:在getline调用之前添加cin.ignore():

cin.ignore();
getline(cin, NameArray[count]);