You are viewing our Forum Archives. To view or take place in current topics click here.
Any C++ programmers?
Posted:

Any C++ programmers?Posted:

APG
  • Christmas!
Status: Offline
Joined: Jan 27, 201014Year Member
Posts: 1,177
Reputation Power: 46
Status: Offline
Joined: Jan 27, 201014Year Member
Posts: 1,177
Reputation Power: 46
Im in my first year of college and I took a programming class in C++. I've been completely fine up until now. We are writing a program that prompts the user to input the name of a text file and then outputs the number of words in the file. I have ideas on where to start but the project is due to soon for me to not know where to go with everything. If anyone can help it'd be greatly appreciated I can also paypal money to anyone who helps as I couldn't see a tutor the past 2 weeks due to work.

Heres the prompt:

Write a program that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a word to be any text that is surrounded by whitespace (for example, a space, carriage return, newline) or borders the beginning or end of the file.
Input Notes:There are two input sources in this program : the text file and standard input. The standard input consists of a single word, the name of the text file to be read.

Output Notes (Prompts and Labels): The filename provided in standard input is prompted for with "Enter a file name : " and displayed so that the filename entered is on the same line as the prompt.

The output of the program is a single line of the form: "The file contains N words." where N is the number of words determined by the program .
#2. Posted:
Ant
  • Retired Staff
Status: Offline
Joined: Jun 12, 200915Year Member
Posts: 8,515
Reputation Power: 520
Status: Offline
Joined: Jun 12, 200915Year Member
Posts: 8,515
Reputation Power: 520
Ok this is a pretty neat little program you can make in 30 lines or less.

1. Make yourself a C++ Win32 Console Application, make sure it's an empty project. (this is assuming you are using Visual Studio by the way)

2. Add a .cpp file to the project, named whatever.

3. I used 3 includes as followed (assuming you know what they are and what they do):

#include <fstream>
#include <iostream>
#include <string>

4. If your Professor is insistent on it, add the namespace std however I view this as bad practice in C++. Not having "using namespace std;" in your code just means that you need to add std:: before pieces of code such as making a new variable "std::string s;"

using namespace std;

5. Create yourself a main method, just call it "int main()" since we don't need to give it any parameters.

6. We need 4 variables for this program. An ifstream for reading in the file, 2 strings (one for the file name and another for words) and an int to act as a counter.

7. Write out a prompt to the user asking them to input a file name.

8. Take the user input and put it into the file name string we declared earlier.

9. Open the file using the ifstream and the file name.

10. Have a while loop that repeats itself until the end of the file. Add the content of the ifstream to the word string and increment the counter. This is all you need in this loop.

11. Output to the console the amount of words the file contains (you have the format in your spec).

12. Close the ifstream.

13. Even though I hate it when people do this. Use something like std::cin.get(); or system("PAUSE"); so that you can see the console when you compile.

14. Return 0 at the bottom of the main method.

[ Register or Signin to view external links. ]

Remember to put the text file in the root directory (the same place as the project and cpp files etc.) and to use ".txt" when inputting the file name (something you should remind the user to do).

I didn't want to explicitly give you the code but if you're having any issues, just send me a PM and I can help you further.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.