I'm a complete noob at C++, and the first problem I am encountering is the following:
no operator >> matches these operands
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "hello world!";
cin >> "hello world!";
}
std::cin needs to write to a variable, but you are passing it a const char[13] string literal instead.
You need to pass it something like a std::string instead:
std::string str;
std::cin >> str;
P.S. This is a good time to a) read compiler messages, b) avoid using namespace std; globally, c) get a good C++ book.
Related
So I am a CS student working on a project for exception handling (Try/catch). My teacher told us to implement the sstream library so we could use it in a class that outputs a message that includes a passed parameter of type int. For some reason unknown to me, when I use it, or even when I declare a variable of type stringstream, it causes a compile error with error message:
"copy constructor of 'tornadoException' is implicitly deleted because field 'ss' has a deleted copy constructor"
Here is my code. I am at a loss.
main.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "tornadoException.h"
using namespace std;
int main()
{
try{
int tornado = 0;
cout << "Enter distance of tornado: ";
cin >> tornado;
if(tornado > 2){
throw tornadoException(tornado);
}
else{
throw tornadoException();
}
}
catch(tornadoException tornadoObj){
cout << tornadoObj.what();
}
}
tornadoException.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "tornadoException.h"
using namespace std;
tornadoException::tornadoException(){
message = "Tornado: Take cover immediately!";
}
tornadoException::tornadoException(int m){
ss << "Tornado: " << m << "miles away!; and approaching!";
message = ss.str();
}
string tornadoException::what(){
return message;
}
tornadoException.h
#ifndef tornadoException_h
#define tornadoException_h
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class tornadoException{
public:
tornadoException();
tornadoException(int m);
string what();
private:
stringstream ss;
string message;
};
#endif
Alright, figured out the error but I'll leave up this post since I couldn't find the answer anywhere else. The problem was that I declared the stringstream buffer as a private variable in the class. The buffer needs to be declared locally within the function declaration it is being used in, in this case right before the loading of the buffer in the implementation file:
tornadoException::tornadoException(int m){
stringstream ss;
ss << "Tornado: " << m << " miles away!; and approaching!";
message = ss.str();
}
stringstream has a deleted copy constructor, which means that a stringstream object cannot be copied.
Since your tornadoException class has a stringstream variable, this means that your class cannot be copied either.
In your main function, you capture the exception by value, which means that you are copying it into the tornadoObj variable - which is not allowed.
Try changing the line
catch(tornadoException tornadoObj) to catch(tornadoException& tornadoObj) so that you're getting a reference to the exception instead of a copy of it.
This is actually a general rule: An exception shall always be caught by reference, not by copy: Core Guidelines E.15
An assignment wants me to make a function that makes all lowercase characters uppercase.
Here's my code:
main.cpp:
#include <iostream>
#include "Function.h"//Includes function file in main file
using namespace std;
int main(){
char a;
cout<<"Enter some words:";
cin.get(a);//Collects info from user
strcap(a);
cout<<a;
}
function.cpp:
#include <iostream>
#include "Function.h"
using namespace std;
char strcap(char a){
while (a!='\n'){
if (a>='a' && a<='z'){
a-=32;//
}
cin.get(a); //get the next letter
}
}
function.h:
#include <iostream>
char strcap(char a);
While the approach you have taken approximates a standard C approach, you have not provided adequate storage to read more than a single character. You could wrap your cin.get(a); strcap(a); cout << a; in a while loop, that would be an approach that is about a decade out of place in C++. Using std::basic::string provides automatic memory management for your input and std::transform makes it simple to apply a transformation to all elements of a container.
The example for std::transform provides exactly what you need, but it doesn't explain how to split the operation up into a separate header and source file. If after reading the documentation you are still stuck, the you can do something similar to the following.
Your header for function.h as you have it simply needs to provide for the declaration of strcap, e.g.
#include <string>
void strcap (std::string& s);
Your implementation for function.cpp likewise just needs to provide for the definition of strcap:
#include <cctype>
#include <algorithm>
#include "function.h"
void strcap (std::string& s)
{
std::transform (s.begin(), s.end(), s.begin(),
[](unsigned char c) -> unsigned char { return std::toupper(c); });
}
(note: the trailing-return-type "-> unsigned char" can be omitted above and it will be deduced properly)
Your main.cpp would then be:
#include <iostream>
#include "function.h"
int main (void) {
std::string s;
std::cout << "enter string: ";
if (getline (std::cin, s)) {
strcap(s);
std::cout << s << '\n';
}
}
Example Use/Output
Compile as you normally would, but you will require the language standard of at least -std=c++11, and then, e.g.
$ ./main
enter string: My dog has fleas
MY DOG HAS FLEAS
Also note, if you do not want to use std::transform, you can use a range-based-for loop to iterate over each character in your string converting to uppercase as well. Your strcap() function would then be:
void strcap (std::string& s)
{
for (auto& c : s)
c = toupper(c);
}
And if for some reason your compiler doesn't support the range-based for loop, then you can also use basic std::string:iterator to iterate over the string providing the conversion, e.g.
void strcap (std::string& s)
{
for (std::string::iterator it = s.begin(); it != s.end(); it++)
*it = toupper(*it);
}
There are several different approaches you can take.
Look things over and let me know if you have any further questions.
Code::Blocks Compiler Options
Just to make sure we are on the same sheet of paper, you should see:
I tried to write a program that takes input as a string and then passes that string to a macro which is supposed to insert the string as a plain-text expression but the macro is not behaving as I would suspect.
#include <iostream>
#include <cmath>
#include <string>
#define PARSE(a) a;
using namespace std;
int main() {
string c ;
int b;
cin >> c;
b = PARSE(c);
cout << b;
return 0;
}
This code will not compile, it gives an error saying that I cannot convert string to int, however PARSE(c) should not be treated like a string it should just be replaced by plain text.
The error means you are really trying to convert std::string (c) to int and it can't be done (b).
If you want to access the plain text, once you are using std::string, you should call the c_str() method, example:
cout << c.c_str();
#edit:
If you are trying to do something like "eval()" from PHP, you can check this: https://stackoverflow.com/a/11078610/12385171
I have looked at a few other questions regarding getline() not functioning, however most problems regarding the topic were due to the programmer not including the string header. I have the string header included however getline is still giving me error E0304 (which I have already looked into).
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
char input[100];
getline(cin, input);
cout << input << endl;
}
There are two forms of getline:
std::cin.getline(array, size); // reads into raw character array
getline(std::cin, string); // reads into std::string
You should use a std::string instead of a raw character array:
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string input;
getline(std::cin, input);
std::cout << input << "\n";
}
The non-member getline only works with std::string. Use the std::istream member function getline for C-style strings:
std::cin.getline(input, sizeof(input));
I'm having trouble understanding where I went wrong with my code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string str = "";
cin >> str;
remove(str.begin(), str.end(), ' ');
cout << str;
cin.ignore();
}
The error says "'remove': function does not take 3 arguments (C2660)"
Try adding
#include <algorithm>
"algorithm" is an STL header containing a lot of functions, including std::remove, which the OP is trying to call. The error he got was because there is another function that takes a single argument, called "remove", which deletes a file.