Why is thi not working?

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void main()
{
	string pdtname;
	int index=0,qty=0,price=0;

	ifstream productslist;	
	productslist.open("list.txt");
	cout<<""<<endl;
	cout<<"_______________________________________________"<<endl;
	cout<<""<<endl;
	cout<<"Product Name \t\tQty. \t\tPrice"<<endl;
	cout<<"_______________________________________________"<<endl;
	cout<<""<<endl;

	//for (int i=0;i<8;i++)
	//{
	productslist>>index>>pdtname>>qty>>price;
	//productslist>>qty;
	//productslist>>price;
	cout<<index<<pdtname<<qty<<price<<endl;
	productslist.close();
	//}

}




list text

001 Baby Oil 100 1500
002 Feeding Bottle 50 2500
003 Baby Cream 50 500
004 Tricycle 15 6500
005 Spoon 100 100
006 Cotton Buds Pack 50 75
007 Baby Ear Cleaner 30 500
008 Bath Tub 15 200
>void main()

lol
What is it supposed to do? What does it do?
I believe using int main() would do something.
The problem you are having is that "Baby Oil" contains a space. When reading pdtname, the program stops on the space after "Baby". Then when the program tries to read "qty", it sees "Oil", which is non-numeric, and returns "0". The "Oil" remains in the stream, and the same thing happens with price.

For now, I would suggest sticking with 1-word products so you don't run into this problem. There are ways to solve this problem, but for a beginner I would suggest just using a different data set until you understand the basics.

By the way, the void main() -- The standards require that int main() be used. Some compilers allow the non-standard void main() for various reasons (mainly historical, I suspect). Some books and teachers use the void main() structure because they are not aware of the correct way, or they don't want to explain why main is supposed to return a value. Get in the habit of using int main() If you don't supply your own return statement, it will return for you.
Topic archived. No new replies allowed.