I need help with this particular c++ problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 2
Reputation: chanz is an unknown quantity at this point 
Solved Threads: 0
chanz's Avatar
chanz chanz is offline Offline
Newbie Poster

I need help with this particular c++ problem

 
1
  #1
Feb 25th, 2005
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:


  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

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

 
1
  #2
Feb 25th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 8
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

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

 
0
  #3
Feb 25th, 2005
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.
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,856
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

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

 
0
  #4
Feb 25th, 2005
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 8
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

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

 
0
  #5
Feb 26th, 2005
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).
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
1
  #6
Feb 26th, 2005
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:

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:
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 2
Reputation: bill13 is an unknown quantity at this point 
Solved Threads: 0
bill13 bill13 is offline Offline
Newbie Poster

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

 
0
  #7
Feb 26th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #8
Feb 26th, 2005
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).
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,856
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

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

 
0
  #9
Feb 26th, 2005
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 8
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

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

 
0
  #10
Feb 27th, 2005
Messages have been restored, sorry for the inconvience!

I stand corrected on this issue
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 2072 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC