The task is to write a program, the user enters a line, at the output the program shows how many words in the line begin with the letter "t", the counter does not work, does not output anything. Tell me, please, where I made a mistake.

#include <iostream> #include <string.h> using namespace std; int main() { int c=0; char a[100] = {0}; cout << "Vvedite stroky: "; cin.getline(a,100); if (a[100] == 't') { c++; for (int i = 0; i < strlen(a); i++) { if ((a[i] = ' ') && (a[i + 1] == 't')) { c++; } } cout << c; } system("pause"); return 0; } 
  • In the debugger already watched? - Vladimir Martyanov
  • @ Vladimir Martianov looked, it shows everything is fine, or I looked crookedly .. - Mr.Flatman
  • one
    if (a [100] == 't') {- what are you checking? And how did you manage to pass this check in the debugger? - Vladimir Martyanov
  • @ Vladimir Martianov, oops, I am sorry, it was crooked, it should be if (a [0] == 't') now it remains to figure out why he considers all the letters t - Mr.Flatman
  • one
    And look again at the debugger - Vladimir Martyanov

2 answers 2

You have a whole line filled with spaces.

 if ((a[i] = ' ') && (a[i + 1] == 't')) { 

Need to

 if ((a[i] == ' ') && (a[i + 1] == 't')) { 

    Suppose you have already corrected a blemish with a[100] and now you have

     if (a[0] == 't') { c++; for (int i = 0; i < strlen(a); i++) { 

    And what happens if your first character is not t ? your program does nothing in this case.

    You will carry out a cycle from a body if ...

    The fact that in the loop you spoil the line, instead of comparing it with a space, you have already written.

    And, of course, it’s better not to jump out of the line :), although in your particular case nothing terrible happens - it’s just pointless to watch the last character in the line - it’s clear that you don’t meet him.

    And three more small questions - do you need only words with a small t ? Does it bother you that the displayed number merges into one line with "To continue ..."? are you looking at compiler warnings? already about the assignment in the if condition it should have warned you ...