【发布时间】: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
*/
【问题讨论】: