-
This code wont work
Someone please tell me what is wrong with this code:
Code:
#include <fstream>
#include <iostream>
#include <cstdlib>
int main ()
{
using namespace std;
char in_file_name[16], out_file_name[16], next;
int ws, letters;
ifstream fin;
ofstream fout;
cout << "I will find the total number of occurrences of characters in the file "
<< "total number of nonwhitespace characters, and the total number of letters "
<< "in a file\n";
cout << "Enter the input file name (maximum of 15 characters) :\n";
cin >> in_file_name;
cout << "Enter the output file name (maximum of 15 characters) :\n";
cin >> out_file_name;
fin.open(in_file_name);
if (fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
fout.open(out_file_name);
if (fout.fail())
{
cout << "Output file opening failed.\n";
exit(1);
}
fin.get(next);
while (! fin.eof())
{
if (isspace(next))
ws ++;
fin.get(next);
}
fin.get(next);
while (! fin.eof())
{
if (isalpha(next))
letters ++;
fin.get(next);
}
fin.close();
fout.close();
cout << "The total number of whitespaces was " << ws <<endl;
cout << "The total number of letters was " << letters << endl;
cout << "Thank you for using this program\n";
return 0;
}
Code tags added by Kermi3
-
It isn't enclosed in code tags.
-
Code Tags
In the furture please use Code Tags. People will be much more likely to help you if you do. And they'll be happy about it ;)
Information on code tags may be found at the link on my signature.
-
>Someone please tell me what is wrong with this code:
Your compiler can tell you what is wrong with your code. Did it ? What do you want your code to do, what does it do instead ? Does it compile, does it link ? You are presenting us a lot of work we have to do to help you. You already did this work and don't share. Not funny. Give us information.
>Someone please tell me what is wrong with this code:
Without the information I'd say it doesn't let me switch into first person view and hunt down some jap planes.
Edit:
Just noticed that this was your very first post... sorry for being sarcastic, but please post more info, because you already have it and we could really use it to help you.
-
The code looks fine... except for 2 small bugs
[list=1][*]Always initialize your variables
Code:
int ws = 0, letters = 0;
[*]You count the number of whitespaces and when you're at the end of your input file you start counting the number of letters.
What you need to do is check for whitespace AND letter in the same while loop:
Code:
while (! fin.eof())
{
if (isspace(next))
ws ++;
else if (isalpha(next))
letters ++;
fin.get(next);
}
[/list=1]