Why is cin operation undefined? - c++

#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.

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()

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();

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
}

Related

ERROR: Invalid conversion from ‘const char*’ to ‘char*’ debugging homework problem

I'm new to C++ and am having a hard time debugging this. Any idea why I am getting this error? The upper function is supposed to take in a pointer to a C-string as an argument, iterate through each character in the string and convert it to uppercase. Also, how can I get my function to return the string in uppercase?
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
string upper(char* some_string){
for (int i=0; i < strlen(some_string); ++i){
toupper(some_string[i]);
cout << some_string[i];
}
return "Done"
}
int main(){
std::string word;
cout << "Enter a string: ";
cin >> word;
upper(word.c_str());
}
Make the argument const char* because the function doesn't modify the string.
Apply toupper() to what should be printed. (toupper() returns int, so the result should be casted to char to have it be printed as characters)
Add a semicolon after return "Done".
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
string upper(const char* some_string){ // use const char*
for (int i=0; i < strlen(some_string); ++i){
cout << static_cast<char>(toupper(some_string[i])); // apply toupper() to what to print
}
return "Done"; // add a semicolon
}
int main(){
std::string word;
cout << "Enter a string: ";
cin >> word;
upper(word.c_str());
}

Is there a way to search an element of a vector<String>, then check if it contains a particular char, if it does print it

The final element in the vector is the char to search for.
Here’s my code:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector<string> words;
string in;
while(cin>>in)
{
words.push_back(in);
}
int size = words.size()
string check = words.at(size-1);
}
I tried your code and the loop was infinite. Try asking a set number of words then use the find method for each string. Here's an example.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> words;
string in;
cout << "Enter 5 words: \n";
for (int i{0}; i < 5; ++i)
{
cout << "Word: ";
cin >> in;
words.push_back(in);
}
cout << "What character do you want to search for? ";
char c;
cin >> c;
for (auto word : words)
{
if (word.find(c) != string::npos)
{
cout << "Character \"" << c << "\" found in " << word << endl;
}
}
return 0;
}
Edit: I noticed that you tried to use int to store the size for the string. Use size_t instead. I also think you meant to use the length method of the string, which also returns size_t.

string subscript out of range c++

I need a help in a very basic c++ code.
My program is about guessing name game the problem which i faced is in reading string char by char
#include <iostream>
#include <time.h>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
void Play(int, int,int, string[], string[]);
string GetRandomName(int, int, int , string[], string[]);
const int ArrayMax = 100;
void Play(int selection, int FArraySize, int MArraySize,string Female[], string Male[])
{
int MAX_TRIES = 3;
int i=0;
ofstream ofFile;
ifstream InFile;
int num_of_wrong_guesses=0;
char letter;
string GuessedName;
GuessedName = GetRandomName(selection, FArraySize, MArraySize, Female, Male);
cout << "Guess the following name:" << endl;
while (GuessedName[i]!= 0 ){
cout<<"?";
i++;
}
cout << "\nEnter a guess letter? or * to enter the entire name" << endl;
cin >> letter;
return;
}
I don't complete coding...
the problem is in the while loop how can i solve it without using cstring?
could you help me?
int i = 0;
while(GuessedName[i] != 0)
{
cout << "?";
i++;
}
Seems like you are trying to print sequence of ? with the length of the string to guess. But you cannot treat std::string as c-string. When its length is n, GuessedName[n] is string subscript out of range - you cannot access one element past end - it's not null-terminated. Use for loop:
for(int i = 0; i < GuessedName.length(); ++i)
cout << "?";
Or simply:
cout << std::string(GuessedName.length(), '?');
Change the while loop like this:
while (GuessedName[i]){
cout<<"?";
i++;
}

Program won't allow 2 inputs?

So I am trying to get the user to input their name and height.
I have got other code.
I have got this.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int name1;
cout << "What's your name?";
cin >> name1;
int height1;
cout << "What's your height?";
cin >> height1;
return 0;
}
The issue is that it won't allow the user to enter their height. Any ideas?
The Problem is, that you're using int variables instead of std::string. However, you already included the <string> header file, so you probably want to do this:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
std::string name1;
cout << "What's your name?";
cin >> name1;
std::string height1;
cout << "What's your height?";
cin >> height1;
return 0;
}
Otherwise it only works if you enter integer numbers - but that doesn't make much sense for an 'name' input.
Edit: If it's also your requirement to input names with whitespaces, you could use std::getline
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
std::string name1;
cout << "What's your name?";
getline(cin, name1);
std::string height1;
cout << "What's your height?";
getline(cin, height1);
return 0;
}

Indexing an array of chars - issue with pointers

#include <iostream>
#include <cstring>
using namespace std;
void getInput(char *password, int length)
{
cout << "Enter password: ";
cin >> *password;
}
int countCharacters(char* password)
{
int index = 0;
while (password[index] != "\0")
{
index++;
}
return index;
}
int main()
{
char password[];
getInput(password,7);
cout << password;
return 0;
}
Hi!
I'm trying two things here which I'm unable to do atm. I'm trying to create a char array with unspecified length in main, and I'm trying to count the number of words in the char array in the function countCharacters. But password[index] doesnt work.
EDIT: I'm doing a homework assignment, so I have to use cstrings only.
EDIT2: And I'm not allowed to use the "strlen"-function either.
At first replace
char password[];
by
char password[1000]; // Replace 1000 by any maximum limit you want
And then replace:
cin >> *password;
by
cin >> password;
Also instead of "\0" you should put '\0'
P.S. There is no char array with unspecified length in C++, you should use std::string instead(http://www.cplusplus.com/reference/string/string/):
#include <string>
int main() {
std::string password;
cin >> password;
cout << password;
return 0;
}