Why is g++ Giving Me Conflicting Errors While Compiling? [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 2 years ago.
Improve this question
I am attempting to compile my second, (still noobish) C++ program, and g++ is giving me these errors:
new.cpp: In function ‘int main()’:
new.cpp:10:4: error: ‘cin’ was not declared in this scope
cin >> name;
is the first. Here's the second:
^~~
new.cpp:10:4: note: suggested alternative:
In file included from new.cpp:1:
/usr/include/c++/8/iostream:60:18: note: ‘std::cin’
extern istream cin; /// Linked to standard input
^~~
and I believe these are telling me to change both ways to write it to the other. I have tried changing both, and I'm not sure how to fix this. Here is the program:
#include <iostream>
#include <string>
int main() {
std::string age;
std::string name;
std::cout << "Please input your age.";
std::cin >> age;
std::cout << "Please input your name.";
cin >> name;
return 0;
}
(CLOSED)

Here is a little bit of explanation for a c++ and g++ newbie:
new.cpp:10:4: error: ‘cin’ was not declared in this scope
cin is declared under the std namespace. See https://en.cppreference.com/w/cpp/io/cin
The second one is not an error, but a suggestion by the compiler by pointing to the alternative found by the compiler. It gives a hint about std::cin.
note: suggested alternative:
In file included from new.cpp:1:
/usr/include/c++/8/iostream:60:18: note: ‘std::cin’
extern istream cin; /// Linked to standard input
^~~
At line 10, you are using cin from the global namespace. Therefore, the compiler complains that it can't find the declaration of cin.
Our fellow already provided a fix for you by changing line 10 to: std::cin >> name;.
#include <iostream>
#include <string>
int main() {
std::string age;
std::string name;
std::cout << "Please input your age.";
std::cin >> age;
std::cout << "Please input your name.";
std::cin >> name;
return 0;
}

Related

Getting input in a String in cpp [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 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

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;
}

C++ Visual Studio "Non-standard syntax; use '&' to create a pointer to member" [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 have a ran in to this error (error C3867: non-standard syntax; use '&' to create a pointer to member) a couple of times. I know this question has been asked a lot of times, but I don't get why the problem happens and what I can do to fix it. I've read a lot of guides how pointers work and I've tried to play with the new knowledge, but I don't know how to do it correctly.
For this question I have made a simple code. Can someone help me understand why this error occurs and how to fix this code?
Error: error C3867: 'BankAccount::amountOfMoney': non-standard syntax; use '&' to create a pointer to member
Source.cpp
#include <iostream>
#include <string>
#include "BankAccount.h"
using namespace std;
int main(){
BankAccount bankAccount1("testName", 200.0);
cout << bankAccount1.amountOfMoney << endl;
}
BankAccount.h
#pragma once
#include <string>
using namespace std;
class BankAccount
{
public:
BankAccount();
BankAccount(string name, double money);
~BankAccount();
double amountOfMoney();
private:
string name;
double money;
};
BankAccount.cpp
#include "BankAccount.h"
BankAccount::BankAccount()
{
}
BankAccount::BankAccount(string n, double m) {
name = n;
}
BankAccount::~BankAccount()
{
}
double BankAccount::amountOfMoney() {
return money;
}
You forgot the function call operator (). Change your main code to:
int main(){
BankAccount bankAccount1("testName", 200.0);
cout << bankAccount1.amountOfMoney() << endl;
}
Without the parentheses it tries to print the address of a member function, which it is not able to do unless the function is not a member of a class.
If you want to call your member function, use brackets.:
cout << bankAccount1.amountOfMoney() << endl;

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;
}

How does stringstream work with <<? [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 7 years ago.
Improve this question
I am trying to use << as a means of moving integers into a stringstream. There must be something fundamental and basic I am overlooking. The simplest of code does not even compile:
std::stringstream ss;
ss << "simple test ";
produces this error:
error C2297: '<<' : illegal, right operand has type 'const char [13]'
That is not a valid C++ program.
First, you need to include sstream. Then, you need to put that expression with << into a function.
Like this:
#include <sstream>
int main()
{
std::stringstream ss;
ss << "simple test ";
}
This worked:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
stringstream ss;
string s;
ss << "simple test ";
s = ss.str();
cout << s;
return 0;
}