using istream for input

I'm trying to figure out how to use istream for taking input for a class. We barely talked about it in class and my teacher didn't give us so much as an example or explanation.

Header.h file
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
#include <iostream>
#include <string>

using namespace std;

class Alcohol{
private:
protected:
	string type;
public:
	void setType(int);
	string getType();
};

class Vodka:public Alcohol{
private:
	string flavor;
	double percentage;
protected:
public:
	friend istream& operator>>(istream& is, Vodka& vodka);
	void setFlavor(string);
	void setPercent(int);
	string getFlavor();
	double getPercent();
};


void Alcohol::setType(int c){
	if (c == 1)
		type = "Vodka";
	else if (c == 2)
		type = "Beer";
}
string Alcohol::getType(){
	return type;
}

void Vodka::setFlavor(string f){
	flavor = f;
}
void Vodka:: setPercent(int p){
	percentage = p;
}
string Vodka::getFlavor(){
	return flavor;
}
double Vodka::getPercent(){
	return percentage;
}


main.cpp file
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
#include <iostream>
#include <string>
#include "Header.h"

using namespace std;

int main(){

	Alcohol selection;
	string input;
	int choice;

        //select which alcohol they have
	cout << "Enter 1 for Vodka, 2 for Beer.\n";
	cin >> choice;
	selection.setType(choice);
	cout << selection.getType() << endl;

        //if they choose vodka, get vodkas characteristics and output them
	if(selection.getType() == "Vodka"){
		Vodka vodka;
		cout << "What is the flavor of vodka?\n";
		//cin >> vodka.setFlavor();
		istream& operator>>(istream& ifs, Vodka& vodka);
		ifs >> vodka.setFlavor();
		cout << "What percentage alcohol is it?\n";
		//cin >> vodka.setPercent();
		cout << "You have " << vodka.getFlavor() << "flavored " << selection.getType() << " that is " << vodka.getPercent << "% alcohol.\n";
	}
	else if(selection.getType() == "Beer")
		Beer beer;
	else
		cout << "ERROR!\n";

	system("PAUSE");
	return 0;
}


It say ifs is undefined. The one istream example from him:
istream& operator >> (istream& if, complex &c)
complex being class name, c being initialized as that class, if should be something else since "if" is a reserved word.

Will gladly take all constructive criticisms since I need it to learn. Thanks much!
your line 24 is more like a method declaration than a method call.

http://www.tutorialspoint.com/cplusplus/input_output_operators_overloading.htm

read that for an example of how to call in main()
Last edited on
Thank you so much! I was looking for examples and was having a hard time finding any. After playing with it for a bit and reading that I figured it out!
Topic archived. No new replies allowed.