New to c++ Need Help!

Hello everyone names Matt I'm new to the programming world, I have some assignments that im starting in my college class and would need some assistance getting them done.

I am using the Xcode for Mac so im also learning how everything is done. I had a bit of exercise on a pc and it seemed to be easier when debugging i was able to input examples into the black debugging screen I'm not sure if its called the command screen or not.

Here is my question that I'm working on

Write a program that opens a file and counts the whitespace-separated words in that file.?

My code that i wrote for this example is as follows...

#include <iostream>

using namespace std;
#include <iostream>
#include <string>
#include <fstream>

int main()
string filename = "Question 1.cpp"; // File name goes in here
ifstream file(filename);
int wordcount = 0;
string word;

while (file >> word)

cout << "The file \"" << filename << "\" has " << wordcount << " words." << endl;

Is this correct or am i missing something?

Cheers everyone
Last edited on
It is the compiler that says whether your code is correct.
Compiler says

The file "Question 1.cpp" has 0 words.

I was much used to using visual studio where i would compile and the window would ask for me to input a text or numbers etc... so then depending on my code it would execute the program based on my input parameters.

Can i run a window that prompts me in Xcode to input for example a string name and then it executes to count the word spaces
Never mind i seemed to understand now i just began using this today so i figured out how the debugger works. :) Gotta keep trying until u get something!!

Now my question is am i answering this question properly that is being asked of me for the assignment.?
The reason that there is 0 words is probably that the file was not opened successfully.
i tested something else and it worked fine ie a example asking the user to input his age...

Im wondering if its due to the fact i have

string filename = "Question 1.cpp";

can that cause my output to display zero words.?
I already pointed out the possible reason of the result.
Topic archived. No new replies allowed.