cquery no matching funtion for call 'to_upper' - c++

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
int main() {
string city1, city2;
cout << ("Please enter your citys name");
cin >> city1;
cout << ("Please enter your citys second name");
cin >> city2;
cout << city1 [0,1,2,3,4];
cout << city2 [0,1,2,3,4];
boost::to_upper(city1, city2);
cout << city1,city2;
}
This is my code and for some reason boost::to_upper(city1, city2); gets the error: [cquery] no matching funtion for call 'to_upper'

boost::algorithm::to_upper is declared as (from boost reference)
template<typename WritableRangeT>
void to_upper(WritableRangeT & Input, const std::locale & Loc = std::locale());
so you can pass only one string to this function. Replacing
boost::to_upper(city1, city2);
with
boost::to_upper(city1);
boost::to_upper(city2);
makes the code compile and an example output is Please enter your citys namePlease enter your citys second nameosLONDON.
It lacks newline characters and there is one more mistake - misunderstanding of comma operator. Generally comma is used to separate arguments or array elements but in lines
cout << city1 [0,1,2,3,4];
cout << city2 [0,1,2,3,4];
// ...
cout << city1,city2;
comma operator is used. Comma operators takes two operands and it's value is value of the right operand (e.g. after int x = (1, 2); variable x is equal to 2). Code above is equivalent to
cout << city1[4];
cout << city2[4];
// ...
cout << city1;
city2;
Finally, the corrected code is
#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
int main() {
string city1, city2;
cout << "Please enter your citys name" << std::endl;
cin >> city1;
cout << "Please enter your citys second name" << std::endl;
cin >> city2;
cout << city1 << std::endl;
cout << city2 << std::endl;
boost::to_upper(city1);
boost::to_upper(city2);
cout << city1 << std::endl << city2 << std::endl;
}

Related

Why am I getting an error trying to read data from an istringstream?

I have the following C++ code:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <cstring>
using namespace std;
int main() {
istringstream inSS;
string title;
string col1;
string col2;
string val;
int numCommas;
vector<string> stringData();
vector<int> intData();
cout << "Enter a title for the data:" << endl;
getline(cin, title);
cout << "You entered: " << title << endl << endl;
cout << "Enter the column 1 header:" << endl;
getline(cin, col1);
cout << "You entered: " << col1 << endl << endl;
cout << "Enter the column 2 header:" << endl;
getline(cin, col2);
cout << "You entered: " << col2 << endl << endl;
while (1) {
cout << "Enter a data point (-1 to stop input):" << endl;
getline(cin, val);
if(strcmp(val.c_str(), "-1") == 0) {
break;
}
inSS >> stringData >> intData;
cout << "Data string: " << stringData << endl;
cout << "Data integer: " << intData << endl;
}
return 0;
}
Error in question:
main.cpp: In function 'int main()': main.cpp:46:9: error: no match for 'operator>>' (operand types are 'std::istringstream {aka std::cxx11::basic_istringstream<char>}' and 'std::vector<std::cxx11::basic_string<char> >()')
inSS >> stringData >> intData;
~~~^~~~~~~~~~~
What does this error mean? How do I fix it?
The error has to do with a combination of factors. For starters, let's look at this line:
inSS >> stringData >> intData;
Here, you're trying to read from an istringstream into a vector<string> and vector<int>. However, you can't use the stream extraction operator to read a vector from a stream - there's no fundamental reason why not, it's just that the standard doesn't allow it. You'll need to read that data one element at a time, which will likely require you to rewrite a lot of this code.
There's another more subtle issue here. These lines don't do what you think they do:
vector<string> stringData();
vector<int> intData();
These lines look like they declare variables named stringData and intData of type vector<string> and vector<int>, using the default constructors. Unfortunately, C++ interprets these, believe it or not, as function prototypes. That first one is a prototype of a function named stringData that takes no arguments (hence the emptiness between the parentheses) and returns a vector<string>, for example. To fix this, drop the parentheses. Just write
vector<string> stringData;
vector<int> intData;
To summarize:
You'll need to fundamentally make some changes to your code, since you can't read from an istringstream into a vector. That necessitates a logic update.
You'll need to fix the two declarations from earlier on by dropping the parentheses.

is there a better way? new to c++

I am new to c++ but do have a basic knowledge in coding. This program works fine and well but I'm wondering if there is a better way to do this.
The program makes a star wars name by taking the first three letters of your last name and the first 2 of your first name to make your first name of your star wars name. Then for your star wars surname it takes the first two letters of your mother's maiden name and the first three letters of the city you were born in.
// starWarsName.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string firstName;
string surname;
string maidenName;
string city;
cout << "This program is designed to make you a star wars name, it takes some information and concatinates parts of the information to make your NEW name" <<endl << endl;
cout << "please enter your first name" << endl;
cin >> firstName;
cout << "please enter your surname" <<endl;
cin >> surname;
cout << "what is your mothers maiden name?" << endl;
cin >> maidenName;
cout << "please tel me which city you were born in" << endl;
cin >> city;
cout << firstName << " " << surname << endl;
cout << firstName[0] << " " << surname << endl;
int size = firstName.length();
//cout << size;
cout << surname[0] << surname[1] << surname[2] << firstName[0] << firstName[1];
cout << " " << maidenName[0] << maidenName[1] << city[0] << city[1] << city[2];
cin.get();
cin.ignore();
return 0;
}
You can use string::substr here to store character sequence instead of writing surname[0]..surname[2] again and again.
Here is an example of string::substr
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}
Output:
think live in details.

no operator matches || these operands

Hi im having trouble with an if statement in C++. When I compile my code I get an error stating " no operator "||" matches these operands". Any guesses? The project is a small text based game I'm creating in visual studio.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <fstream>
using namespace std;
//Prototypes
void introScreen(int);
string characterCreation(string);
int choice;
string characterName, wepon1, wepon2, bow, sword, mace;
const string sword = sword;
const string bow = bow;
const string mace = mace;
// Functions
int main()
{
introScreen(choice);
return 0;
}
void introScreen(int choice)
{
cout << "----------------------------------------\n"
<< "Welcome to the arena!\n"
<< "Select a menu option\n"
<< "-----------------------------------------\n"
<< "1. New Game\n"
<< "2. Load\n"
<< "3. Exit\n\n"
<< "Enter your desired number ";
cin >> choice;
if (choice == 1)
characterCreation(characterName);
else
if (choice == 2)
exit(0);
else
if (choice == 3)
exit(1);
}
string characterCreation(string characterName,string wepon1,string wepon2, const string bow, const string sword, const string mace)
{
cout << "Welcome to the character creation menu!\n"
<< "Enter your name\n"
<< "Name: ";
cin.ignore();
getline(cin, characterName);
ofstream loadFile("Save.txt");
loadFile << characterName << endl;
cout << "\nEnter 2 wepons\n"
<< "Wepon list\n\n"
<< "Sword\n"
<< "Mace\n"
<< "Bow\n";
cin >> wepon1, wepon2;
if (wepon1 || wepon2 != bow || sword || mace)
{
cout << "\n\nThose wepons are invalid! Enter new ones\n";
cout << "\nEnter 2 wepons\n"
<< "Wepon list\n\n"
<< "sword\n"
<< "mace\n"
<< "bow\n";
cin >> wepon1, wepon2;
}
loadFile << wepon1 << endl
<< wepon2 << endl;
return characerName;
}
The '||' operator is an evaluation of a logical condition and you are asking it to evaluate a string type. Generally, when strings values are evaluated, they are evaluated as
if (weapon1 != "") then...
Also, be careful about how you set the value. String values are passed inside double quotation marks.

Can someone help me solve this issue?

//program8.cpp
#include <iostream>
#include "species.h"
#include "reptilian.h"
#include "mammalian.h"
#include "insects.h"
using namespace std;
void VirtualPrint(species &typeofSpecies){
typeofSpecies.printme();
}
void VirtualDanger(species &typeofSpecies){
typeofSpecies.showDanger();
}
int main(int argv, char **args){
reptilian rep;
insects ins;
VirtualPrint(rep);
VirtualPrint(ins);
VirtualDanger(rep);
VirtualDanger(ins);
return 1;
}
//species.h
#ifndef SPECIES_H
#define SPECIES_H
#include <iostream>
#include <string>
using namespace std;
class species{
public:
species();
virtual void printme();
virtual void showDanger() = 0;
protected:
string name;
string color;
int lifeExp;
bool thr;
};
#endif
//species.cpp
#include <iostream>
#include <string>
#include "species.h"
using namespace std;
species::species(){
cout << "Please enter name of species:" << endl;
getline(cin, name);
cout << "Please enter color of species:" << endl;
getline(cin, color);
cout << "Please enter life expectancy of species in years:" << endl;
cin >> lifeExp;
cout << "Please enter if species is threat:true(1) or false(0)" << endl;
cin >> thr;
}
void species::printme(){
cout << "Name: " << name << " Color: " << color << " Life Expectancy: " << lifeExp << " Threat: " << thr << endl;
}
//reptilian.h
#ifndef REPTILIAN_H
#define REPTILIAN_H
#include <iostream>
#include <string>
#include "species.h"
using namespace std;
class reptilian : public species{
public:
reptilian();
virtual void printme();
virtual void showDanger();
protected:
int length;
int lethality;
};
#endif
//reptilian.cpp
#include <iostream>
#include "reptilian.h"
using namespace std;
reptilian::reptilian(){
cout << "Please enter length(inches): " << endl;
cin >> length;
cout << "Please enter lethality(0-100): " << endl;
cin >> lethality;
}
void reptilian::printme(){
species::printme();
cout << " Length: " << length << " Lethality: " << lethality << endl;;
}
void reptilian::showDanger(){
cout << endl;
if(thr == true){
cout << "This species is a threat" << endl;
cout << "The name of the species is " << name << " has a color of " << color << ", has a life expectancy of " << lifeExp << " has a length of " << length << ", and a lethality of " << lethality << endl;
}
}
This is my code for my program it runs fine if one reptilian object is made. However when two are made it will not take the name of the second object. The same happens when two insects objects are made. I have not tested the mammalian object yet as I am trying to solve this problem first
edit:
output:
Please enter name of species:
sam
Please enter color of species:
orange
Please enter life expectancy of species in years:
100
Please enter if species is threat:true(1) or false(0)
0
Please enter length(inches):
60
Please enter lethality(0-100):
0
Please enter name of species:
Please enter color of species:
yellow
Please enter life expectancy of species in years:
20
Please enter if species is threat:true(1) or false(0)
0
Please enter length(inches):
10
Please enter lethality(0-100):
0
Please enter how venomous the species is(0-100):
0
Name: sam Color: orange Life Expectancy: 100 Threat: 0
Length: 60 Lethality: 0
Name: Color: yellow Life Expectancy: 20 Threat: 0
Length: 10 Lethality: 0
Venomous: 0
Your problem is mixing using getline with the >> operator. From http://en.cppreference.com/w/cpp/string/basic_string/getline:
When used immediately after whitespace-delimited input, e.g. after int n; std::cin >> n;, getline consumes the endline character left on the input stream by operator>>, and returns immediately. A common solution is to ignore all leftover characters on the line of input with cin.ignore(std::numeric_limits::max(), '\n'); before switching to line-oriented input.
So after the cin >> lethality a newline is left in the cin stream. The getline in the second instance of species sees this newline and immediately returns.
Also refer to this: std::cin:: and why a newline remains
To fix this, change your getline calls to use the cin >> method.

C++ Menu and String Searching within Array

When I enter this code and try to run it, it isn't working when the user selects option 1, to enter some text and a string to search for within their text. It outputs "enter text" and then "enter string to search" immediately after, without giving the user the chance to input some text. What is wrong?
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <algorithm>
using namespace std;
string s1, text;
int rand(int*);
int Array[100];
void sortArray(int[], int);
void showArray(const int [], int);
int main()
{
while (1)
// Menu to prompt user choice
{
char choice[1];
cout << endl;
cout << endl;
cout << "--MENU--" << endl;
cout << "1. Pattern Matching" << endl; // search for string within text
cout << "2. Sorting Techniques" << endl; // generate and then sort 10 random numbers
cout << "Enter your choice: " << endl;
cout << endl;
cin >> choice;
cout << endl;
if (choice[0] == '1') // string search option
{
cout << "Enter text:" << endl; // accept text from user
getline (cin, s1);
cout << "Enter string to search:" << endl; // accept string to search from user
getline (cin, text);
int pos = s1.find(text); // finds position where the string is located within text
if (pos >= 0)
{
cout << "Found '" << text << "'" << " at position " << pos + 1 << "." << endl;
}
else
{
cout << "Did not find text." << endl;
}
}
This is because cin >> choice reads part of the current input line for the choice entered by the user. The first getline() call reads the remaining part of the input line immediately following the choice entered by the user. You need to ignore the rest of the input line after the choice.
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
You will also need to add #include <limits> to the beginning of your code in order to pull in numerical_limits.
It looks as though you are defining some sort of char array for the user response. I would tend to make that a non-zero integer type with an exception if the choice is neither 1 nor 2. There are also some shortcuts for output formatting that reduces lines of code. Also, you would want to include the standard string class to accept the string. Maybe try something like the following:
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <algorithm>
using namespace std;
string s1, text;
int rand(int*);
int Array[100];
void sortArray(int[], int);
void showArray(const int [], int);
int main()
{
while (1)
// Menu to prompt user choice
{
int choice;
cout << "\n--MENU--\n"l;
cout << "1. Pattern Matching\n"; // search for string within text
cout << "2. Sorting Techniques\n"; // generate and then sort 10 random numbers
cout << "Enter your choice:\n";
cin >> choice+"\n";
if (choice == 1 && choice > 0 && choice != 0) // string search option
{
cout << "Enter text:" << endl; // accept text from user
getline (cin, s1);
cout << "Enter string to search:" << endl; // accept string to search from user
getline (cin, text);
int pos = s1.find(text); // finds position where the string is located within text
if (pos >= 0)
{
cout << "Found '" << text << "'" << " at position " << pos + 1 << ".\n";
}
else
{
cout << "Did not find text.\n";
}
}}}