error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘<unresolved overloaded function type>’) - c++14

I am not able to find the solution for the error no match for 'operator <<'
below is my code where the error is producing,
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
std::cin >> t;
while(t--) {
long int i,n,count=0,idx,min=LONG_MAX;
cin >> n;
long int s[n];
for(i = 0; i < n; i++) {
cin >> s[i];
if(s[i] <= min) {
min = s[i];
idx = i;
}
}
cout << "count= " << count << " max= " << max << " idx= " << idx << "\n";
}
}
I am getting an error like,
prog.cpp: In function ‘int main()’: prog.cpp:19:43: error: no match
for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and
‘<unresolved overloaded function type>’)
cout<< "count= "<< count << " max= " << max << " idx= " << idx <<"\n";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
can anyone pls help me to resolve this.

first of all max(line 19) is undeclared.
it's giving this error because it is trying to call std::max which is inbuilt(algorithm header file).
i guess you have typo it's min(on line 19) instead of max :).

Related

error: no matching member function for call to 'erase'?

I was working with vector and trying to work with erase function from vector library.
Here is my code
#include <iostream>
#include <vector>
int leftover(std::vector<int> aList)
{
// size = 1 will be termination point
if (aList.size() == 1)
{
return aList.at(0);
}
else
{
aList.push_back(aList.at(0));
aList.erase(aList.at(0));
aList.erase(aList.at(1));
return leftover(aList);
}
}
int main()
{
int x;
std::cout << "Enter a number: ";
std::cin >> x;
std::vector<int> intVector;
for (int i = 0 ; i < x; i++) {
intVector.push_back(i+1);
}
for (const int& i : intVector)
{
std::cout << i << " ";
}
std::cout << "\n";
//int leftwith = leftover(intVector);
//std::cout << leftwith << "\n";
return 0;
}
Anything with remove the element at particular position would be much appreciated.

if first input is larger than 2nd input it will choose 2 instead of 1

I'm having some trouble on my code if yourcurrency is larger than convert the convert will choose the 2 instead of 1
can't figure it out what's wrong with my code i've tried on different compiler I think the issue is the code itself can you help me guys?
#include <iostream>
#include <string>
using namespace std;
int picked[5] = {0,0,0,0,0};
int main(){
string currentvalue[5][5] = {{"1","0.01818484","0.015423716","2.4462308","0.019060361"},{"55.012111","1","0.84815483","134.47933","1.0482778"},{"64.90818","1.1789799","1","158.58285","1.2359668"},{"0.40901121","0.0074303636","0.0063022858","1","0.0077907482"},{"52.511804","0.95391572","0.80907737","128.35738","1"}};
string currency[5] = {"Philippine Peso","euro","pounds","yen","usd"};
int yourcurrency;
int convert;
int timesToRun = 5;
int number = 1;
system("COLOR 0a");
cout << "Choose your currency \n" << endl;
for (int counter = 0 ; counter < timesToRun; counter++)
{
cout << number;
cout << "." + currency[counter] << endl;
number++;
}
cout << "\nOption: ";
cin >> yourcurrency;
system("CLS");
yourcurrency = yourcurrency - 1;
picked[yourcurrency] = 1;
cout << "Select your currency you want to convert into \n" << endl;
number = 1;
for (int counter = 0; counter < timesToRun; counter++)
{
if (picked[counter] != 1){
cout << number;
cout << "." + currency[counter] << endl;
number++;
}
}
cout << "\nOption: ";
cin >> convert;
system("CLS");
cout << currency[yourcurrency]+ " - " + currency[convert];
cout << " [" + currentvalue[yourcurrency][convert] + "] " << endl;
cout << "Amount: ";
int cash;
cin >> cash;
double value = stof(currentvalue[yourcurrency][convert]);
double total = cash * value;
cout << currency[convert]<< ": " << total;
}

c++11 use std::map as return value

I usually return an object of std::vector or std::map as an incoming reference paremeter(as funcVec2 and funcMap2 below). But it is a bit inconvenient when writing codes. So I think if I can use return value under c++11(as funcVec1 and funcMap1 below) because it will call move constructor but not copy constructor, so it maybe still spend only one construct time and no deconstruct as the form of incoming reference paremeter.
But I write the codes below to verify it and it turns out that funcVec1 and funcMap1 takes more times then funcVec2 and funcMap2. So I am confused now why funcVec1 and funcMap1 takes so long?
#include <iostream>
#include <vector>
#include <map>
#include <chrono>
using namespace std;
vector<int> funcVec1() {
vector<int >vec;
for (int i = 0; i < 10; ++i) {
vec.push_back(i);
}
return vec;
}
void funcVec2(vector<int>&vec) {
for (int i = 0; i < 10; ++i) {
vec.push_back(i);
}
return;
}
map<int, int> funcMap1() {
map<int, int>tmpMap;
for (int i = 0; i < 10; ++i) {
tmpMap[i] = i;
}
return tmpMap;
}
void funcMap2(map<int, int>&tmpMap) {
for (int i = 0; i < 10; ++i) {
tmpMap[i] = i;
}
}
int main()
{
using namespace std::chrono;
system_clock::time_point t1 = system_clock::now();
for (int i = 0; i < 100000; ++i) {
vector<int> vec1 = funcVec1();
}
auto t2 = std::chrono::system_clock::now();
cout << "return vec takes " << (t2 - t1).count() << " tick count" << endl;
cout << duration_cast<milliseconds>(t2 - t1).count() << " milliseconds" << endl;
cout << " --------------------------------" << endl;
vector<int> vec2;
for (int i = 0; i < 100000; ++i) {
funcVec2(vec2);
}
auto t3 = system_clock::now();
cout << "reference vec takes " << (t3 - t2).count() << " tick count" << endl;
cout << duration_cast<milliseconds>(t3 - t2).count() << " milliseconds" << endl;
cout << " --------------------------------" << endl;
for (int i = 0; i < 100000; ++i) {
map<int, int> tmpMap1 = funcMap1();
}
auto t4 = system_clock::now();
cout << "return map takes " << (t4 - t3).count() << " tick count" << endl;
cout << duration_cast<milliseconds>(t4 - t3).count() << " milliseconds" << endl;
cout << " --------------------------------" << endl;
map<int, int>tmpMap2;
for (int i = 0; i < 100000; ++i) {
funcMap2(tmpMap2);
}
auto t5 = system_clock::now();
cout << "reference map takes " << (t5 - t4).count() << " tick count" << endl;
cout << duration_cast<milliseconds>(t5 - t4).count() << " milliseconds" << endl;
cout << " --------------------------------" << endl;
return 0;
}
you are not only meassuring the time for your operations, you also include the printouts. this is suboptimal.
you should measure performance in release mode. be aware that you are not doing anything usefull with your objects and the optimizer may throw away most of your code you wanted to measure.
the comparisons are not "fair". for example in your map1 case you are constructing an empty map, fill it (memory allocations happen here) and then you throw it away. in the map2 case you are reusing the identical map object over and over again. you are not allocating memory over and over again.

My program can't read all the data from a file

My program can't read all of the data from my MarvelIn.txt file.
It reads about 29 spaces in, and MarvelIn.txt contains only 9 entries, then I get a runtime error.
I think I have all the syntax right, however this is the only error I have. It will not output to the output file "MarvelOut.txt".
Here is the code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
using namespace std;
struct sstruct
{
string first, last;
string department;
int salary;
};
void init2(sstruct s[50])
{
int maxarray = 50;
sstruct init = { "Darth", "Vader", "None", 0 };
for (int i = 0; i < maxarray; i++) {
s[i] = init;
cout << "init: " <<s[i].first<<endl;
}
}
void read(sstruct s[50], int &nums)
{
int maxarray = 50;
ifstream inf("MarvelIn.txt");
int i = 0;
while (!inf.eof())
{
inf >> s[i].first >> s[i].last >> s[i].department >> s[i].salary;
cout << "read: "<<s[i].first<<s[i].last << s[i].department <<
s[i].salary << endl;
i++;
}
nums = i;
}
void avg(sstruct s[50], int &nums, double &average2)
{
int maxarray = 50;
int i;
for (i = 0; i < nums; i++)
average2 += s[i].salary;
average2 /= nums;
}
void print(sstruct s[50], int nums, double &average2)
{
int maxarray = 50;
ofstream outf("MarvelOut.txt");
int i = 0;
string temp;
outf << "the number of professors is: " << nums << endl;
cout << "the number of professors is: " << nums << endl;
outf << endl << "The average salary of the professors is: " << average2 << endl;
outf << "Advisor " << "Major " << " Department " << "Salary " << endl;
for (i = 0; i < nums; i++)
{
temp= s[i].last + "," + s[i].first;
cout << "last, first " << temp << endl;
outf << left << setw(20) << temp << right << setw(5)<< s[i].department << setw(5) << s[i].salary << setw(8) << endl;
}
outf << endl << endl;
}
void swap(sstruct &a, sstruct &b)
{
sstruct temp;
temp=a;
a=b;
b=temp;
}
void bubbleSort(sstruct s[50], int &nums)
{
int maxarray = 50;
int i, j;
bool swapped;
for (i = 0; i < nums - 1; i++)
{
swapped = false;
for (j = 0; j < nums - i - 1; j++)
{
if (s[j].department > s[j + 1].department)
{
swap(s[j], s[j+1]);
swapped = true;
}
}
// IF no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
}
int main() {
int nums=0;
double average3=0.0;
const int maxarray = 50;
sstruct s[maxarray];
init2(s);
print(s, nums, average3);
read(s, nums);
cout << "numsfirst: " << nums << endl;
avg(s, nums, average3);
cout << "nums" << nums << endl;
bubbleSort(s,nums);
print(s, nums, average3);
system("pause");
return 0;
}

How to ignore 0's in an Array and print out unique numbers

so my program is supposed to use one 5 sized array to store inputted integers. If it's a duplicated integer it would not be stored into the array.
The problem here is there would be 0's in my array indefinitely since i initialized the size to 5. I need to output only the unique numbers how would i do so?
One thing i noticed was that without my unsigned int position; whenever i enter a duplicate integer it would skip the index;
e.g. array[0] = 10, array[1] = 10 // duplicate, array[2] = 20 // inputted 20, this should've been stored into array[1] but it doesn't.
So i had the position to only increment whenever it's not a duplicate to make sure it's not skipping over the index when a duplicate is entered.
And was there anything i could've done or do a different approach to get my result?
Code:
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
const unsigned int MIN_VALUE = 10;
const unsigned int MAX_VALUE = 100;
const size_t arraySize = 5;
array <int, arraySize> numberArray = {};
template<size_t size>
bool isDuplicate(array<int, size> array, int value)
{
for (unsigned int i = 0; i < array.size(); i++)
{
if (value == array[i])
{
return true;
}
}
return false;
}
int main()
{
unsigned int input;
unsigned int position = 0;
for (unsigned int i = 0; i < arraySize; i++)
{
cout << "Enter # " << (i + 1) << " : ";
cin >> input;
if (input < MIN_VALUE || input > MAX_VALUE)
{
cout << "The number entered is not in valid range of 10 to 100" << endl;
--i;
}
else if (!isDuplicate(numberArray, input))
{
numberArray[position] = input;
position++;
cout << "The number: " << input << " is unique\n" << endl;
}
}
}
Thanks!
The only missing part in your code is an additional block below your else if block:
else {
cout << "The number: " << input << " is not unique\n" << endl;
--i;
}
where you would decrease your position when the value is a duplicate, and warn the user about it.
If I had to update your program while keeping the most of your code I would write:
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
const unsigned int MIN_VALUE = 10;
const unsigned int MAX_VALUE = 100;
const size_t arraySize = 5;
// Initialize all array values to 0 (see std::array documentation)
array <int, arraySize> numberArray = {0};
template<size_t size>
bool isDuplicate(array<int, size> arr, int val)
{
bool ret = false;
// Do not waste time with invalid values
if (val < MIN_VALUE || val > MAX_VALUE)
return ret;
// Using size_t to express size
size_t pos = 0;
// Loop until reaching the end OR a not yet set array value
while ( pos < arr.size() && arr[pos]){
if (arr[pos] == val) {
// Found!
ret = true;
break;
}
++pos;
}
return ret;
}
int main()
{
unsigned int input = 0;
size_t position = 0;
while (position < numberArray.size()) {
cout << "Enter # " << (position + 1) << " : ";
cin >> input;
if (input < MIN_VALUE || input > MAX_VALUE) {
cout << "The number entered is not in valid range of 10 to 100" << endl;
} else if (!isDuplicate(numberArray, input)) {
numberArray[position] = input;
// Pre-increment operator is more efficient, see doc.
++position;
cout << "The number: " << input << " is unique\n" << endl;
} else {
cout << "The number: " << input << " is not unique\n" << endl;
}
}
}
It looks like an exercise with a really weird specification. It should be better explained so as to give you a more relevant real-life solution. ;)
Hope this helps.

Resources