How to read a Files Data

Pages: 12
closed account (ENhkSL3A)
Am I doing this right guys???????
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    const char* const file_name = "information.txt" ;
    {
        // create the test file
        std::ofstream out_file(file_name) ;
        out_file << " Height Radius Calculation-Type \n"
                 << " 1.0 1.0 C \n 1.0 1.0 V \n 1.0 1.0 S \n 1.0 1.0 D \n 1010.0 250.0 C \n "
                 << "12.3 3.25 V \n 123.0 43.0 S \n 12.3 3.25 K \n 1.12 2.13 C \n"
                 << " -999\n" ; // The last entry in the file is a sentinel to indicate the end of data
                                // (something like -999)
    }

    {
        // sanity check: verify that the file was created correctly
        std::ifstream in_file(file_name) ;
        std::cout << "contents of test file\n-------------------\n" << in_file.rdbuf() ;
    }

    {
        std::ifstream file(file_name) ; // open the file for input

        // read the first line and print it
        std::string first_line ;
        std::getline( file, first_line ) ;
        std::cout << first_line << '\n' ;


        double height ; // The first number in each line is the height
        double radius ; // the second number is the radius
        char calculation_type ; // and the last number is the calculation type (C/V/S)

        // for each height read in, till the sentinel (-999) to indicate the end of data
        while( file >> height && height != -999 )
        {
            file >> radius // read in the radius
                 >> calculation_type ; // and the calculation type

            switch(calculation_type) // switch on the calculation type
            {
                case 'C' : // circumference
                    // ...
                    break ;

                case 'V' : // volume
                    // ...
                    break ;

                case 'S' : // surface area
                    // ...
                    break ;

                default : // invalid
                    std::cout << "invalid calculation type\n" ;
                    // ...
            }
        }
    }
}


Take it up from there.
Hello kns2872,

The program to create the output file will work, but from line 23 to 31 you have used to many '\t's and have extra spaces between the pieces of information. Just one space is needed between each piece of information.

The code for reading the file will work to make sure you can read the file.

The code from JLBorges is a very good start.

Hope that helps,

Andy
closed account (ENhkSL3A)
After case for each one do I just start putting in the equations and then a cout statement to display the solutions? Thank you guys for the help! Also, the notes from both of you are helping me understand this a lot better.
closed account (ENhkSL3A)
Also after case for each one do I need to put the formulas and the cout in {}?
closed account (ENhkSL3A)
Alright guys! It works :D YAY!! Thank you for all your help! I only have one last question! It is displaying all the data and I only need it to display one line. Just the first line that says Height Radius and Calculation Type. Do you know what I have to change to do this? I tried a lot of things yesterday and couldn't figure it out. Here is the final copy of the code.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    const char* const file_name = "information.txt";
    {
        std::ofstream out_file(file_name) ;
        out_file << " Height Radius Calculation-Type \n"
                 << " 1.0 1.0 C \n 1.0 1.0 V \n 1.0 1.0 S \n 1.0 1.0 D \n 1010.0 250.0 C \n "
                 << "12.3 3.25 V \n 123.0 43.0 S \n 12.3 3.25 K \n 1.12 2.13 C \n"
                 << " -999\n" ;
    }

    {
        std::ifstream in_file(file_name);
        std::cout << "contents of test file\n-------------------\n" << in_file.rdbuf();
    }

    {
        std::ifstream file(file_name);

        std::string first_line;
        std::getline( file, first_line );
        std::cout << first_line << '\n';


        double height; 
        double radius; 
        char calculation_type;

        // for each height read in, till the sentinel (-999) to indicate the end of data
        while( file >> height && height != -999 )
        {
            file >> radius // read in the radius
                 >> calculation_type ; // and the calculation type
                         double pi = 3.14159265359;

            switch(calculation_type) // switch on the calculation type
            {
				case 'C' :
				std::cout << (2 * pi * radius) << '\n';
				break ;
				
				case 'V' :
				std::cout << pi * (radius * radius) * height << '\n';
				break ;
				
				case 'S' :
				std::cout << (2 * pi * (radius * radius)) + (height * (2 * pi * radius)) << '\n';
				break ;
				
				default :
                    std::cout << "invalid calculation type\n" ;
				}
		}
	}
}
Hello kns2872,

What you need to change is on line 19. Remove or comment out the part of the line << in_file.rdbuf(). Not quite sure how it is working, but it does print out the entire file.

Looks like everything is working. Good job.

Andy
closed account (ENhkSL3A)
Hey,

So what should I put there for it to print the first line of the file?

Katie
> So what should I put there for it to print the first line of the file?

You already have the code (lines 25-27) to extract and print the first line of the file:
1
2
3
        std::string first_line;
        std::getline( file, first_line );
        std::cout << first_line << '\n'; 


Just comment out lines 17-20 if you do not want to display all the data in the file.
1
2
3
4
    // {
          // std::ifstream in_file(file_name);
          // std::cout << "contents of test file\n-------------------\n" << in_file.rdbuf();
    // } 
Topic archived. No new replies allowed.
Pages: 12