Hello residentevil35,
For awhile I have dumbfounded by what you can and can not learn and what you can not use. And I have perplexed with what to say about it. Since I am not in the class I do not think it is my place to talk about something I do not know enough about.
Anyhow
zapshe has a good solution that works, but here is a different solution. Also part of programming is to be able to take something like what
zapshe has presented an change it to what you need. Sometimes parts of code are left out to give you something to work on.
This is similar to what
zapshe has presented with some changes that you should fine usful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#include <cctype> // <--- std::tolower() and std::toupper().
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
//using namespace std; // <--- Best not to use.
int main()
{
const std::string PATH{ "C:\\data\\" };
const std::string inFileName{ "namesLast.txt" };
const std::string outFileName{ "Results.txt" };
std::string temp1, temp2, temp3, temp4;
// <--- What you would use.
//std::ifstream inFile(PATH + inFileName); //declaring the first file to open
//std::ofstream outFile(PATH + outFileName);
// <--- What I use.
std::ifstream inFile(inFileName); //declaring the first file to open
std::ofstream outFile(outFileName);
//inFile.open(PATH + "namesLast.txt"); // <--- Can be used, but the above is better.
if (!inFile)
{
std::cout << "\n File " << std::quoted(inFileName) << " did not open" << std::endl;
//std::cout << "\n File \"" << inFileName << "\" did not open" << std::endl;
//std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread". This line is optional.
return 1; //exit(1); // If not in "main".
}
if (!outFile)
{
std::cout << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;
//std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread". This line is optional.
return 2; //exit(2); // If not in "main".
}
std::string name;
int count{}, badLines{};
std::cout << '\n';
while (inFile >> temp1 >> temp2 >> temp3 >> temp4)
{
// <--- This should be in the while condition, but if used would require more work to break it up.
//std::getline(inFile, name, '\n'); // <--- The third parameter is not necessary as the "\n" is the default.
if (temp1 == "lastname" || temp1 == "surname")
{
temp2[0] = std::toupper(temp2[0]);
temp3[0] = std::toupper(temp3[0]);
temp4[0] = std::toupper(temp4[0]);
//if (temp2[0] >= 'a' && temp2[0] <= 'z')
// temp2[0] -= 32;
//if (temp3[0] >= 'a' && temp3[0] <= 'z')
// temp3[0] -= 32;
//if (temp4[0] >= 'a' && temp4[0] <= 'z')
// temp4[0] -= 32;
name = temp2 + ", " + temp3 + ' ' + temp4;
}
else if (temp3 == "lastname" || temp3 == "surname")
{
temp1[0] = std::toupper(temp1[0]);
temp2[0] = std::toupper(temp2[0]);
temp4[0] = std::toupper(temp4[0]);
name = temp4 + ", " + temp1 + ' ' + temp2;
}
else
{
std::cout << "\n Improper format. Please check the input file!" << std::endl;
std::cout << " " << temp1 << ' ' << temp2 << ' ' << temp3 << ' ' << temp4 << '\n' << std::endl;
outFile << "\n Improper format. Please check the input file!" << std::endl;
outFile << " " << temp1 << ' ' << temp2 << ' ' << temp3 << ' ' << temp4 << '\n' << std::endl;
badLines++;
continue;
}
std::cout << ' ' << name << std::endl;
outFile << name << std::endl;
count++;
}
std::cout << "\n\n Processing finished.\n\n There were " << count << " lines in the input file.\n And "
<< badLines << " inproperly formatted line(s) in the file." << std::endl;
inFile.close();
outFile.close();
return 0;
}
|
To address what you said "<cctype>" and "<ctype.h>" are the same thing. The "ctype" is the C++ header file that should be used. Including both does not help or hinder you.
In your example:
lastname(0)=toupper(lastname(0));)
When accessing an array the []s are most often used although the ()s may work, but I am not sure about that part.
You are correct
std::tolower();
and
std::toupper();
only work on a single character. That is why you use
lastname[0]
to access the first element of the array and only work with one character.
At the beginning the first section of header files I do this way because the tend to be header files that are common to any program. By accident I found the putting them in alphabetical order helps you to remember and realize if you miss one.
The second section would be any header files that the given program would need. i.e., any extra header files.
The third section, if it was here, would be any header files that you would write, those that ate contained in double quotes. Although the order of the header files should not make a difference, sometimes it does.
I think you should understand the constant variables I defined.
For dealing with opening the file
inFile.open(PATH + "namesLast.txt");
this works, but you can do the same thing with
std::ifstream inFile(PATH + inFileName);
. This not only sets up "inFile" as the handle to the file stream, but also opens the file at the same time.
The two if statements I use to check that the file stream is open and ready to use.
In the first if statement I have an alternative if you can not use "std::quoted()" from the "iomanip" header file. Change the comments as needed. Also you may have to change the if statement for the output if statement.
For an input file stream I would say it is mandatory that you check the stream status. With an output file stream it is more 50/50. If an output file name does not exist it will be created before it is written to, so not usually a problem. But when you use a path this can create a problem and can cause the output stream to fail. If you fail to check this the program will continue, but be unable to write to the file leaving you wondering why your file is empty.
I did the while statement a bit different. By reading all four at one time you can check what you need and arrange the variables in the proper order.
zapshe's method will work if you need to read one word at a time. Although I feel that the use of "+=" for everything is not necessary. Just as the "+=" is overloaded for the string class so too is the "+" operator overloaded to concatenate the string.
If you want some more input have a look at
http://www.cplusplus.com/reference/string/string/ This will list all the member functions plus extras that are availavle to the string class.
I think that between the two programs you should be able to come up with something that will work.
In the future make mention that this is homework or a lab assignment along with what you can and can not use in the program. It is also helpful to include the directions that you were given for th program so that no one will have to guess at what the program needs to do. You did include the inpput file, but do not forget this in the future. And when you get to the point where you are creating your own header files do not forget to include them when you post your code. Unless it is a big program it is always better to post a program and all the files that can be compiled and run to test the program. Leaving something out just delays an answer.
Hope that helps,
Andy