Hello nypran,
I was getting no answer and wanted to start fresh, that's why I erased.
I did not know it was forbidden. |
In that case reply to your own post with "bump". This will put the post at the top of the list.
Your ".dat" file can be opened in "Notepad". It can also be opened in VS.
I am not sure if it is actually forbidden to delete the beginning of a post, but it is bad form. It makes the replies hard to understand especially for someone who comes in late.
nuderobmonkey has one approach. Here is a different approach:
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
|
#include <iostream>
#include <string>
#include<fstream>
#include <chrono>
#include <thread>
//using namespace std; // <--- Best not to use.
int main()
{
int uppercase = 0;
int lowercase = 0;
char data;
const std::string inFileName{ "abcfile.dat" };
const std::string outFileNameLow{ "LowerCase.txt" };
const std::string outFileNameUp{ "UpperCase.txt" };
std::ifstream inFile(inFileName);
if (!inFile)
{
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".
return 1; //exit(1); // If not in "main".
}
std::ofstream outFileLow(outFileNameLow);
if (!outFileLow)
{
std::cout << "\n File \"" << outFileNameLow << "\" did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
return 2; //exit(1); // If not in "main".
}
std::ofstream outFileUp(outFileNameUp);
if (!outFileUp)
{
std::cout << "\n File " << outFileNameUp << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
return 3; //exit(1); // If not in "main".
}
while (inFile.get(data))
{
if ((data >= 'A') && (data <= 'Z'))
{
uppercase++;
std::cout << data << std::endl;
outFileUp << data << ' ';
}
else if ((data >= 'a') && (data <= 'z'))
{
lowercase++;
std::cout << data << std::endl;
outFileLow << data << ' ';
}
}
std::cout << "\nNumber of uppercase letters " << uppercase << std::endl;
std::cout << "Number of lowercase letters " << lowercase << std::endl;
inFile.close();
outFileLow.close();
outFileUp.close();
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}
|
You will need to include the header file "string". The header files "chrono" and "thread" are optional, will explain shortly.
I like using the strings for the file names because I can use them to open the files and use them in the if statements.
When opening a file for input it is not only a good idea to check if it opened it is a must. If the file did not open and the program continues nothing will be read.
In contrast if an output file name does not exist it will be created. It is not impossible for an output file to fail to open, but it could happen. Checking that an output file is open is just a good practice.
In the if statements lines 24, 34 and 44 are optional. I use this in my VS to keep the console window open long enough to read the message before the window closes. You may not need or want this.
Since this is all contained inside "main" the return statement is best to use. "return 0;" is a normal exit of the program. Any number greater than zero is considered a problem exit. By using different numbers, (1, 2, 3) you can use this to help find where the problem is. As the comment says if you use this if statement outside "main" you will need to use the "exit()". The different numbers greater than zero apply the same.
One single while loop can accomplish everything that you need with the least amount of work and coding.
The lines above the "return" are something I use and may not be needed by you. Keep them if you want or delete them. The use is similar to line 24.
Although it is not necessary to end the program with
return 0;
it is good form and also a good place for a break point when debugging the program.
Hope that helps,
Andy