reading numbers from a text document

so the data file is a .txt that is going to be pre-written like this:
numbers:

562
444223.123321 312233.31222 3232323.31122
332231.323223 333232.32323 3232322.32233

am i reading the txt correctly? why are the numbers so weird when i "cout" to check?

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
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <ctime>
#include <stdlib.h>
using namespace std;

  int main()
{
    double mb1; //mass body 1
    double xc1; //x coordinate 1
    double yc1; //y coordinate 1
    double mb2; //mass body 2
    double xc2; //x coordinate 2
    double yc2; //y coordinate 2
    
    
    ifstream inFile;
    inFile.open("data.txt");
    string lineData;
    int lineCount =1;
    
    while(getline(inFile, lineData))
          {
        if (lineCount == 1){
            if(lineData == "0"){
                srand(time(NULL));
            }
            else{
                srand(atoi(lineData.c_str())); //seed value if its 0
            }
        }else if(lineCount == 2)
            {
           
            istringstream lineDataStream(lineData);
            string number;
            int verticalCounter = 1;
           
            while(getline(lineDataStream, number, ' ')) { //line data on second line
                if(verticalCounter == 1)
                    mb1 = atof(number.c_str());
                if(verticalCounter == 2)
                    xc1 = atof(number.c_str());
                if(verticalCounter == 3)
                    yc1 = atof(number.c_str());
                
                verticalCounter++;
            }
            
                
            }
            else
            {
            
            istringstream lineDataStream(lineData);
            string number;
            int verticalCounter = 1;
            
            while(getline(lineDataStream, number, ' ')) {
                if(verticalCounter == 1)
                    mb2 = atof(number.c_str());
                if(verticalCounter == 2)
                    xc2 = atof(number.c_str());
                if(verticalCounter == 3)
                    yc2 = atof(number.c_str());
                
            }
            
        
    
        lineCount++;
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 <sstream>
#include <cstdlib>

int main()
{
    std::istringstream file( // use std::ifstream instead
        "562\n"
        "444223.123321 312233.31222 3232323.31122\n"
        "332231.323223 333232.32323 3232322.32233\n"
               ) ;

    int seed ;
    if( file >> seed )
    {
        std::cout << "seed: " << seed << '\n' ;
        std::srand(seed) ;

        double mb1; //mass body 1
        double xc1; //x coordinate 1
        double yc1; //y coordinate 1

        double mb2; //mass body 2
        double xc2; //x coordinate 2
        double yc2; //y coordinate 2

        if( file >> mb1 >> xc1 >> yc1 >> mb2 >> xc2 >> yc2 )
        {
            std::cout << std::fixed << "body 1: " <<  mb1 << ' ' <<  xc1 << ' ' << yc1 << '\n'
                                    << "body 2: " <<  mb2 << ' ' <<  xc2 << ' ' << xc2 << '\n' ;

            // ...
        }

    }
}
hey @JLBorges, thank you but i already know how to cout the numbers. the problem i am having is that when i do it, the numbers arent coming out. is there something wrong with my code?

because when i press run the output is just
0
Last edited on
> is there something wrong with my code?

Conceptually, yes.

You want to read structured input, but you are using a mechanism (unformatted input) which is designed to be used for reading unstructured sequences of characters.

To read seven numbers from a stream, use formatted input ( operator >> ).

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
64
65
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>

int main()
{
    const char* const path = "data.txt" ;
    
    // create test file
    { 
        std::ofstream(path) << "562\n"
        "444223.123321 312233.31222 3232323.31122\n"
        "332231.323223 333232.32323 3232322.32233\n" ;
    }

    std::ifstream file(path) ; // open file for input
    
    if( !file.is_open() ) // verify that the file was opened
    {
        std::cerr << "could not open input file\n" ;
        return EXIT_FAILURE ;
    }
    
    int seed ;
    if( file >> seed ) 
    {
        if( seed == 0 ) 
        {
            std::clog << "using curent time as seed " ;
            seed = std::time(nullptr) ;
        }
        std::cout << "seed: " << seed << '\n' ;
        std::srand(seed) ;

        double mb1; //mass body 1
        double xc1; //x coordinate 1
        double yc1; //y coordinate 1

        double mb2; //mass body 2
        double xc2; //x coordinate 2
        double yc2; //y coordinate 2

        if( file >> mb1 >> xc1 >> yc1 >> mb2 >> xc2 >> yc2 )
        {
            std::cout << std::fixed << "body 1: mass " <<  mb1 << "  location ( " << xc1 << ", " << yc1 << " )\n"
                                    << "body 2: mass " <<  mb2 << "  location ( " << xc2 << ", " << yc2 << " )\n" ;

            // ...
        }
        
        else // file >> mb1 >> xc1 >> yc1 >> mb2 >> xc2 >> yc2 failed
        {
            std::cerr << "badly formatted input on subsequent lines\n" ;
            return EXIT_FAILURE ;
        }
        
    }
        
    else // file >> seed failed
    {
        std::cerr << "badly formatted input on line one\n" ;
        return EXIT_FAILURE ;
    }
}

clang++ -std=c++14 -stdlib=libc++ -O2 -Wall -Wextra -pedantic-errors -Werror main.cpp -lsupc++ && ./a.out

seed: 562
body 1: mass 444223.123321  location ( 312233.312220, 3232323.311220 )
body 2: mass 332231.323223  location ( 333232.323230, 3232322.322330 )

http://coliru.stacked-crooked.com/a/94649a67b0f73397
Thank you so much! I am able to see what i did wrong. You were so helpful!
Topic archived. No new replies allowed.