当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - cin >> function template arg of unknown type

问题描述:

I have a function template and main as follows:

 template <class type > type* calculate(type inputVal) {

type val;

static int counter = 0;

static type sum = inputVal;

static type average = inputVal;

static type* address = &sum

do {

cout << "Enter value: ";

cin >> val;

counter++;

sum += val;

average = sum / counter;

} while (!cin.eof());

return address;

}

void main() {

int num;

cout << "Enter Value: ";

cin >> num;

int *ptr = calculate(num);

cout << "SUM: " << *ptr << " AVG: " << *(ptr+1);

}

My problem is that this should work with different input types instead of just int, so if a user first enters a float it'll treat everything as that type, as wells as if the user enters a char.

Also the template function cannot print the end values.

网友答案:

A normal variable sum is treated as one-element array for pointer aritimetic (N3337 5.7 Additive operators) and when ptr points at it, ptr+1 doesn't point at a valid object and therefore must not be dereferenced. If you want continuous memory region, use arrays.

Also note that

  • Checking !cin.eof() after updating sum and average doesn't seem a good idea because it will use an invalid (duplicate) data. Check if input is successful before processing the data.
  • Declareing void main() (or main with return type other than int) in the global namespace is illegal in the standard C++. Unless you have some special reason -- for example, your boss or teacher forbids writing standard-compliant code -- you should use int main() (in this case).
  • You should initialize counter to 1 to put inputVal in the number. Avoiding getting input as argument to avoid writing duplicate code seems better.

Try this:

#include <iostream>
using std::cin;
using std::cout;

template <class type > type* calculate(type inputVal) {
  type val;
  static int counter = 1;
  static type buffer[2];
  type& sum=buffer[0];
  type& average=buffer[1];
  sum=average=inputVal;
  static type* address = buffer;

  for(;;) {
    cout << "Enter value: ";
    if(!(cin >> val)) break;
    counter++;
    sum += val;
    average = sum / counter;
  }
  return address;
}

int main() {
  int num;
  cout << "Enter Value: ";
  cin >> num;
  int *ptr = calculate(num);
  cout << "SUM: " << *ptr << " AVG: " << *(ptr+1);
}

Or this without input from the argument:

#include <iostream>
using std::cin;
using std::cout;

template <class type > type* calculate() {
  type val;
  static int counter = 0;
  static type buffer[2];
  type& sum=buffer[0];
  type& average=buffer[1];
  sum=0; // the type used has to be capable to be converted from integer 0
  average = 0;
  static type* address = buffer;

  for(;;) {
    cout << "Enter value: ";
    if(!(cin >> val)) break;
    counter++;
    sum += val;
    average = sum / counter;
  }
  return address;
}

int main() {
  int *ptr = calculate<int>();
  cout << "SUM: " << *ptr << " AVG: " << *(ptr+1);
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: