编写了一个程序,该程序提供两个重载函数,名为.
mycount(...).This函数,如果传递整数,则计算第二个参数和first参数之间的差异(按此顺序),或者,如果传递一个字符和一个字符串,则计算字符出现的次数。例如,mycount(7, 3)应该返回−4,mycount(’i’, "this is a string")应该返回3。如果没有发生,应该返回0。编写一个简单的main()函数,演示上述两个函数的行为。
我的问题只是从键盘上取下字符串。计算差额的函数工作正常。我也不想强迫用户先输入两个整数,然后输入一个字符和一个字符串,我希望程序独立于顺序传递这些变量。
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int mycount(int x, int y) {
  int difference = y - x;
  cout << "The difference is: ";
  return difference;
}
int mycount(char c, string s) {
  int i;  // loop counter
  int count = 0;
  for (i = 0; i < s.size(); i++) {
    if (s[i] == c) {
      count++;
      cout << "the count is: " << count << endl;
      return count;
    } else
      return 0;
  }
}
int main() {
  int x;
  int y;
  char c;
  string s;
  if (cin >> x && cin >> y) {
    cout << mycount(x, y) << endl;
  }
  if (cin >> c) {
    getline(cin, s);  // i cannot take the string from the keyboard
    cout << mycount(c, s) << endl;
  }
  return 0;
}我不能输入字符串作为输入,程序在输入字符串之前结束,所以就在从键盘上取出字符之后。
发布于 2019-11-03 01:35:09
让我们看一看代码的这一部分:
if(cin >> c)
{
    getline(cin, s); // i cannot take the string from the keyboard
    cout << mycount(c,s) << endl;
}cin >> c从stdin中读取,直到找到空白字符,例如空格、制表符或换行符。每当它击中其中一个时,它就停止读取,将它们留在缓冲区中,并返回它找到的值。
getline(cin, s)读取stdin,直到它找到换行符,从缓冲区中移除字符,并返回它读取的所有内容,直到那个时候为止。
因此,假设您的输入如下所示:
7
3.
我
这是一个字符串
cin >> x将读取7并将换行符保留在缓冲区中。
cin >> y忽略空白,读取3,在缓冲区中保留另一个换行符。
cin >> c忽略空格,读取'i',离开换行符。
getline(cin, s);将读取缓冲区中的所有内容,直到换行符为止。因为缓冲区上的下一个字符是一个新行,所以它返回一个空字符串。
要解决这个问题,您可以在上一个cin之后读取并丢弃一个字符,以去掉新的行字符。
cin.ignore()就是这样做的,所以您的代码可以如下所示:
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int mycount(int x, int y) {
    int difference = y - x;
    cout << "The difference is: ";
    return difference;
}
int mycount(char c, string s) {
    int i; // loop counter
    int count = 0;
    for(i = 0; i < s.size(); i++) {
        if(s[i] == c) {
            count++;
            cout << "the count is: " << count << endl;
            return count;
        } else
            return 0;
    }
}
int main() {
    int x;
    int y;
    char c;
    string s;
    if(cin >> x && cin >> y) {
        cout << mycount(x, y) << endl;
    }
    if(cin >> c) {
        // Get rid of trailing newline character on the buffer
        cin.ignore();
        getline(cin, s);
        cout << mycount(c, s) << endl;
    }
    return 0;
}注意:
您可能想看看您的int mycount(char c, string s);函数的实现,因为在您完成循环之前返回计数或0之后,它不会执行您希望它做的事情。
发布于 2019-11-03 01:41:39
我认为实际的问题是读取两个值,并决定它是两个数字还是一个字符和字符串。您可以通过读取两个字符串并解析它们来实现这一点:
#include "mycount.hpp"
#include <cctype>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::getline;
using std::isdigit;
using std::stoi;
using std::string;
int main() {
    string first;
    cin >> first;
    if (first.length() == 1 && !isdigit(first[0])) {
        // first is char and second is string
        cin.ignore();
        string second;
        std::getline(cin, second);
        cout << mycount(first[0], second) << '\n';
    else {
        // two numbers
        int second;
        cin >> second;
        cout << mycount(stoi(first), second) << '\n';
    }
}https://stackoverflow.com/questions/58676595
复制相似问题