试图打印出阵列但是在打印之前它正在进入一个新行

时间:2016-12-13 21:01:33

标签: c++ arrays output

我感兴趣的是为什么输出后面的数字的输出不在同一行,除了第一次出现任何解决方案或推理都会非常感激。 有没有办法防止getline读取/ n? 如果是这样的话?

#include<iostream>
#include <fstream> 
#include <string> 
#include <vector>
#include<sstream>
using namespace std;

const int NCOLS = 4;
const int NROWS = 10;

//void description_and_options(string data[][NCOLS], int count[NCOLS]);

int main()
{
    ifstream file_name;//create the new file
    string user_input_file;//the files name inputed by the user 
    string starting_array[8];
    int stringlength;
    string read_in_array[NROWS][NCOLS];
    string line;
    int counter = 1;

    cout << "Enter the name of the input file: ";

    cin >> user_input_file;



    if (user_input_file.length() > 4)// check to see if its more than 4 in length
    {
        stringlength = user_input_file.length(); //saves length
        if (user_input_file.substr(stringlength - 4, 4) == ".txt")//checks to see if its .dat
        {
            file_name.open(user_input_file.c_str());
            if (file_name.fail())
            {
                cerr << "The file " << user_input_file << " failed to open.\n";//tells user if it fails
                exit(1);
            }
        }
    }
    else
    {
        user_input_file += ".txt";//adds .dat to non .dat 
        file_name.open(user_input_file.c_str());
    }

    if (file_name.fail())
    {
        cout << "File failed to open" << endl;
        system("PAUSE");
        exit(1);
    }


    //for (int row = 0; row <=9; row++)
    for (int row = 0; row <= 9; row++)
    {
        for (int col = 0; col < 4; col++)
        {
            if (getline(file_name, line, ';'))
            {
                read_in_array[row][col] = line;
                //  cout << read_in_array[row][col];
            }
        }
        //cout << counter[row]<<read_in_array[row][0]<<endl;

    }
    //[updown][leftright]
    file_name.close();
    for (int lcv = 0; lcv < 9; lcv++)
    {
        cout << counter<< "   "<< read_in_array[lcv][0] << endl;
        counter++;
    }
    cout << endl;
    //  description_and_options(read_in_array, counter);
    system("PAUSE");//pause
}

输出应该是

的行
1   Google (on one line)
2   Deviantart(on the next line)
3   Dragcave.net(and so on)
etc...

,数据文件是

Google;KyleSmith01@gmail.com;Kyleman27;security question:White rabbit with a watch;
Deviantart;Dragonmaster27;Gandalfthegrey; NULL;
Dragcave.net;Dragonmaster27; DragonM27; Notes: username shortend; 
Youtube.com;DragonMaster207; DragonM207; Notes: 207 not 27;
Facebook.com; KyleSmith27; KsmithFB; NULL;  
Twitter.com; KyleSmith207; KsmithT; NULL;
Blogger; Kylesmith207; KyleSmith27; Notes: password 27 not 207;
Yahoo.com; KyleSmith.01@gmail.com; kSmith08; yahoo has a . before 01;
Jibjab.com;Kyle.207; KyleSmit.2.7; .2.7;
Dragonworld.com;KyleDragonMaster;DragonMkyle;;

1 个答案:

答案 0 :(得分:0)

getline看到:...rabbit with a watch;\nDeviantart;...,然后下一次阅读获得\nDeviantart

您可以使用两个getline循环;一个读取行,第二个读取每个值,直到;

#include <iostream>
#include <string> 
#include <vector>
#include <sstream>
#include <iomanip>

extern const char* file_data; 

int main()
{
    std::istringstream is{file_data}; //could replace with ifstream
    std::vector<std::vector<std::string>> read_in_array{};
    for(std::string line{}; std::getline(is, line);) {
        std::istringstream line_is{line};
        std::vector<std::string> row{};
        for(std::string value{}; std::getline(line_is, value, ';');) {
            row.push_back(value);
        }
        read_in_array.push_back(row);
    }

    size_t counter{0};
    for(const auto& row : read_in_array) {
        std::cout << counter++ << "   "; 
            for(const auto& val : row) {
                std::cout << std::setw(25) << val;
        }
            std::cout << '\n';
    }
}

const char* file_data =
R"(Google;KyleSmith01@gmail.com;Kyleman27;security question:White rabbit with a watch;
Deviantart;Dragonmaster27;Gandalfthegrey; NULL;
Dragcave.net;Dragonmaster27; DragonM27; Notes: username shortend; 
Youtube.com;DragonMaster207; DragonM207; Notes: 207 not 27;
Facebook.com; KyleSmith27; KsmithFB; NULL;  
Twitter.com; KyleSmith207; KsmithT; NULL;
Blogger; Kylesmith207; KyleSmith27; Notes: password 27 not 207;
Yahoo.com; KyleSmith.01@gmail.com; kSmith08; yahoo has a . before 01;
Jibjab.com;Kyle.207; KyleSmit.2.7; .2.7;
Dragonworld.com;KyleDragonMaster;DragonMkyle;;)";

产地:

0                      Google    KyleSmith01@gmail.com                Kyleman27security question:White rabbit with a watch
1                  Deviantart           Dragonmaster27           Gandalfthegrey                     NULL
2                Dragcave.net           Dragonmaster27                DragonM27 Notes: username shortend
3                 Youtube.com          DragonMaster207               DragonM207        Notes: 207 not 27
4                Facebook.com              KyleSmith27                 KsmithFB                     NULL
5                 Twitter.com             KyleSmith207                  KsmithT                     NULL
6                     Blogger             Kylesmith207              KyleSmith27 Notes: password 27 not 207
7                   Yahoo.com   KyleSmith.01@gmail.com                 kSmith08  yahoo has a . before 01
8                  Jibjab.com                 Kyle.207             KyleSmit.2.7                     .2.7
9             Dragonworld.com         KyleDragonMaster              DragonMkyle