I'm at wits end here and I know that Sup Forums can be a bunch of fucking pricks about this, but I need help...

I'm at wits end here and I know that Sup Forums can be a bunch of fucking pricks about this, but I need help. I'm trying to read a text file and search through each word for the first letter and see if it's the letter 'd'. This is taken verbatim from some shit my professor made in class, I just filled in some blanks and here's where I'm at.
Can anyone help?

Other urls found in this thread:

cplusplus.com/forum/beginner/78150/
twitter.com/SFWRedditVideos

Just fix the weird bullshit bugs and implement the logic you just described. Your post is like "I have a fork and some food and I need to take the fork and put the food into my mouth, can anyone help?" TAKE THE FUCKING FORK AND STUFF YOUR FUCKING SHITFACE YOURSELF YOU RETARDED POS

>This is taken verbatim from some shit my professor made in class

Your professor didn't write that if/else statement and expected it to work.

(You)
Right, that's because I misunderstood the question at first but realized I fucked it. At this point, I understand the idea: I need to traverse the file and check the letter the program is at and the letter behind it to see if it's a space and then a 'd'. I have no clue how to implement that with the char datatype and I've been searching around for the past hour and can't find anything helpful.

If you can't figure it out on your own, with the wealth of information that the internet affords you, give up now.

ask on reddit or stackoverflow. Sup Forums is full of retards.

So you need to count occurrences of a space (' ') followed by d ('d')?

You can loop over the characters and keep track of the previous character in another variable. Your if/else statement is likely wrong because the else if block will never get executed if ch < 'a'. The way you check if something is upper case is kind of strange too, but it technically works, do you understand exactly why it works?

Stack overflow either refuses to help becuase it's homework and the CS majors always make it a point not to help or to help in a way that requires just as much knowledge as they have. I've had bad experiences with them. Also fuck reddit.
I do understand why it works, but you see the program actually executes and will count every single letter 'd' in a file (I give it dwords.text in the same directory as the project), except that's obviously not what is needed. I can start from the ground up with the loop.

Because you only check the currently read out character ch, you should also check the value of the previous character. Right now, that value isn't available in your loop, so you need to make that available first by writing code to keep track of the character from the previous loop iteration.

split to make an array, then a loop and word[0]

Okay, I'm working on that now. I don't understand how to store something from the last iteration of a loop, though. The way it's always been explained to me is that the loop will well...loop independently from the rest of the program for its duration and you really can't do anything outside of it for that duration.
Alright, how would I go about taking a file in as an array?

cplusplus.com/forum/beginner/78150/

So, this checks all letters, rather than the first letter of each word. You'll have to split the file into "words" and store them in an array. Since strings are just an array of characters, you'll be able to check the first letter of each in one loop after storing them by referencing them like

if (words[i][0] == 'd')
{
count++;
}

In python you can create lists with the .split() method and it will automatically split on whitespace. Perhaps you can build your own split function, or you can build it into the loop.

To check against the previous iteration:

array[i] == array[i - 1]

Ensure you don't do this if i == 0 or you're out of bounds.

Hm, thank you for the suggestions. As an update, I've emailed my professor to see if he'll answer me last minute, probably not; I might even get some shitty passive aggressive response.
I'll try implementing the array and see if I can do that right.


Tits for your time.

Ah, quick question, what the hell is this std:: shit I keep seeing? We never covered that and I see it everywhere.

cpp arrays are different from the other languages I've used in that you need to assign pointers afaik. You'll need to size the array initially, so you could get a character count, store it all as a giant string, then split it on space, tab, return, etc into an array of strings. I'm not familiar with cpp or your program requirements (is it restricted to standard libraries, etc), so there may be an easier way if you can use nonstandard libraries.

Namespace. It's a reference to where your program looks for libraries in your include statements.

No restrictions that I'm aware of, other than the stupid shit that people generally don't use, like "break" except in certain conditions. Here's the assignment from the book, verbatim:

>Query the user for the name of a file. Open and process that file, reporting the number of words that begin with the letter d, irrespective of case. The input file for testing this assignment is posted.

Ah, thank you.

I see what you want now that I fully read the thread.

if (array[i - 1] == ' ')
{
if (array[i] == 'd')
{
dCount++;
}
}

That's not going to account for return, tab, or any other whitespace character, though.

And again, ensure that you keep the array in bounds by not checking i-1 if i == 0. You can do this while reading the file in, instead of storing it all in an array, but might encounter errors if you don't process the file first. I like splitting the file into words because it enables you to do more with it later on, should later assignments use previous ones and build upon them, as cs projects usually do.