-2
#include <iostream>
#include <string>
using namespace;

int main()
{
    string word = " ";

    do
   {
        cout << "Enter a word that has at least 5 characters: " << endl;
        cin >> word;
       }while(word.size() < 5);

        char searchCh = '0';
       cout << "Enter a character and the program will tell " <<
        "you how many times it appears in the word " << word << "." << endl;
        cin >> searchCh;

        int counter = 0;

    for(int i = 0; i < (int)word.size(); i++ )
    {
        char ch = word.at(i)

        if(searchCh == ch)
        {
            counter++; //counter = counter + 1
        }
    }

    cout << "The number of " << searchCh << " 's in the word " << word <<  " is " << counter << ".\n";


}

I continuously receive multiple errors such as: 'endl' was not declared in the scope 'cin' was not declared in this scope 'word' was not declared in this scope 'string' was not declared in this scope expected ',' or ';' before '}' token

I am using codeblocks, if anyone could answer it would be much appreciated. Thank you:D

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    `using namespace;` should be `using namespace std;` – Random Davis Jan 24 '17 at 18:28
  • 1
    Not a behavioural change, but your entire for loop could be replaced by a call to `std::count`. As a bonus, it becomes instantly clear that the purpose of this piece of code is to count occurrences of a value. – chris Jan 24 '17 at 18:45
  • Codeblocks is an IDE; this question has nothing to do with any IDE. – MrEricSir Jan 24 '17 at 19:35
  • Note that you shouldn't really use `using namespace std;` anyway, because it's bad practice. Drop it before you start writing your first header files! – Christian Hackl Jan 24 '17 at 20:58

1 Answers1

2

One of your first lines is

using namespace;

You probably wanted to write

using namespace std;

However, please read Why is using namespace std considered bad practice?

Community
  • 1
  • 1
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    Regarding that this it is a bad practice: I am not c++ programmer but one can use namespace alias (http://en.cppreference.com/w/cpp/language/namespace_alias) on libraries that colide it's names with std. I personally like to type less. – MaLiN2223 Jan 24 '17 at 18:32
  • @MaLiN2223 I prefer to have my code as explicit as possible. There are of course other ways to deal with the problem, but not being aware of it isn't a solution. Maybe I will try to phrase the answer a bit less subjective – 463035818_is_not_an_ai Jan 24 '17 at 18:35
  • I do not disagree with you, my habbits from C# are of much weight here. However I just wanted to state the alternative (as you said, not being aware is not a solution at all :P ). – MaLiN2223 Jan 24 '17 at 18:37
  • 1
    @MaLiN2223 yeah sure, no offense taken nor intended. Actually thanks for the input :) – 463035818_is_not_an_ai Jan 24 '17 at 18:39
  • @MaLiN2223, One difference from C# is that `std` is huge, whereas C# namespaces are more manageable. – chris Jan 24 '17 at 18:44
  • 1
    @MaLiN2223: Easier typing is not an important goal in programming. Easier **reading** is. Writing `std::` everywhere tends to make code more readable. And I fail to see how namespace aliases avoid the problem with `using namespace` at global scope in header files. – Christian Hackl Jan 24 '17 at 21:03