Question

I have found 4 tasks on recruitcoders.com and I have completed all of them, but in the first one i have scored only 1/10:

Write a program that works as a simple calculator that supports five operations: addition, subtraction, multiplication, division and modulo.

Input: There is an unknown number of tests. Each test consists of one-character symbol which corresponds to specific operation (+ addition, - subtraction, * multiplication, / division and % modulo) and two following integers. Each test will be separated by spaces and followed by a newline. Number of tests doesn't exceed 100 and the result is less than 2^31. You can assume that there is no situation in which you would have to divide by 0.

Output: For each test you should print a single number being the result of each operation.

Example:

Input:
  + 7 9
  - 0 4
  * 5 6
  / 8 3
  % 5 2

Output:
  16
  -4
  30
  2
  1

MyCode:

#include <iostream>
using namespace std;
int fcount(char, int, int);
int main() {
    char znak;
    long a, b;
    long* wynik=new long[100];
    for(char i=0;i<100;i++){
    cin>>znak>>a>>b;
    wynik[i]=fcount(znak,a,b);
    }
    for(char i=0;i<100;i++)
        cout<<wynik[i]<<endl;
    return 0;
}
int fcount(char znak, int a, int b){
    switch(znak){
        case '+':
        return a+b;
        case '-':
        return a-b;
        case '*':
        return a*b;
        case '/':
        return a/b;
        case '%':
        return a%b;
    }
}

THIS CODE IS WORKING FINE, IT IS JUST UNDERRATED BY RECRUITCODERS (1/10)

I am not asking you for better code, I just wonder where am I loosing so many points in such an easy task? Any suggestions? I have completed all 4 tasks scoring 28/40 total (1/10, 10/10, 10/10, 7/10), so the task with score 1/10 is a pain in a** for me :/

Was it helpful?

Solution

The requirement says that there is an unknown number of tests, but you are assuming exactly 100 tests. Change it to:

while (cin >> znak >> a >> b)
    cout << fcount(znak, a, b) << endl;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top