【问题标题】:Cannot get Array to print with extra spaces in front. Only the first value will print with spaces无法让 Array 打印前面有多余的空格。只有第一个值会用空格打印
【发布时间】:2013-12-05 20:55:59
【问题描述】:

我需要做什么才能让我的数组值在前面打印一个制表符或空格?

#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
*/

【问题讨论】:

    标签: c++ arrays string


    【解决方案1】:

    这可能是自动换行的结果,因为没有换行符。只需在行尾添加endl

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

    编辑:

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

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

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

    【讨论】:

    • 我试过了。这会导致它在每个输出之间放置一个空行,并且它仍然只将空格添加到第一个输出。
    • 这提供了一个线索——某处有一个额外的换行符。也许是city[i]的第一个字符?由于加德满都在输入文件中排在第一位,因此它前面没有换行符,因此它不会显示在输出中。
    • 是的,好像在比较字符串的时候,如果字符串不匹配Katmandu的一部分,它会添加空行,但是如果它在Katmandu之内,那么它不会跳到下一行。不过,它仍然只将空间放在加德满都前面,而不是在每个城市前面。
    • 空格在那里 - 它们是您看到的空白行。 city[i] 的开头或附近有一个换行符,导致它在下一行自行打印。
    • 这一行 inFile.getline(buff1, ARRAY_SIZE, FIELD_DELIM_CHAR); 导致您将换行符读入数据中。
    猜你喜欢
    • 2014-04-17
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 2011-05-28
    • 2018-02-25
    • 2014-11-15
    • 2023-01-31
    • 2016-07-06
    相关资源
    最近更新 更多