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

main()
{

    bool string1[20];
    cout << "Enter string: ";
    cin >> string1;
    int counter = 0;
    int length;
    length = strlen(string1);

This is incomplete code, but my question is why am I getting a compiling error when using cin? It says:

error: no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘bool [20]’)

On this line:

 cin >> string1;

I'm not sure how to fix this.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Joe
  • 1
  • 1
  • 4
  • What exactly are you expecting someone to enter with `bool string1[20]`? Are you expecting your user to input something like "true", or like `0101010101101`? – Tas Oct 13 '17 at 03:47
  • I want them to input a string of characters or numbers like gfc098 – Joe Oct 13 '17 at 03:48
  • Then why do you have a `bool` array? Why not use `std::string` like you included? – Tas Oct 13 '17 at 03:49
  • Didn't think of that but it doesn't seem to solve the compile error – Joe Oct 13 '17 at 03:52
  • when i change it to a string the compile error shows up as error: no match for ‘operator>>’ (operand types are ‘st d::istream {aka std::basic_istream}’ and ‘std::string [20] {aka std::basic _string [20]}’) cin >> string1; I still have the cin error – Joe Oct 13 '17 at 03:59
  • @Joe you need to change `bool string1[20];` to either `char string1[20];` or `string string1;` not to `string string1[20];` – Remy Lebeau Oct 13 '17 at 04:45
  • 2
  • Possible duplicate of [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) –  Oct 13 '17 at 05:22

3 Answers3

1

bool string1[20]; is the wrong choice for the user input as a string, all it does is create an array of 20booleans, true or false which is not what you want.

what you are after is your included #include <string>

string string1;
cout << "Enter string: ";
cin >> string1;

Instead of using strlen you get the length by using the length method provided by std::string

auto length = string1.length()

Samer Tufail
  • 1,835
  • 15
  • 25
0

There is no operator>> for reading an array of bool values. What you need is an array of char values instead:

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

int main() {
    char string1[20];
    cout << "Enter string: ";
    cin >> setw(20) >> string1;
    int length = strlen(string1);

Or better, a single std::string:

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

int main() {
    string string1;
    cout << "Enter string: ";
    cin >> string1;
    int length = string1.length();
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
-1

I think you are confusing string with array. string1 in your code is not an string its an array. So, you can't just put data in it without giving the proper index number. Also remember its an bool type so you can only enter 0/1/true/false value.

Again, you have used strlen() function in your code which is used for determining the length of the string but your is an array. You didn't ask about this but when I ran the code in my IDE it got error.

Here is one way to do it :

    main()
    {
        bool string1[20];
        cout << "Enter string: ";
        for(int i=0;i<20;i++)//iterating through the boolian array
        {
            cin >> string1[i];
        }
        int counter = 0;
        int length;
        length = sizeof(string1)/sizeof(string1[0]);
        cout<<length;//printing the size of the array
    }