Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
My program won't run I get an error message saying:
`error: no match for 'operator<<' (operand types are 'std::string' {aka 'std::__cxx11::basic_string<char>'} and '<unresolved overloaded function type>')|`
On the cout << address, " ", street << endl; line
I was using VS2017 but switched to CodeBlocks mid-way through
I have Windows 10 Pro Ryzen 5 2400G, 1060 6gb 16gb ram
Here is my program:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, city, state, road, country, street;
int address;
cout << "Enter your: Name\n";
cin >> name;
cout << "Enter your Street\n";
cin >> street;
cout << "Enter your: Address\n";
cin >> address;
cout << "Enter your:\n City\n";
cin >> city;
cout << "Enter your: Province/State\n";
cin >> state;
cout << "Enter your: Country\n";
cin >> country;
//Output
cout << name << endl;
cout << address, " ", street << endl;
cout << city, " ", province, " ", country;
}
Thanks in advance!
Your syntax is wrong. You can't use , to chain arguments to cout like that. Instead do:
cout << address << " " << street << endl;
cout << city << " " << province << " " << country;
Your last two statements are syntactically incorrect. They should be as follows -
cout << address<<" "<<street << endl;
cout << city<< " "<< province<<" "<< country;
You probably are trying to use something like python in C++. But obviously that doesn't work. Each time you just have to keep doing cout<< variable1 << " " << variable2<< " "; . This is how chaining works in C++. No shorthand to that
I hope this solves your issue!
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 days ago.
Improve this question
I created a class where I am defining member variables of string and integer data types.
When the function is called for a new object, I type in the string value, press Enter, and all the variables left are given some default value, and my program ends.
I do not know how to stop it from just ending, and letting me type each string value.
Here is a copy of my code:
#include <iostream> //Header file needed to do inputs and outputs
#include <iomanip> //Header file needed to use setw
#include <math.h> //Header file needed to use power function
using namespace std;
class MovieData
{
private:
string title, director;
int year, runTime;
public:
void setInfo()
{
cout << "Movie title: ";
cin >> title;
cout << "Diector: ";
cin >> director;
cout << "Year: ";
cin >> year;
cout << "Runnign Time: ";
runTime;
}
void displayInfo ()
{
cout << "Movie title " << title << endl;
cout << "Director " << director << endl;
cout << "Year " << year << endl;
cout << "Run Time " << runTime << endl;
}
};
int main() {
MovieData movie1, movie2;
movie1.setInfo();
movie2.setInfo();
movie1.displayInfo();
movie2.displayInfo();
return 0;
}
I tried defining string variables as c-string, so character arrays, to see if that would help the issue, but it is the same.
Change this line
runTime;
to
cin >> runTime;
A couple of issues pointed out in the other comments and answers, but the main issue is this code:
cout << "Movie title: ";
cin >> title;
cout << "Director: ";
cin >> director;
Using >> on a string reads a single word only, so if you type Lord of the Rings, then title will equal "Lord" and director will equal "of", etc.
If you want to read a whole line of text then use std::getline():
cout << "Movie title: ";
getline(cin, title);
cout << "Director: ";
getline(cin, director);
When you start using getline(), it's very important to read (and understand) this post:
Why does std::getline() skip input after a formatted extraction?
This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
cin and getline skipping input [duplicate]
(4 answers)
Closed last year.
I was trying to make a program where the user is asked to give an input and it gives output with answered questions. Everything looks alright except that cin skipped my last question about school.
This is the original code:
//this program fills my data in profile
#include <iostream>
#include <string>
using namespace std;
int main () {
int age, exp_years;
char desired_grade;
string school_name, name;
const int year_grad = 4;
bool is_student;
cout << "You will need to enter your data for portfolio card" << endl;
cout << "Enter your last name" << endl;
cin >> name;
cout << "Enter your age" << endl;
cin >> age;
cout << "Enter your years of work experience" << "\n";
cin >> exp_years;
cout << "Is it true or false that you are a student (put 'true' or 'false')" << endl;
cin >> is_student;
cout << "Great! What school are you in (if you are not a student put desirable school)?" << endl;
cin >> school_name;
/* trying to make a function below */
cout << "Awesome, here is your info" << endl << "Your last name is "<< name << endl <<"You are "<<age << " years old." << endl << exp_years << " years of work experience" << endl;
if ( is_student == 1)
{
cout << school_name << " is lucky to have you!\n";
return 0;
} else {
cout << "Btw I am sure that " << school_name << " would be happy to have you as their student anytime\n";
return 0;
}
return 0;
}
I read some article and they said that getline() can help, so I tried to substitute with:
cout << "Is it true or false that you are a student (put 'true' or 'false')" << endl;
getline(cin, is_student);
cout << "Great! What school are you in (if you are not a student put desirable school)?" << endl;
getline(cin, school_name);
Hovewer, it gives me an error:
error: no matching function for call to 'getline'
What am I missing?
It seems the problem is related to entering a boolean value.
Here is shown how to enter boolean values
#include <iostream>
#include <iomanip>
int main()
{
bool is_student;
std::cin >> is_student; // accepts 1 as true or 0 as false
std::cout << is_student << '\n';
std::cin >> std::boolalpha >> is_student; // accepts strings false or true
std::cout << is_student << '\n';
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm a beginner in C++. I appreciate any suggestions in which could improve my code but what I'm really looking for is an explanation as to why my current code isn't returning what I want.
#include <iostream>
#include <string>
using namespace std;
struct MATRIX {
float values[1][1];
string names;
};
MATRIX get_matrix(string name, MATRIX m){
m.names = name;
cout << "Enter values of 2x2 Matrix:" << endl;
cin >> m.values[0][0];
cin >> m.values[0][1];
cin >> m.values[1][0];
cin >> m.values[1][1];
return m;
}
// end get_matrix1
MATRIX get_matrix2(string name2, MATRIX m2){
m2.names = name2;
cout << "Enter values of 2x2 Matrix:" << endl;
cin >> m2.values[0][0];
cin >> m2.values[0][1];
cin >> m2.values[1][0];
cin >> m2.values[1][1];
return m2;
}
int main(){
string testname;
MATRIX matrixtest;
string testname2;
MATRIX matrixtest2;
cout << "Name 1st Matrix:" << endl;
cin >> testname;
MATRIX result = get_matrix(testname, matrixtest);
cout << "Name 2nd Matrix:" << endl;
cin >> testname2;
MATRIX result2 = get_matrix2(testname2, matrixtest2);
cout << "[" << result.values[0][0] << "," << result.values[0][1] << "]" << endl;
cout << "[" << result.values[1][0] << "," << result.values[1][1] << "]" << endl << endl;
cout << "[" << result2.values[0][0] << "," << result2.values[0][1] << "]" << endl;
cout << "[" << result2.values[1][0] << "," << result2.values[1][1] << "]" << endl << endl;
}
It's supposed to return the name of the matrix as well as its values. Whenever it hits the name member in result it just stops the program.
EDIT: Updated to current code
You have to specify the number of elements, not the maximum index, to declare arrays in C++.
With this declaration
float values[1][1];
Only values[0][0] is avaliable.
The declaration should be
float values[2][2];
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This is the code :
#include <iostream>
using namespace std;
int main()
{
cout << " SSSSSSSSSSSSSSS" >> endl;
cout << " SS:::::::::::::::S" >> endl;
cout << "S:::::SSSSSS::::::S" >> endl;
cout << "S:::::S SSSSSSS" >> endl;
cout << "S:::::S" >> endl;
cout << "S:::::S" >> endl;
cout << "S::::SSSS" >> endl;
cout << " SS::::::SSSSS" >> endl;
cout << " SSS::::::::SS" >> endl;
cout << " SSSSSS::::S" >> endl;
cout << " S:::::S" >> endl;
cout << " S:::::S" >> endl;
cout << "SSSSSSS S:::::S" >> endl;
cout << "S::::::SSSSSS:::::S" >> endl;
cout << "S:::::::::::::::SS" >> endl;
cout << " SSSSSSSSSSSSSSS" >> endl;
cout << "IIIIIIIIII" >> endl;
cout << "I::::::::I" >> endl;
cout << "I::::::::I" >> endl;
cout << "II::::::II" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << "II::::::II" >> endl;
cout << "I::::::::I" >> endl;
cout << "I::::::::I" >> endl;
cout << "IIIIIIIIII" >>
}
This is for school so don't ask why I am writing this code. But i get a lot of errors like the one in the title and I can't seem to find the problem so if anyone can help me, it'll be great!
When writing using the extraction or insertion operator (e.g >> or << respectively) you need to basically point it where you want the flow to go, and not change the flow for that whole statement, to put it in simple terms, in your case you want both insertion/extraction operators that you use to be facing like so << and that one is called an insertion operator.
This is the code you want:
#include <iostream>
using namespace std;
int main()
{
cout << " SSSSSSSSSSSSSSS" << endl;
}
and replicate that for as many times as you want there to be output statements.
Keep the flow going one direction and one direction only:
std::cout << "..." << std::endl;
You've got this going in and out at the same time.
You can't read from std::cout, it's for output only. It's an ostream which means it has no idea what >> even is. That operator isn't defined which is why you get that error.
This question already has answers here:
Using getline(cin, s) after cin [duplicate]
(13 answers)
Closed 5 years ago.
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
void cpp_string();
void cstyle_string();
int main()
{
cpp_string();
cstyle_string();
system("pause");
return 0;
}
void cpp_string()
{
string fName, lName;
char grade;
int age;
cout << "What is your first name?";
getline(cin, fName);
cout << "What is your last name?";
getline(cin, lName);
cout << "What letter grade do you deserve?";
cin >> grade;
cout << "What is your age?";
cin >> age;
cout << "Name: " << fName << ", " << lName << endl;
cout << "Grade: " << grade << endl;
cout << "Age: " << age << endl;
return;
}
void cstyle_string()
{
char fNm[20], lNm[20];
char grade;
int age;
cout << "What is your first name?";
cin.get(fNm, 20).get();
cin.clear();
cout << "What is your last name?";
cin.get(lNm, 20).get();
cout << "What letter grade do you deserve?";
cin >> grade;
cout << "What is your age?";
cin >> age;
cout << "Name: " << fNm << ", " << lNm << endl;
cout << "Grade: " << grade << endl;
cout << "Age: " << age << endl;
return;
}
I'm getting output as
What is your first name?demiurge conon
What is your last name?no
What letter grade do you deserve?a
What is your age?22
Name: demiurge conon, no
Grade: a
Age: 22
What is your first name?What is your last name?What letter grade do you deserve?What is your age?Name: ,
Grade: ╠
Age: -858993460
Press any key to continue . . .
but if I run cstyle_string() in different file then I'm not getting any errors code works perfectly.
I want to know why this is happening?
There are two question.
Redundant \n
the state of cin
The last cin in cpp_string is cin >> age.
It will leave a \n not extracted.
In first of cstyle_string is cin.get(fNm, 20).get();
The delimiting character is not extracted from the input sequence if found, and remains there as the next character to be extracted from the stream
the cin.get(FNm, 20) will parse empty input before \n, and no characters are available in the stream in actually. In this case, the failbit flag will be set and next all cin >> operator will fail.
You can only call cstyle_string and press enter directly, the same thing will happen.