943,352 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2348
  • C++ RSS
Feb 25th, 2005
1

I need help with this particular c++ problem

Expand Post »
Write a program which reads a text from a file declared by the user.
If the file does not exist a message should state this and the program
should be terminated.
If the file opens with success the program should read the text and then output the uppercase letters A-Z and each of their counts and same of the lowercase letters a-z.
The program will then print the percentages of capital letters for every letter A-Z and small letters for every letter a-z to the output file also declared the user.

This program's main function should be a bunch of function calls and declarations. and the information for the count function should be put into an array of structures, and the information should be passed as reference also the input and output files should be passed as reference.
Also there are to be no global variables.Please help me someone. thanks in advance.
Below is what I have done so far:


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. struct letters
  8. {
  9. char ch;
  10. int lcount;
  11. };
  12.  
  13.  
  14. void main()
  15. {
  16. void Explanation();
  17. int OpenFile(ifstream&, ofstream&);
  18. int Count(ifstream&, letters[], int);
  19. void PrintResult(letters[], int);
  20.  
  21. const int Size = 52;
  22. letters List;
  23.  
  24. ifstream infile;
  25. ofstream outfile;
  26.  
  27. Explanation();
  28. OpenFile(infile, outfile);
  29. Initialize(List, Size);
  30. Count(infile, List, Size);
  31. PrintResult(List, Size);
  32.  
  33. }
  34.  
  35. void Explanation()
  36. {
  37. cout << "This program reads a text from a file declared by you the user." << endl;
  38. cout << "If the file does not exist a message will state this and the program\n"
  39. << "will be terminated." << endl << endl;
  40. cout << "If the file opens with success the program will read the text and then\n"
  41. << "output the letters in that file, both upper and lower case and the count\n"
  42. << "of each letters appearance in the file." << endl << endl;
  43. cout << "The program will then print the total number as well as the percentages of\n"
  44. << "capital letters for every letter A-Z and small letters for every letter a-z\n"
  45. << "to the output file also declared by you the user." << endl << endl << endl;
  46. }
  47.  
  48. int OpenFile(ifstream& infile, ofstream& outfile)
  49. {
  50. string inputfile;
  51. string outputfile;
  52.  
  53. cout << "Please enter the name for the input file: ";
  54. cin >> inputfile;
  55. cout << endl;
  56.  
  57. infile.open(inputfile.c_str());
  58.  
  59. if (!infile)
  60. {
  61. cout << "Cannot open the input file!" << endl;
  62. return 1;
  63. }
  64.  
  65. cout << "Now enter the name for the output file: ";
  66. cin >> outputfile;
  67. cout << endl;
  68.  
  69. outfile.open(outputfile.c_str());
  70. }
Last edited by alc6379; Feb 28th, 2005 at 5:55 pm.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
chanz is offline Offline
2 posts
since Feb 2005
Feb 25th, 2005
1

Re: I need help with this particular c++ problem

ok had a chance to skim.... but void main?? main always returns a value...
int main(void)

or

int main()

not sure what the problems are could you state them? and please use code tags
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Feb 25th, 2005
0

Re: I need help with this particular c++ problem

Quote originally posted by Acidburn ...
ok had a chance to skim.... but void main?? main always returns a value...
int main(void)

or

int main()

not sure what the problems are could you state them? and please use code tags
void main is acceptable it is usually more common with programmers using MSVC based compilers (nmake).

also why are you declaring the file streams in main? You should jsut point the open file function the the file name where it declares and instanciates the file streams. If you need to count lines, do it in the Open function and return that value, then use that returned value in main.

anyways, just skimming through it.
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004
Feb 25th, 2005
0

Re: I need help with this particular c++ problem

>void main is acceptable
The language standard explicitly states that main must return an int. This has always been the case, except in the past the language has been imprecise enough for some to misinterpret it. Some compilers support void main, but only to cater to stupid people who don't know what language they're using.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 26th, 2005
0

Re: I need help with this particular c++ problem

read


http://homepages.tesco.net/~J.deBoyn...void-main.html

Unfortunetly there is a valid claim to using void main(). It's bad practice, but does; technically, follow the standard (the specific standard mentioned in that article).
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004
Feb 26th, 2005
1

Re: I need help with this particular c++ problem

Quote originally posted by BountyX ...
read


http://homepages.tesco.net/~J.deBoyn...void-main.html

Unfortunetly there is a valid claim to using void main(). It's bad practice, but does; technically, follow the standard (the specific standard mentioned in that article).
Been there, done that. I like this:

Quote ...
Even if your compiler accepts "void main()" avoid it, or risk being considered ignorant by C and C++ programmers.
"Hey, boss, the following code may or may not work -- it's behavior is unspecified*!"

*behavior where two or more possibilities exist and no further requirements on which is chosen in any instance


[edit]Follow some of the links and find that corrections have been made. Or that fun restrictions apply:
Quote ...
If you declare main or wmain as returning void, you cannot return an exit code to the parent process or operating system using a return statement; to return an exit code when main or wmain are declared as void, you must use the exit function.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 26th, 2005
0

Re: I need help with this particular c++ problem

i need your help.is it possible to give a solution to the following problem.
Store random numbers into two-dimensional array of 6 * 6 and interchange first 3 rows with last three rows.
i would be grateful to hear from you soon.
thank you in advance.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bill13 is offline Offline
2 posts
since Feb 2005
Feb 26th, 2005
0

Re: I need help with this particular c++ problem

Quote originally posted by bill13 ...
i need your help.is it possible to give a solution to the following problem.
Store random numbers into two-dimensional array of 6 * 6 and interchange first 3 rows with last three rows.
i would be grateful to hear from you soon.
thank you in advance.
Since this has nothing to do with this thread, it would have been better to have started a new one. Of course it is possible -- please post your initial attempt (and please use code tags).
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 26th, 2005
0

Re: I need help with this particular c++ problem

>does; technically, follow the standard
Technically, no. Technically, the standard allows for non-portable extensions, but doesn't specify those extensions in any way. Therefore, void main does not follow the standard. It only has the potential for implementation-defined behavior rather than undefined behavior if the implementation documents it.

Your argument could be applied to any language or library extension.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 27th, 2005
0

Re: I need help with this particular c++ problem

Messages have been restored, sorry for the inconvience!

I stand corrected on this issue
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: dynamic array of structures problem
Next Thread in C++ Forum Timeline: Building a database application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC