无法在前面有多余空格的情况下获取要打印的数组.只有第一个值会用空格打印



我需要做什么才能让数组值打印出前面有一个选项卡或空格?

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <iomanip>
using namespace std;
const char FIELD_DELIM_CHAR = ',';
const char LINE_DELIM_CHAR  = 'n';
const int ARRAY_SIZE = 1000;
int fileLen;
string city[ARRAY_SIZE];
char buff1[ARRAY_SIZE];
double lowTemp[ARRAY_SIZE];
double highTemp[ARRAY_SIZE];
int LoadData();
void ShowAll();
int ShowCities(string city[], string str);
void ConvertStringToLowerCase(const string orig, string& lwr);
int i, count;
string filePath;
ifstream inFile;

这是我的主程序

void main()
{
    cout << "Welcome to Josh's Library Database. " << endl << endl;
    char action;
    string str;
    int data;

我的问题是S和F的输出结果。我得到了正确的输出值,但无法使它们被选项卡覆盖或在开始的中包含额外的空格

    do
    {
        cout << "(L)oad File,  (S)how All,  (F)ind City, (Q)uit:";
        cin >> action;
        action = tolower(action);
        if(action == 'l')
        {
            cout << "Please enter the name of the temperatures file: ";
            cin >> filePath;
            data = LoadData();
            if(data <= 0) // Prompt user to retry if filepath entered has no values
            {
                cout << "Error opening file!  Please try again! " << endl << endl;
            }
            else
            {
                cout << data << " record(s) found." << endl << endl;
            }
        }
        else if(action == 's')
        {
            ShowAll();
        }
        else if(action == 'f')
        {
            cout << ShowCities(city, str) << " record(s) found. " << endl << endl;
        }
        else if(action == 'q')
        {
            break;
        }
        else
        {
            cout << "Invalid Response! Please try again! " << endl << endl;
        }
    }while(action != 'q');  
}
int LoadData()
{
    char chr;
    int i = 0;
    // Open the file
    inFile.open(filePath);
    while (!inFile.eof())
    {
        buff1[0] = 0;
        // Get the next field from the file
        inFile.getline(buff1, ARRAY_SIZE, FIELD_DELIM_CHAR);
        // If the array is empty, exit the loop
        if (buff1[0] == 0)
        {
            return -1;
        }
        else if(inFile.eof())
        {
            return i;
        }
        else
        {
            // Convert 'buff' char values into a string
            string s(buff1);
            city[i] = s;
            // Collect low temp value, ignore comma, collect high temp value
            inFile >> lowTemp[i];
            inFile.get(chr);
            inFile >> highTemp[i];
        }
        i++;
    } 
    if (inFile.is_open())
    {   //  Close the file in such a way that it can be reopened
        inFile.close();
        inFile.clear(std::ios_base::goodbit);
    } 
    return i; // Return total number of cities listed
}

这就是我遇到很多问题的地方,输出只将额外的空间添加到列出的第一个城市,而没有添加到任何其他

// Loop to display all data in files
void ShowAll()
{
    for(int i = 0; city[i] != ""; i++)
    {
        cout << "   " << city[i] << " (" << lowTemp[i] << ", " << highTemp[i] << ")";
    }
    cout << endl << endl;
}

当我选择这个选项时,输出不包括额外的空格,它会在输出第一个城市之前下降到下一行,这是不应该的。

int ShowCities(string city[], string str)
{
    string lwr1, lwr2;
    int idx, count = 0;
    cout << "City: ";
    cin >> str;
    // Convert a copy of the search string to lower case
    ConvertStringToLowerCase(str, lwr1);
    for (int i = 0; city[i] != ""; i++)
    {
        // Convert a copy of the array string to lower case
        ConvertStringToLowerCase(city[i], lwr2);
        idx = lwr2.find(lwr1);
        if (idx > -1)
        {
            cout << "   " << city[i] << " (" << lowTemp[i] << ", " << highTemp[i] << ")";
            count++;
        }
    }
    cout << endl;
    return count; // Return total number of cities with matching strings
}
void ConvertStringToLowerCase(const string orig, string& lwr)
{
    lwr = orig;
    for (int j = 0; j < orig.length(); ++j)
    {
        lwr[j] = tolower(orig.at(j));
    }
}

/*
*/ This is the data from the inFile.
Katmandu,       -34,  28
Perth,           92,  105
Chicago,         22,  45
St. Petersburg,  19,  37
Miami,           68,  84
Cincinnati,      21,  39
Bern,            45,  55
Johannesburg,    87,  102
Seattle,         34,  42
New York,        24,  39
St. Louis,       12,  23
Beijing,         45,  66
Munich,          31,  38
Zurich,          33,  41
Buenos Aires,    69,  84

Output for ShowAll() is: Only the first output includes spaces.
   Katmandu,       -34,  28
Perth,           92,  105
Chicago,         22,  45
St. Petersburg,  19,  37
Miami,           68,  84
Cincinnati,      21,  39
Bern,            45,  55
Johannesburg,    87,  102
Seattle,         34,  42
New York,        24,  39
St. Louis,       12,  23
Beijing,         45,  66
Munich,          31,  38
Zurich,          33,  41
Buenos Aires,    69,  84
*/

这可能是换行的结果,因为没有换行。只需在行的末尾添加一个endl

cout << "   " << city[i] << " (" << lowTemp[i] << ", " << highTemp[i] << ")" << endl;

编辑:

const char FIELD_DELIM_CHAR = ',';
...
inFile.getline(buff1, ARRAY_SIZE, FIELD_DELIM_CHAR);

这会导致您的代码读取城市前面的换行符。因此,当您输出city[i]时,它前面有一个换行符,导致它显示在换行符的开头。

您需要在读取数据时考虑到这一点,或者在读取数据后修剪掉多余的空白。

最新更新