Getting input in a String in cpp [closed] - c++

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 8 months ago.
Improve this question
Hello I want to get some input in a string in cpp and I am getting and error. Here is the code:
#include <iostream>
int main() {
using namespace std;
string name;
cout << "Type your name:";
cin >> name;
cout << "Your name is: " << name;
return 0;
}
I am building the project and I get this error:
Test1.cpp:10:6: error: invalid operands to binary expression
It is this line: cout << "Type your name:";
What am I missing here ? It is the first time when I am using c++

You need to include header <string>
#include <string>

<iostream> does not include <string>. Hence, you also need to include <string> in order to use std::string.
#include <iostream> // for std::cout, std::cin
#include <string> // for std::string

Related

request for member 'resize' in 'abs', which is non-class type 'char**' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
i have this code, and the compiler says in this line
abs.resize(jlh);
request for member 'resize' in 'abs', which is non-class type 'char**'.
edit:i need abs and mhs to be in char so i can encrypt in the next step.
what do i have to do? thanks!
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){
int jlh,x,y;
char **mhs=new char*[100];
char **abs=new char*[100];
cout<<"enter students total: ";
cin>>jlh;
abs.resize(jlh);
for(x=0;x<jlh;x++){
cout<<"enter students name: ";
mhs[x] = new char[1024000];
cin>>mhs[x];
cout<<"enter students presensi: ";
cin>>abs[x];
cout<<endl;
}
}
Like this
#include <vector>
#include <string>
int main()
{
std::vector<std::string> abs(100);
...
abs.resize(jlh);
...
}
For full details consult a book on C++. You really need one if you are going to learn C++. You cannot learn C++ by guess work.

Scanf throws a random exception when I try to use it(same for scanf_s) [closed]

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
Here is the code:
#include <iostream>
#include <stdio.h>
int main()
{
char name[15];
printf_s("What is your name: ");
scanf_s("%s",name);
printf_s("Nice to meet you, %s", name);
return(0);
}
Please help idk whats wrong. Im doing this in VS2019 and using c++ if that helps.
You can just use scanf instead of scanf_s, to silence the error you can write "#define _CRT_SECURE_NO_WARNINGS" on top of your code. Difference between scanf and scanf_s is, in scanf_s you can specify buffer size and control the size of an input to avoid crash. It's not necessary in this level but i suggest you to look into it.
Also, if you're using C++, you can declare strings like:
std::string varName, and cout/cin operations are way easier imo.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
int main()
{
char name[128];
printf("What is your name: ");
scanf("%s", &name);
printf("Nice to meet you, %s", name);
return(0);
}
Or in easier way:
#include <iostream>
int main()
{
std::string name;
std::cout<< "What is your name: ";
std::cin >> name;
std::cout << "Nice to meet you, " << name;
return 0;
}

Cannot print string with std::end [closed]

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 5 years ago.
Improve this question
I am having trouble with printing a string in C++.
I know there are lots of topics about this matter on SO, but most say to include <string>, <iostream> or namespace std. But I did all of that but still encounter the issue. Here is my code and the error.
#include <iostream>
#include <string>
using namespace std;
//...
void affiche_date(int annee, int nbjours) {
string mois;
if (nbjours>31) {
mois = "avril";
nbjours -= 31;
} else {
mois = "avril";
}
cout << "Date de Paques en " << annee << " : " << nbjours << " " << mois << end;
}
int main() {
int annee ( demander_annee() ) ;
int jour ( date_paques(annee) );
affiche_date(annee, jour);
}
Here is the error I get when I compile:
"error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘<unresolved overloaded function type>’)"
This error is coming from the line with the cout in the function I gave you.
I am using Geany on linux Ubuntu and using c++11.
Thanks for you help
std::end() is a function for getting an iterator to the end of a container.
You meant to use the std::endl stream manipulator instead.
Note: avoid using namespace std; in your actual code, either take advantage of using directives to bring in only what you need, or favor qualifying names with their namespaces, like I have here.

getline function is not defined error.Can't hold the veriable in string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#include <iostream>
#include <string>
using namespace std;
int main()
{
string message;
cout << "Type your message: ";
cin.ignore();
getline(cin, message);
return 0;
}
It gives getline function is not defined error.
I just want to hold the writed string inside the message veriable.
Here's the picture
"...string, stdafx.h and iostream...": "stdafx.h" must be the first line. The compiler will ignore everything above it (<string> will not be included, so the compiler will complain: getline not found).
I do not know what you're trying to do but the code below will compile and run:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string message;
cout << "Type your message: ";
cin.ignore();
getline( cin, message );
return 0;
}

c++ : invalid operands to binary expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I don't know what's wrong here ?
It's just running errors !!!
#include <iostream>
using namespace std;
int main()
{
cout << string("hello world");
return 0;
}
Read more about C++. So read first Programming -- Principles and Practice Using C++.
Then read C++ reference documentation, notably the one about std::string-s.
You need to #include <string>
You should enable all warnings when compiling. If using GCC, compile with g++ -Wall -g
You don't need that string before the actual string:
#include <iostream>
using namespace std;
int main()
{
cout << "hello world";
return 0;
}
Or, alternatively, if you're trying to store a string:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str = "hello world";
cout << str;
return 0;
}