help with while loops.

i got this code to test fstream its functions. and i am getting a error only on visual c++ console applications. is there a another way to get the while loop running?
i get the error at ">>" for no operator matches these operands
 
while (objectFile >> name >> power) {

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
//displayItems
void displayItems(int x) {

	ifstream objectFile("objects.txt");
	string name;
	double power;

	if (x == 1) {
		while (objectFile >> name >> power) {
			if (power == 0) {
				cout << ' ' << name << ' ' << power << endl;
			}

		}
	}

	if (x == 2) {
		while (objectFile >> name >> power) {
			if (power>0) {
				cout << ' ' << name << ' ' << power << endl;
			}
		}
	}

	if (x == 3) {
		while (objectFile >> name >> power) {
			if (power<0) {
				cout << ' ' << name << ' ' << power << endl;
			}
		}
	}

I'm not sure but try to remove a > from
 
while (objectFile >> name >> power)

So it's
 
while (objectFile > name > power)

Not sure why it wouldn't work.
:( its putting out 1000's more errors.

one error if set '<<'

C2679: binary'>>' no pparater found which takes a right hand openhand of type 'stdstring'(or there is no acceptable conversion)
:(
is compiling in 64bit makes a diffrence
Last edited on
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

#include <iostream>
#include <fstream>
using namespace std;

int getWhatTheyWant();
void displayItems(int x);

//main function
int main() {

    int whatTheyWant;

    whatTheyWant = getWhatTheyWant();

    while(whatTheyWant != 4){
        switch (whatTheyWant){
            case 1:
                displayItems(1);
                break;
            case 2:
                displayItems(2);
                break;
            case 3:
                displayItems(3);
                break;

        }
        whatTheyWant = getWhatTheyWant();


    }

}
//getWhatTheyWant function
int getWhatTheyWant(){
    int choice;
    cout<<"|-----------------|"<<endl;
    cout<<endl;
    cout<<"  ------------------"<<endl;
    cout<<" | 1 - Plain items  |"<<endl;
    cout<<"  ------------------"<<endl;
    cout<<" | 2 - Helpful items|"<<endl;
    cout<<"  ------------------"<<endl;
    cout<<" | 3 - Harmful items|"<<endl;
    cout<<"  ------------------"<<endl;
    cout<<" | 4 - Quit program |"<<endl;
    cout<<"  ------------------"<<endl;
    cout<<endl;
    cin >> choice;
    cout <<endl;
    return choice;
}

//displayItems
void displayItems(int x){

    ifstream objectFile ("objects.txt");
    string name;
    double power;

    if(x==1){
        while(objectFile >> name >> power){
            if(power==0){
                cout <<"|-----------------|"<<endl;
                cout << ' '<<name << ' ' << power << endl;
            }

        }
    }

    if(x==2){
        while(objectFile >> name >> power){
            if(power>0){
                cout <<"|-----------------|"<<endl;
                cout<<' '<< name << ' ' << power << endl;
            }
        }
    }

    if(x==3){
        while(objectFile >> name >> power){
            if(power<0){
                cout <<"|-----------------|"<<endl;
                cout<<' '<< name << ' ' << power << endl;
            }
        }
    }

}

Is it fixed? Working fine on Xcode.
What does the errors say?
Topic archived. No new replies allowed.