Function not working properly

May 5, 2013 at 11:51am
Hello I am writing a program which prints out information from a file depending on what input the user gives. But I have a problem with one function. First of all I get an error which says that a variable or field is declared void. This is in my displayitems function. I assume that it has something to do with my parameter. Secondly: In my main function I get the error in my if statement that displayItems has to many arguments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  #include <iostream>
#include <fstream>

int getWhatTheyWant();
void displayItems();
//main function
int main(){

int WhatTheyWant;
WhatTheyWant = getWhatTheyWant();

while(WhatTheyWant != 4){
if( WhatTheyWant > 0 && WhatTheyWant < 5){
WhatTheyWant = getWhatTheyWant();


   displayItems(WhatTheyWant);
}


}
}
// return input function
int getWhatTheyWant(){
int choice;
std::cout<< " 1 - plain items " << std::endl;
std::cout<< " 2 - Harmfull items " << std::endl;
std::cout<< " 3 - Beneficial items " << std::endl;
std::cout << " 4 -  quite program " << std::endl;

std::cin >> choice;
return choice;


}
void displayItems(WhatTheyWant){
std::string name;
int power;
std::ifstream objectfile("objects.txt");

if ( WhatTheyWant == 1){
    while(objectfile >> name >> power ){
        if(power == 0) {
            std::cout<< name << power << std::endl;

        }


    }
}
     if(WhatTheyWant == 2){
            while(objectfile >> name >> power ){
        if(power < 0) {
std::cout<< name << power << std::endl;
}

}
     }
     if( WhatTheyWant == 3){
while(objectfile >> name >> power){
    if( power > 0){
        std::cout << name << power << std::endl;

    }
}
     }
}

What am I doing wrong? Please help me!
Kind regards,
Skilzz
May 5, 2013 at 12:20pm
On line 5 you forgot to list the parameters and on line 36 you forgot to specify the type of the parameter.
Last edited on May 5, 2013 at 12:20pm
May 5, 2013 at 12:22pm
Thank you! The program works now!
May 5, 2013 at 1:16pm
Ok , I have another problem. The program starts with giving you 4 numbers you should enter, described in my getWhatTheyWant function.
Subsequently you have to enter a number. Every number has it own specific items. The first time it shows the right items. However, If I enter a different number the program gives me the same items as it did in the first run. Despite the fact that is a different number.
My code;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <fstream>

int getWhatTheyWant();
void displayItems(int WhatTheyWant);
//main function
int main(){

int WhatTheyWant;
WhatTheyWant = getWhatTheyWant();

while(WhatTheyWant != 4){

if( WhatTheyWant > 0 && WhatTheyWant < 5){



displayItems(WhatTheyWant);
getWhatTheyWant();

}


}
}
// return input function
int getWhatTheyWant(){
int choice;
std::cout<< " 1 - plain items " << std::endl;
std::cout<< " 2 - Harmfull items " << std::endl;
std::cout<< " 3 - Beneficial items " << std::endl;
std::cout << " 4 -  quite program " << std::endl;

std::cin >> choice;
return choice;


}
void displayItems(int WhatTheyWant){
std::string name;
int power;
std::ifstream objectfile("objects.txt");

if ( WhatTheyWant == 1){
    while(objectfile >> name >> power ){
        if(power == 0) {
            std::cout<< name << power << std::endl;

        }


    }
}
     if(WhatTheyWant == 2){
            while(objectfile >> name >> power ){
        if(power < 0) {
std::cout<< name << power << std::endl;
}

}
     }
     if( WhatTheyWant == 3){
while(objectfile >> name >> power){
    if( power > 0){
        std::cout << name << power << std::endl;

    }
}
     }
}

Can someone help me out?
Last edited on May 5, 2013 at 1:21pm
May 5, 2013 at 1:43pm
Maybe you want line 19 to be WhatTheyWant = getWhatTheyWant();
May 5, 2013 at 1:45pm
line 19 should be WhatTheyWant = getWhatTheyWant();
May 5, 2013 at 1:53pm
It works, thank you both. However, I don't genuinly understand why this should be like this. Could you explain it?
May 5, 2013 at 2:03pm
Because that's what you did to get the user choice line 10.
May 5, 2013 at 2:06pm
Ah, ok. So can't just write execute function without assigning it again to my variable. If I just would let the code execute it would give me the number and the output of that function. But why won't it run disPlayItems?
Or atleast show something on the screen.
Last edited on May 5, 2013 at 2:09pm
May 5, 2013 at 2:08pm
Well, you can, but then the variable will not update.
May 5, 2013 at 2:20pm
Wow, I fully understand it now haha. Update was the keyword.
Thank you!
Programming can be really frustrating, but I really want to learn it before I go enroll into university computer sience. Does it ever get easy?
Last edited on May 5, 2013 at 2:27pm
Topic archived. No new replies allowed.