Bad Output

I sent my Exercise to my teacher already, but my question is about the output.

I want to the output to be something like this.

1
2
3
4
5
6
7
8
9

LoriBeth Allen      19596.74
Chachi Arcola       49062.50
Richie Cunningham   91816.74
Howard Cunningham  204178.50
Marion Cunningham  199402.56
Joanie Cunningham  171933.84
...


But I got this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

LoriBeth Allen       19596.74
Chachi Arcola      49062.50
Richie Cunningham  91816.74
Howard Cunningham 204178.51
Marion Cunningham 199402.57
Joanie Cunningham 171933.85
Al Delvecchio  46735.76
Arthur Fonzarelli 204178.51
Ralph Malph       85486.50
Roger Phillips    11683.94
Jenny Piccalo      9498.50
Potsie Weber      138474.00


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

/**

Name: William Lee
Compiler: GNU GCC Compiler
Lab Exercise #2

*/

using namespace std;

    int main(){

    ifstream infile;
    infile.open("Ex2.txt");
    const int NUMBERS = 12;
    int radii[NUMBERS];
    string firstname[NUMBERS], lastname[NUMBERS];
    const float PI = 3.14;
    int count = 0;

    if (infile){
        while (count < NUMBERS && infile >> firstname[count] >> lastname[count] >> radii[count]){
            count++;
        }
        infile.close();
        for (count = 0; count < NUMBERS; count++){
            static_cast<double>(radii[count]);
            double area = pow(radii[count], 2.0) * PI;
            cout << firstname[count] << " " << left << setw(10) << lastname[count];
            cout << right << setw(10) << setprecision(2) << fixed << showpoint << area << endl;
        }
    }
    else{
        cout << "Can't open the file.";
    }
    return 0;
    }
Hello leewilliam236,

I most generally have to play with the value of setw till I get it right, but what yu might try is something like this:
 
std::cout << std::left << std::setw(20) << firstname[count] + " " + lastname[count];

followed by the next cout. You might want to bump the setw from 10 to 11. Not necessary, your choice.

Hope that helps,

Andy
Last edited on
Hello leewilliam236,

Your input file would be useful for testing the program.

Andy
1
2
3
4
5
6
7
8
9
10
11
12
LoriBeth Allen      79
Chachi Arcola      125
Richie Cunningham  171
Howard Cunningham  255
Marion Cunningham  252
Joanie Cunningham  234
Al Delvecchio      122
Arthur Fonzarelli  255
Ralph Malph        165
Roger Phillips      61
Jenny Piccalo       55
Potsie Weber       210
Hello leewilliam236,

Thank you for the input file it was a big help.

This may work for you or be a starting point to work out what you want.
1
2
std::cout << " " << std::left << std::setw(20) << firstname[count] + " " + lastname[count];
std::cout << std::right << std::setw(10) << std::setprecision(2) << std::fixed << std::showpoint << area << std::endl;


When I started working with files I came up with this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	std::string iFileName{ "" };  // <--- Put input file name here.

	std::ifstream inFile;

	inFile.open(iFileName);

	if (inFile.is_open())
	{
		// I comment out these lines when I know it is working.
		std::cout << "\n File " << iFileName << " is open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
	}
	else
	{
		std::cout << "\n File " << iFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		exit(1);  // <--- Exits program because there is no reason to continue.
	}


It is always a good practice to make sure that the file is open before trying to use it. What code you come up with is up to you. there are several ways of doing this. Some peple will put the main part of the program inside the if statement, but I like to keep things separate. Once you understand how all this works the code can be shortened. By then you should understand enough to figure it out.

I made one other change. I set PI = M_PI which comes from the "<math.h>" header file which comes from the "<cmath>" header file. The only way to use these built in "#defines" is to use "#define _USE_MATH_DEFINES" before you include "<cmath>", i.e.,
1
2
#define _USE_MATH_DEFINES
#include <cmath> 


I think this works, it has been awhile since I worked with it. Actually I changed the "<cmath>" file and put the "#define _USE_MATH_DEFINES" right before the "#include <math.h>"this way it is always available for me. Editing the "<cmath>" file may not be easy if the directories and sub directories are "read only" protected.

Using PI with a greater precision did change the numbers in the final output.

Hope that helps,

Andy
closed account (48T7M4Gy)
Also, the combination or by using the dummy read.
Last edited on
Thanks. My class doesn't really do std::stream manipulator anyways.
Topic archived. No new replies allowed.