fstream cout problem

I think I have everything right except can't get it to output correctly

I am using ifstream to get First names and sales from a txt file.
The output should be like this if text file has these lines

Jim 1000
Jane 500
Joe 400
Mark 300

*=100

Jim **********
Jane *****
Joe ****
Mark ***


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

using namespace std;

int main(){
	string line;
	ifstream readyPlayerOne("sales.txt");
	std::string firstName{};

	const int MINIMUM = 0,
		hundred = 100;
	int totalSales;
	char symbol = '*';
	
	if (readyPlayerOne.is_open())
	{
		while (readyPlayerOne >> firstName >> totalSales){
			const int MAXIMUM = totalSales / hundred;
			for (int i = MINIMUM; i < MAXIMUM; i++)
				
		std::cout << std::right << std::setw(10) << firstName << ": " << symbol<<endl;
		}
		readyPlayerOne.close();
	}
	
	else cout << " Unable to open file";

	return 0;
}


This is output
before I put for condition it was working correctly for output but, output numbers not asterisk

Jim: *
Jim: *
Jim: *
Jim: *
Jim: *
Jim: *
Jim: *
Jim: *
Jim: *
Jim: *
Jane: *
Jane: *
Jane: *
Jane: *
Jane: *
Joe: *
Joe: *
Joe: *
Joe: *
Mark: *
Mark: *
Mark: *
Thanks for help!!
Last edited on
@briancb2004

You just have your lines of code in the wrong order. Try this.

Also, you don't need the std:: parts of your code, as you have using namespace std;

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 <fstream>
#include<iomanip>

using namespace std;

int main(){
	string line;
	ifstream readyPlayerOne("Sales.txt");
	std::string firstName;

	const int MINIMUM = 0,
		hundred = 100;
	int totalSales;
	char symbol = '*';

	if (readyPlayerOne.is_open())
	{
		while (readyPlayerOne >> firstName >> totalSales)
		{
			int MAXIMUM = totalSales / hundred; // CONST NOT needed
			cout << right << setw(10) << firstName << ": ";
				// First, just print the name
				for (int i = MINIMUM; i < MAXIMUM; i++)
				{
					cout << symbol; // Now print the symbols
				}
				cout << endl; // Now print a newline
		}
		readyPlayerOne.close();
	}

	else cout << " Unable to open file";

	return 0;
}
Last edited on
Thanks you I didn't realize that is what using namespace std; did thanks for tip

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

using namespace std;

int main(){
	string line;
	ifstream readyPlayerOne("sales.txt");
	std::string firstName{};

	const int MINIMUM = 0,
		hundred = 100;
	int totalSales;
	char symbol = '*';
	
	if (readyPlayerOne.is_open())
	{
		cout << "=========================\n";
		cout << "SALES DATA FOR SALES TEAM\n";
		cout << "=========================\n\n";
		cout << "every * = $100 in Sales...\n\n\n";

		while (readyPlayerOne >> firstName >> totalSales)
		{
					
			int MAXIMUM = totalSales / hundred;
			cout << right << setw(10) << firstName << ": ";
			for (int i = MINIMUM; i < MAXIMUM; i++)
			{
				cout << symbol;
			}
			cout << endl;
		}
		readyPlayerOne.close();
	}
	
	else cout << " Unable to open file";

	return 0;
}


final code works like a charm appreciate the help
Hello briancb2004,

An alternative worth looking at. I did not correct everything, but it appears that you did.
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
#include <iostream>
#include <string>
#include <fstream>
#include<iomanip>

int main()
{
    std::string line;
    std::ifstream readyPlayerOne("sales.txt");
    std::string firstName{};

	if (!readyPlayerOne)
	{
		std::cout << "\n File " << std::quoted("sales.txt") << " did not open\n";

        return 1;
	}

   const int MINIMUM = 0,
        hundred = 100;
    int totalSales{};
    char symbol = '*';

    while (readyPlayerOne >> firstName >> totalSales)
    {
        int MAXIMUM = totalSales / hundred;

        for (int i = MINIMUM; i < MAXIMUM; i++)

            std::cout << std::right << std::setw(10) << firstName << ": " << symbol << endl;
    }

    readyPlayerOne.close();  // <--- Not required as the stream will close when the function looses scope. OK if you leave it.

    return 0;
}

I do differ with whitenite1. You should learn to not use using namespace std; as a global variable because of the problems it causes. Better to learn what is in the standard name space and to quilify, (std::), what you need. You have already demonstrated that you can and does not cause any problems to use using namespace std; along with std::string line;, for example.

It is always a good practice to initial your variables when they are defined. Especially numeric variables.

Andy
You don't need the for loop.

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

int main() {
	ifstream readyPlayerOne("Sales.txt");

	if (readyPlayerOne.is_open()) {
		string firstName;
		int totalSales {};
		const char symbol {'*'};
		const int hundred {100};

		while (readyPlayerOne >> firstName >> totalSales)
			cout << setw(10) << setfill(' ') << firstName << ": " <<  setfill(symbol) << setw(totalSales / hundred + 1) << '\n';

		readyPlayerOne.close();
	} else
		cout << " Unable to open file";
}


which for the given input file displays:


       Jim: **********
      Jane: *****
       Joe: ****
      Mark: ***

Topic archived. No new replies allowed.