C++ cin and strnicmp not working - c++

void searchFlight(cust flights[] ,int row)
{
clrscr();
cout << "Search for the flight you are looking for.\n";
string airport;
cout << "Enter Departing Flight : ";
cin >> airport; //error
for (int r=0;r<row;r++)
{
if (strnicmp(airport, flights[r].airport[20], strlen(airport) ==0) //error
{
clrscr();
cout << flights[r].name[20] <<endl;
cout << flights[r].airport[20] <<endl;
cout << flights[r].destination[20] <<endl;
cout << flights[r].ticket <<endl;
cout << flights[r].passangers <<endl;
cout << flights[r].opCost <<endl;
cout << flights[r].income <<endl;
cout << flights[r].netProfit <<endl;;
pressKey();
}
}
pressKey();
}
For the cin error:
error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
For the strnicmp error:
error C2664: 'strlen' : cannot convert parameter 1 from 'std::string' to 'const char *'
I have searched for solutions to this problem and could not fix it. Apologies if there is a similar post on here which could have solved my problems.

For the cin error: error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
Add #include <string> to your CPP file.
For the strnicmp error: error C2664: 'strlen' : cannot convert parameter 1 from 'std::string' to 'const char *'
Confirm that you have #include <cstring> to your CPP file, and replace your call with:
strnicmp(airport.c_str(), flights[r].airport[20], strlen(airport.c_str()) ==0.
I suspect that flights[r].airport[20] is also incorrect, but I can't know because you didn't post a complete program.
If cust::airport is declared like std::string airport;, then you need flights[r].airport.c_str().
If cust::airport is declared like char airport[20];, then you need flights[r].airport.

Have you included the following :
<fstream>
<istream>
<iostream>
<string>
I'm pretty sure you forgot the <string>
Here's a test code I ran and it worked flawlessly!
#include <string>
#include <iostream>
using namespace std;
int main(int, char**)
{
string foo;
cin >> foo;
cout << foo;
}

Related

Why I cannot cout the result?

I have this error message:
Error C2678 binary '^': no operator found which takes a left-hand
operand of type 'std::basic_ostream<char,std::char_traits<char>>'
(or there is no acceptable conversion)
with this code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << 2 ^ 4;
}
could you help me with this code, please
I use visual studio 2019
operator<< has higher precedence than operator ^, so cout << 2 ^ 4; is interpreted as (cout << 2) ^ 4;. (cout << 2) returns cout itself, which can't be used as operand of operator ^.
Change the code to
cout << (2 ^ 4);
Can you use this code.
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << (2 ^ 4);
return 0;
}

C++ cout not accepting strings or strings with + [duplicate]

This question already has an answer here:
What is "pch.h" and why is it needed to be included as the first header file?
(1 answer)
Closed 3 years ago.
class Book {
public:
string title;
string author;
void readBook() {
cout << "Reading" + this->title + " by " + this->author << endl;
}
};
This is causing the following error.
binary '<<': no operator found which takes a right-hand operand of type 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
Secondly
cout << part1 << endl;
This is causing this error.
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::string'
My include statements
#include <string>
#include "pch.h"
#include <iostream>
#include <vector>
#include <exception>
#include <stdio.h>
#include <Windows.h>
using namespace std;
All in VS 2017.
I can get the strings to print with
cout << part1.c_str() << part2.c_str() << endl;
Can someone explain to me why it won't print the strings without .c_str() and why it won't accept multiple strings? I am following a tutorial and the tutor is able to process these variables without error.
cout << somethig is implemented by operator overloading..
operator <<( const char * ) is exist..
but operator <<( string ) is not exist..
c_str() returns const char *. so, You can use it with cout <<
This will work..
cout << "Reading" << this->title.c_str() << " by " << this->author.c_str() << endl;

Program has errors, binary operators

#include <iostream>
#include <fstream>
using namespace std;
class InsurancePolicy
{
friend fstream& operator<<(fstream&, InsurancePolicy);
friend istream& operator>>(istream&, InsurancePolicy&);
private:
int policyNum;
string lastName;
int value;
int premium;
};
fstream& operator<<(fstream& out, const InsurancePolicy pol)
{
out << pol.policyNum << " " << pol.lastName << " " << pol.value << " " << pol.premium << endl;
return out;
}
istream& operator>>(istream& in, InsurancePolicy& pol)
{
in >> pol.policyNum >> pol.lastName >> pol.value >> pol.premium;
return in;
}
int main() {
ofstream outFile;
outFile.open("Policy.txt");
InsurancePolicy aPolicy[10];
for (int count = 0; count < 10; ++count)
{
printf("Enter the policy number, the holder's last name, the value, and the premium.");
cin >> aPolicy[count];
outFile << aPolicy[count] << endl;
}
return 0;
}
This program will not compile because of the following errors:
Severity
Code
Description
Project
File
Line
Suppression State
Error
C2679
binary '>>': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
Project6
c:\users\preston freeman\source\repos\jave.cpp
21
Error
C2679
binary '<<': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)
Project6
c:\users\preston freeman\source\repos\jave.cpp
16
Error
C2679
binary '<<': no operator found which takes a right-hand operand of type 'InsurancePolicy' (or there is no acceptable conversion)
Project6
c:\users\preston freeman\source\repos\jave.cpp
32
How do I fix these errors?
Thank you for your time.
You are using
ofstream outFile;
in main.
But your operator overloading is
friend fstream& operator<<(fstream&, InsurancePolicy);
Note that ofstream is not derived from fstream, that's why overloading operator << for fstream won't make it work for ofstream
You can simply change fstream to ofstream
ofstream& operator<<(ofstream& out, const InsurancePolicy pol)
{
out << pol.policyNum << " " << pol.lastName << " " << pol.value << " " << pol.premium << endl;
return out;
}
And better to pass in InsurancePolicy by const& since ofstream won't change it, and copy a user defined object can be expensive
ofstream& operator<<(ofstream& out, const InsurancePolicy& pol)
You may also need to #include <string>
Demo:
https://ideone.com/rJiw21

Simple C++ program not compiling using wstrings and cout

I´m building this simple C++ program using Visual Studio 2012:
#include <stdafx.h>
#include <string>
#include <iostream>
int main()
{
std::wcout << "Hello World...";
std::string input_data;
std::string output_data("Hello. Please type your name");
std::wcout << output_data;
std::wcin >> input_data;
std::wcout << "Your name is " << input_data;
return 0;
}
I can´t compile. Getting the followig errors:
error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::wistream' (or there is no acceptable conversion)
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
IntelliSense: no operator "<<" matches these operands
operand types are: std::basic_ostream<wchar_t, std::char_traits<wchar_t>> << std::string
IntelliSense: no operator "<<" matches these operands
operand types are: std::wostream << std::string
IntelliSense: no operator ">>" matches these operands
operand types are: std::wistream >> std::string
Can someone help me to fix that ?
You should try changing all std::string ocurrences for std::wstring... or all wcin/wcout for cin/cout... (in the first case, prefix the strings like L"aaa" This, for instance, works perfectly:
#include <string>
#include <iostream>
int main()
{
std::wcout << L"Hello World...";
std::wstring input_data;
std::wstring output_data(L"Hello. Please type your name");
std::wcout << output_data;
std::wcin >> input_data;
std::wcout << L"Your name is " << input_data;
return 0;
}
Alternatively, you could switch everything to narrow strings:
#include <string>
#include <iostream>
int main()
{
std::cout << "Hello World...";
std::string input_data;
std::string output_data("Hello. Please type your name");
std::cout << output_data;
std::cin >> input_data;
std::cout << "Your name is " << input_data;
return 0;
}

Why is writing a std::string to cout causing an unknown operator << error?

I am getting an error when I try to output the return value from one of my methods:
Error: No operator "<<" matches these operands. Operand types are: std::ostream << std::string
Main.cpp
#include <iostream>
using namespace std;
#include "Book.h"
int main()
{
book.setTitle("Advanced C++ Programming");
book.setAuthorName("Linda", "Smith");
book.setPublisher("Microsoft Press", "One Microsoft Way", "Redmond");
book.setPrice(49.99);
cout << book.getBookInfo(); // <-= this won't compile because of the error above.
int i;
cin >> i;
return 0;
};
Method which should return string:
string Book::getBookInfo()
{
stringstream ss;
ss << title << endl << convertDoubleToString(price) << endl;
return ss.str();
}
#include <string> is missing.
How did the code get the definition of string? The header <string> also declares the stream inserter.