0

Like the title says I'm having a problem with calling my method in c++. I tried to solve it on my own I'm just stuck so I decided to post the question here.

So can someone please tell me what I am doing wrong? Thanks in advance!

my code:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int counter;

int _tmain(int argc, _TCHAR* argv[])
{
    // START_CONFIG
    string naam = "XX";                 //student naam.
    string klas = "XX";                 //student klas.
    string studentNummer = "XX";        //student nummer.
    int min_nummer = 5;                     //minimale aantal getallen.
    int max_nummer = 25;                    //maximale aantal getallen.
    int min_random_nummer = 0;              //minimale getal van de random getallen.
    int max_random_nummer = 9;              //maximale getal van de random getallen.
    // END_CONFIG

    // stap 1. (Print naam, klas en student nummer)
    cout << "Naam: " << naam << "\n";
    cout << "Klas: " << klas << "\n";
    cout << "Student Nummer: " << studentNummer << "\n";
    cout << "\n \n";

    // stap 2. (Vraag de gebruiker om het aantal getallen en lees dit)
    int aantal;
    cout << "Met hoeveel getallen wilt u spelen? \n Kiest u alstublieft een getal tussen " << min_nummer << " en de " << max_nummer << ": \n";
    cin >> aantal;
    cout << "\n";
    while (aantal < min_nummer || aantal > max_nummer) {
        cout << "Dit is een foutief getal, kiest u alstublieft een getal tussen de " << min_nummer << " en de "<< max_nummer << ": \n";
        cin >> aantal;
        cout << "\n";
    }

    // stap 3 en 4. (Maak een array aan voor de getallen en vul deze met random getallen)
    int getallenArray[26];
    for (int i = 1; i <= aantal; i++) {
        int randomNumber = rand() % max_random_nummer + min_random_nummer;
        getallenArray[i] = randomNumber;
    }   

    // stap 5. (Print de getallen uit de array op 1 regel)
    for (int i = 1; i <= aantal; i++) {
        cout << "nr " << i << " random number " << getallenArray[i] << "\n";
    }

    // stap 6. (Vraag de gebruiker voor welk getal hij wilt zoeken)
    cout << "Voor welke getal wilt u zoeken? /n Uw kunt alleen kiezen uit getallen van "<< min_random_nummer << " t/m " << max_random_nummer << " \n";
    int gezochtGetal;
    cin >> gezochtGetal;
    while (gezochtGetal < min_random_nummer || gezochtGetal > max_random_nummer) {
        cout << "Voor welke getal wilt u zoeken? /n Uw kunt alleen kiezen uit getallen van " << min_random_nummer << " t/m " << max_random_nummer << " \n";
        cin >> gezochtGetal;
    }

    // stap 8. (roep deze methode aan en print het aantal uit)
    count(gezochtGetal, getallenArray, 26);
    cout << "Het getal: " << gezochtGetal << "komt " << counter << "in de array \n";

    // stap 9. (bereken welk percenage dit aantal van het totale aantal getallen is.
    double percentage = (counter / aantal) * 100;
    cout << "dat betekent dat " << percentage << "% van de getallen in het array gelijk is aan " << gezochtGetal << "\n";
}

// stap 7. (maak een methode)   
int count(int number, int array[], int length){
    for (int i = 1; i < length; i++) {
        if (array[i] == number) {
            counter++;
        return counter;
        }
    }
}

error:

Error   1   error C2782: 'iterator_traits<_Iter>::difference_type std::count(_InIt,_InIt,const _Ty &)' : template parameter '_InIt' is ambiguous    c:\users\kaspe_000\documents\visual studio 2013\projects\getal2\getal2\getal2.cpp   59  1   Getal2
kaspertje15
  • 3
  • 1
  • 6
  • My comment is quite off-topic, but unless you have a weird specification that forces you to write code in... (I'm guessing) German, always write your code (your variables, string literals and comments) in English. Most companies do that and it makes other people help you debug your code, because they can understand the meaning of the variables, classes, etc. – dmg Jan 28 '15 at 14:59

2 Answers2

2
count(gezochtGetal, getallenArray, 26);

This won't call your count function since it hasn't been declared yet.

However, it will try to call the standard library function template std::count, since you've used using to dump most of the library into the global namespace.

You need to declare the function before you call it; either move the definition before that of main, or add a declaration before main:

int count(int number, int array[], int length);

It would be better to keep things out of the global namespace to avoid this kind of conflict. Avoid using namespace std; and, ideally, put your own declarations inside a namespace.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I will try it right away! The only reason why I use namespace std because else cin and cout dont work. – kaspertje15 Jan 28 '15 at 14:30
  • @kaspertje15: Use the qualified names, `std::cin` and `std::cout`. Or use `using std::cin;` to bring just that name, not the whole world, into the global namespace. – Mike Seymour Jan 28 '15 at 14:30
  • Can I ask one more question? – kaspertje15 Jan 28 '15 at 14:50
  • Could you tell me why my variable percentage is zero? Counter and aantal both have values but the program doesnt calculate the percent for me. – kaspertje15 Jan 28 '15 at 14:52
  • @kaspertje15: Because `counter` and `aantal` are both integers, so the result of the division is truncated to zero. Try `(counter * 100) / aantal` or `(double(counter) / aantal) * 100` – Mike Seymour Jan 28 '15 at 14:55
0

The problem is you are not calling your own count method, but you call the one defined in c++ standard library :) You should put a declaration of you count method above main. I would do it like this:

#include "stdafx.h"
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;

int counter;

int count(int number, int array[], int length);

int _tmain(int argc, _TCHAR* argv[])
{

...

Géza Török
  • 1,397
  • 7
  • 15
  • thank you! Could you tell me why my variable percentage is zero? Counter and aantal both have values but the program doesnt calculate the percent for me. – kaspertje15 Jan 28 '15 at 14:51
  • @kaspertje15 Because you are using integer division first, and casting later. That is, `5 / 100` equals `0`. On the other hand `double(5)/100 == 0.05`. – dmg Jan 28 '15 at 14:56
  • Ah like that haha. Thanks! I multiply the variables: (counter * 1.0) / (aantal * 1.0) now and it works like a charm thanks – kaspertje15 Jan 28 '15 at 15:24