CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2020
    Posts
    2

    What is the C++ Implementation for This?

    I have a Windows text file with the following info inside it:

    25 05 38 26 53 04
    07 45 50 33 19 34
    55 25 21 30 09 39
    26 11 30 12 13 41
    32 23 44 11 50 39
    45 30 07 44 55 54
    21 10 35 46 48 27
    52 41 05 53 11 50
    40 38 17 43 10 54
    45 27 29 12 39 31
    24 42 38 02 18 09
    13 43 28 06 53 30
    45 47 29 30 53 13
    38 45 28 48 47 36
    25 34 18 06 07 55


    How can I code them to break that info apart into six columns and put each column into their own array? (Like in the image below...)

    Name:  Untitled.jpg
Views: 1402
Size:  66.7 KB

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: What is the C++ Implementation for This?

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: What is the C++ Implementation for This?

    Possibly:

    Code:
    #include <fstream>
    #include <iostream>
    #include <vector>
    #include <iomanip>
    
    int main()
    {
    	std::ifstream ifs("numbers.txt");
    
    	if (!ifs)
    		return (std::cout << "Cannot open file\n"), 1;
    
    	std::vector<int> cols[6];
    
    	while (ifs)
    		for (size_t i = 0; i < 6; ++i) {
    			int n;
    
    			if (ifs >> n)
    				cols[i].push_back(n);
    		}
    
    
    	const size_t noel {cols[0].size()};
    
    	for (size_t c = 1; c <= 6; ++c)
    		std::cout << "[array" << c << "]  ";
    
    	std::cout << '\n';
    
    	for (size_t r = 0; r < noel; ++r) {
    		for (size_t c = 0; c < 6; ++c)
    			std::cout << "   " << std::setw(2) << std::setfill('0') << cols[c][r] << "     ";
    
    		std::cout << '\n';
    	}
    }
    Code:
    [array1]  [array2]  [array3]  [array4]  [array5]  [array6]
       25        05        38        26        53        04
       07        45        50        33        19        34
       55        25        21        30        09        39
       26        11        30        12        13        41
       32        23        44        11        50        39
       45        30        07        44        55        54
       21        10        35        46        48        27
       52        41        05        53        11        50
       40        38        17        43        10        54
       45        27        29        12        39        31
       24        42        38        02        18        09
       13        43        28        06        53        30
       45        47        29        30        53        13
       38        45        28        48        47        36
       25        34        18        06        07        55
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured