Text File

Hello There, how do i get strings and doubles to work together to put it in a text 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
  #include <iostream>
#include <fstream>

using namespace std;

void main ()
{
	char prod[30];
	double price;
	ofstream outfile("prodfile.txt");
	cout << "Enter product ID(Only enter to exit): ";
		cin.getline(prod,29);
		outfile << prod << endl;
		cout << "Enter price: ";
		cin >> price;
		outfile << price << endl;
		cin.get();
	while (prod[0]!='\n') 
	{
		cout << "Enter product ID: ";
		cin.getline(prod,29);
		outfile << prod << endl;
		cout << "Enter price: ";
		cin >> price;
		outfile << price << endl;
		cin.get();
	}
	outfile.close();


	system ("pause>0");
}


It's not working at all it just keeps getting my inputs.

Also, could you give me information on how to exit the program whenever I want?(Suppose the program is working correctly)like when I pressed enter or entered 0 on both fields it exits?
Last edited on
You might do it like this, using C++ std::string. Here, I've used an infinite loop, break out of it when the user enters a blank string.
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
    string prod;
    double price;
    ofstream outfile("prodfile.txt");
    
    while (true)
    {
        cout << "Enter product ID: ";
        getline(cin, prod);
        
        if (prod.size() == 0)
            break;        
        
        cout << "Enter price: ";
        cin >> price;
        outfile << prod << " " << price << endl;
        cin.get();
    }
    
    outfile.close();

}


(you could do something similar with a char array, length == 0 would meant the first character was the null terminator. But std::string is safer, less worry about what happens if the user enters too many characters).
Ok, I managed to do it, now how do i output it on the screen? What should i put in the condition for the while loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void main()
{
	char prod[30];
	double price;
	ifstream infile("prodfile.txt");
	cout << char(205) << char(205) << "These are the products" << char(205) << char(205) << endl;

	while ()
	{
		infile >> prod >> price;
		cout << prod << '\n';
		cout << price << endl;
	}
	infile.close();
Last edited on
When reading from an input file, this style is generally applicable:
1
2
3
4
    while (infile >> prod >> price)
    {
        cout << prod << '\n' << price << endl;
    }


For a bit of background on how the while condition works, you could look here: http://www.cplusplus.com/forum/beginner/117195/

I'm sure there are better explanations, but that may help a bit.
while (infile >> prod >> price) isn't working at all.

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

using namespace std;

void main ()
{
    string prod;
    double price;
    ofstream outfile("prodfile.txt");

    while (true)
    {
		
        cout << "Enter product ID: ";
        getline(cin, prod);
		if (prod.size()==0)
		{
			break;
		}
		cout << "Enter price: ";
        cin >> price;
		if (price==0)
		{
			break;
		}
		 outfile << prod << '\n' << price << endl;
        cin.get();
		
    }
    
    outfile.close();
}


ifstream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void main()
{
	string prod;
	double price;
	ifstream infile("prodfile.txt");
	cout << char(205) << char(205) << "These are the products" << char(205) << char(205) << endl;

	while (infile >> prod >> price)
	{
		cout << "Product: " <<  prod << endl;
        cout << "Price: " << price << endl;	
	}

	infile.close();
	cout << char(205) << char(205) << "End" << char(205) << char(205) << endl;

	system("pause>0");
}
The code above works for me, except that void main() is not valid according to the C++ standard. It should be int main().

Other than that one small change, I tested it in two different compilers and it worked ok both times.


Topic archived. No new replies allowed.