Bringing in an .txt file

Write a program that displays the contents of the file in three columns titled “Name”, “Quantity”, and “Price”. The program should also display a fourth column that contains the result of multiplying each item’s quantity by its price. Use “Value” as the column’s title. (Hint: You can align the columns using '\t', which is the escape sequence for the Tab key.) In addition, the program should calculate and display the total value of the items in inventory. Display the price, value, and total value with two decimal places.

.txt file contains:
Watch#25#40
Ring#55#99.99
Bracelet#60#20
Earrings#65#17.85
Pins#20#53.5

I only need help with how to put it into columns and also to get the value in column 4

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

using namespace std;

int main()
{
	ifstream qFile("Advanced25.txt", ios :: app);
	string qWord;
	
	if (!qFile.good())
	{
		cout <<"COuld not open the file!" << endl;
		return 1;
	}//end if
	
	while (!qFile.eof())
	{
		qFile >> qWord;
		cout << qWord;
	}//end while
	qFile.close();

	return 0;
}// end main function 
Last edited on
Hello liz Lopez,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Your program is a good start, but as you can see it does not work.

Line 5. It would be best not to use this. It WILL get you in trouble some day.

Line 9. If it works on an input file, using "ios::app" will open the file with the file pointer at the end. Not what you want.

Checking that the file opened is good.

Line 18. Looping on "eof" will not work the way you think it will. Usually you will process the last read twice before the while condition becomes false.

Line 20. will read the entire line as you should have seen. With the use of the "#" between each field using "std:getline" would work better to read each field into a different variable.

Using the header file "<iomanip>" will help to space out the output the way you want.

I will work up an example for you shortly.

Hope that helps,

Andy
Hello liz Lopez,

I think this might give you an idea for what you want. Notice the comments in the code.

Question, Were you given the text file to use or did you make it yourself? Changing the text file will change the way the code is written. If you can change the text file I can show you a different way to read the 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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <chrono>
#include <thread>

//using namespace std;

int main()
{
	std::ifstream qFile("Advanced25.txt");
	std::string qWord;
	std::string item;
	std::string sQty, sPrice;  // <---Used for reading file
	double qty{};
	double price{};

	if (!qFile.good())
	{
		std::cout << "Could not open the file!" << std::endl;
		// <--- Needs a pause here to read the output before ending the program.
		std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"
		return 1;
	}//end if <--- Good comment.

	std::cout << "\n     Item       Qty      Price" << std::endl;

	std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Usual way I setup the output.

	while (std::getline(qFile, item, '#'))  // <--- Usual way of reading a file with a while loop.
	{
		std::getline(qFile, sQty, '#');  // <--- Read as a sring.
		qty = std::stod(sQty);  // <--- Convert to a double.

		std::getline(qFile, sPrice, '\n');
		price = std::stod(sPrice);

		// <--- Output line to format the output.
		std::cout << std::setw(10) << item << std::setw(8) << std::noshowpoint << std::setprecision(0) << qty << std::setw(12) << std::setprecision(2)<< price << std::endl;
	}//end while <--- Good comment.

	qFile.close();

	return 0;
}// end main function 


Hope that helps,

Andy
Andy,
Thank you oh so much for the help. For some reason, I'm getting an error message for lines 34 and 37.

[Error] 'stod' is not a member of 'std'
Hello liz Lopez,

Strange. "stod" is part of the "string" header file, so it is in the "std" namespace. This should not be a problem.

In the past I have had a problem like this where I hd to highlight the offending type and retype the same thing before it worked. Give that a try and see what happens.

I do know that the code works fine for me. Maybe someone else might have an idea.

Hope that helps,

Andy
Here's another way to read and calculate the values.
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
  string item;
  double quantity = 0.0;
  double price = 0.0;
  double value = 0.0;
  char sep;
  ifstream src("Advanced25.txt");
  
  if (!src)
  {
    perror("Error: ");
    return errno;
  }
  while (getline(src, item, '#'))
  {
    src >> quantity;
    src >> sep; // reads #
    src >> price;
    value = quantity * price;
    // TO DO output your variables here
  }
}
Andy, still did not work. Thank you anyways.



The information you provide was really useful but but you can see it does not work. Here you can Change the text file and also change the way the code is written.
Hello liz Lopez,

Sorry for the delay, I have been down with a cold or flu the past few days.

The program I showed you compiles and runs here just fine under the C++11 standards, but not under the C++98 standards.

I am thinking that your compiler my be old and not understanding the newer standards.

I do not know what IDE you are using, but somewhere there should be a check box for the C++11 standard or if you are using the compiler from the command line you may need to add "-std=C++11" to the command line switches to use the newer standards as long as the compiler is able to use this. Otherwise you may want to update the compiler.

A work around might be to include the header file "cstdlib" or just "stdlib.h" and use the "strtod()" function. I have not tried this yet.

Hope that helps,

Andy

Edit:

Upon further testing I added the header file "cstdlib" removed the header files "chrono" and "thread" changed "strtod" to "atof" along with initializing the doubles with "= 0.0" I put a comment on line 23 because it used "chrono" and "thread" and it compiled and worded under the C++98 standards.

In the function "atof" I changed "sQty" and "sPrice" to "sQty.c_str()" to use a C string that is needed by "atof".
Last edited on
Topic archived. No new replies allowed.