Why are all cout's and cin's "undeclared identifiers - c++

In my main.cpp all of my cout's and cin's have errors.
/**
* Description: This program demonstrates a very basic String class. It creates
* a few String objects and sends messages to (i.e., calls methods on)
* those objects.
*
*/
//#include <iostream>
#include "mystring.h"
//using namespace std;
/* Function Prototypes */
void Display(const String &str1, const String &str2, const String &str3);
/*************************** Main Program **************************/
int main()
{
String str1, str2, str3; // Some string objects.
char s[100]; // Used for input.
// Print out their initial values...
cout << "Initial values:" << endl;
Display(str1, str2, str3);
My main.cpp cannot not be changed, so my question is, how can I fix this error, what do I have to add to my header file and implementation file?

In my main.cpp all of my cout's and cin's have errors.
You simply need to include <iostream> header file, and use std with cout and cin:
#include <iostream>
//^^
int main()
{
std::cout << "Initial values: "<< std::endl;
//^^
}

You have iostream header commented out here:
//#include <iostream>
You also need to add std::, this:
cout << "Initial values:" << endl;
should be:
std::cout << "Initial values:" << std::endl;
I see that you have using namespace std; commented out. I would advise against using namespace std;, it may save you some typing but it is considered bad practice and can cause problem later on.

Related

Calling a string getter function from a header file

I'm learning C++, and I'm just messing around with putting classes in separate files for practice. I have a getter function, which returns a string (because the variable is saved as a string). However, from my main() function, I am not sure how to call it. I know the problem is probably that I need to include string somewhere when I call the object, but I have no idea how to format it.
I know this is a pretty newbie questions, but I couldn't find the answer anywhere. Could someone help me out?
(p.s. I'm not trying to get this specific code to work, since it's useless. I'm just trying to learn how to apply it for future reference).
I've tried throwing in string in a couple of places when calling or creating the object, but I always get an error. I know I could get around it by not encapsulating the variable or not having a separate class file, but that's not what I want.
main.cpp
#include <iostream>
#include "usernameclass.h"
#include <string>
using namespace std;
int main()
{
usernameclass usernameobject;
usernameobject.getUsername();
return 0;
}
usernameclass.h
#ifndef USERNAMECLASS_H
#define USERNAMECLASS_H
#include <string>
class usernameclass
{
public:
usernameclass();
std::string getUsername();
void setUsername(std::string name);
askUsername();
private:
std::string usernameVar = "test";
};
#endif
usernameclass.cpp
#include "usernameclass.h"
#include <iostream>
#include "username.h"
#include <string>
using namespace std;
string usernameclass::getUsername(){
return usernameVar;
cout << "test cout" << endl;
}
usernameclass::askUsername(){
string name;
cout << "What is your name?" << endl;
cin >> name;
setUsername(name);
cout << "Ah, so your name is "+usernameVar+", great name I guess!" << endl;
cin.get();
cin.get();
cout << "You're about to do some stuff, so get ready!" << endl;
}
usernameclass::usernameclass(){}
void usernameclass::setUsername(string name){
string* nameptr = &usernameVar;
*nameptr = name;
}
Expected result: runs getUsername() function and returns usernameVar
Actual result: doesn't run the getUsername() function
The current code would not compile, because you have not specified return type of 'askUsername()' routine, which is 'void', I believe.
Other things are good, apart from an output in 'getUsername()', which happens after returning from the function and about which you should have received a warning, I guess.
To the question: you can call that 'get' method in 'main()' as:
cout << usernameobject.getUsername();
Your code should be structured more like this instead:
main.cpp
#include <iostream>
#include "usernameclass.h"
int main()
{
usernameclass usernameobject;
// optional:
// usernameobject.askUsername();
// do something with usernameobject.getUsername() as needed...
return 0;
}
usernameclass.h
#ifndef USERNAMECLASS_H
#define USERNAMECLASS_H
#include <string>
class usernameclass
{
public:
std::string getUsername() const;
void setUsername(std::string name);
void askUsername();
private:
std::string usernameVar = "test";
};
#endif
usernameclass.cpp
#include <iostream>
#include "usernameclass.h"
std::string usernameclass::getUsername() const {
return usernameVar;
}
void usernameclass::setUsername(std::string name) {
usernameVar = name;
}
void usernameclass::askUsername() {
std::string name;
std::cout << "What is your name?" << std::endl;
std::getline(std::cin, std::name);
setUsername(name);
std::cout << "Ah, so your name is " << getUsername() << ", great name I guess!" << std::endl;
std::cout << "You're about to do some stuff, so get ready!" << std::endl;
}

Cout and Cin throwing errors despite using headings #include<iostream> [duplicate]

Why I cannot cout string like this:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
When I do this, I get the following error:
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**
It is amazing, that even this is not working:
string text ;
text = "hello" ;
cout << "String is : " << text << endl ;
You need to include
#include <string>
#include <iostream>
You need to reference the cout's namespace std somehow. For instance, insert
using std::cout;
using std::endl;
on top of your function definition, or the file.
There are several problems with your code:
WordList is not defined anywhere. You should define it before you use it.
You can't just write code outside a function like this. You need to put it in a function.
You need to #include <string> before you can use the string class and iostream before you use cout or endl.
string, cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first.
Above answers are good but If you do not want to add string include, you can use the following
ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();
return os;
}
You do not have to reference std::cout or std::endl explicitly.
They are both included in the namespace std. using namespace std instead of using scope resolution operator :: every time makes is easier and cleaner.
#include<iostream>
#include<string>
using namespace std;
Use c_str() to convert the std::string to const char *.
cout << "String is : " << text.c_str() << endl ;
If you are using linux system then you need to add
using namespace std;
Below headers
If windows then make sure you put headers correctly
#include<iostream.h>
#include<string.h>
Refer this it work perfectly.
#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;
}

how to read a const char* from keyboard input and perform strlen() on it?

So I have been trying for 1.30 hour to get this to work. I am new indeed, but I have searched all over the place and couldn't find an exact answer. I do not wish to do this another way, as it would take away the entire purpose of learning to code. I have to find why this thing isn't working. I tried dozens if not hunderds of syntaxes, but nothing works.
I want to read in a const char* name, than count the number of elements in it, so I thought had to be strlen(), and than output the name and the number of elements. If that works I can write the rest of the code.
#include <iostream>
using namespace std;
int main()
{
//writing your name, and counting the characters including \0
int a;
const char* name;
a = int strlen(name);
cin.getline(name);
cout << name;
cout >> a;
return 0;
}
There are a lot of problems with your code.
You are not allocating any memory for cin.getline() to read into. const char* name; is declaring an uninitialized pointer to nothing. You have to allocate memory for name before you can then read any data into it.
cin.getline() expects two input parameters (a pointer to an allocated buffer, and the max number of characters the buffer can hold), but you are only passing in one value.
You are calling strlen() before you have read anything into name (and there is a syntax error on your strlen() statement anyway).
You are passing a to std::cout using >>, but std::ostream does not implement the >> operator. You have to use << instead.
And lastly, don't use using namespace std;.
Try this instead:
#include <iostream>
#include <cstring>
int main()
{
//writing your name, and counting the characters including \0
int a;
char name[32];
std::cin.getline(name, 32);
a = std::strlen(name);
std::cout << "You entered: " << name << std::endl;
std::cout << "It is << a << " chars in length" << std::endl;
return 0;
}
Or, if you really don't like using std:: everywhere, at least use using <identifier>; instead of using namespace std;:
#include <iostream>
#include <cstring>
using std::cin;
using std::strlen;
using std::cout;
using std::endl;
int main()
{
//writing your name, and counting the characters including \0
int a;
char name[32];
cin.getline(name, 32);
a = strlen(name);
cout << "You entered: " << name << endl;
cout << "It is " << a << " chars in length" << endl;
return 0;
}
Now, that being said, the preferred solution is to use std::getline() instead of cin.getline():
#include <iostream>
#include <string>
int main()
{
int a;
std::string name;
std::getline(std::cin, name);
a = name.length();
std::cout << "You entered: " << name << std::endl;
std::cout << "It is " << a << " chars in length" << std::endl;
return 0;
}
I found a working solution, although I don't see where I had gone wrong. But this does exactly what I want using const char* and strlen() without using std::string.
Thanks for all your help, you have all pointed me to the correct direction.
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main ()
{
const char *name;
int len;
name = "stephane";
len = strlen(name);
cout << name;
cout << len;
return(0);
}
As another user has pointed out, I think it's a good idea for you to take a few steps back and read the basics until you understand how pointers work.
A const char* is that: const. It could be used usually while doing things like this:
const char* cpName = "Stephane"; //expected not to change through the program's lifetime
char* pName = "Stephane"; //can be changed to point to something else
char *pOther = "Vada";
pName = pOther; //pName now points to the string "Vada"
cpName = pOther; //this won't compile as cpName is const

This pointer to print strings C++ [duplicate]

Why I cannot cout string like this:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
When I do this, I get the following error:
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**
It is amazing, that even this is not working:
string text ;
text = "hello" ;
cout << "String is : " << text << endl ;
You need to include
#include <string>
#include <iostream>
You need to reference the cout's namespace std somehow. For instance, insert
using std::cout;
using std::endl;
on top of your function definition, or the file.
There are several problems with your code:
WordList is not defined anywhere. You should define it before you use it.
You can't just write code outside a function like this. You need to put it in a function.
You need to #include <string> before you can use the string class and iostream before you use cout or endl.
string, cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first.
Above answers are good but If you do not want to add string include, you can use the following
ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();
return os;
}
You do not have to reference std::cout or std::endl explicitly.
They are both included in the namespace std. using namespace std instead of using scope resolution operator :: every time makes is easier and cleaner.
#include<iostream>
#include<string>
using namespace std;
Use c_str() to convert the std::string to const char *.
cout << "String is : " << text.c_str() << endl ;
If you are using linux system then you need to add
using namespace std;
Below headers
If windows then make sure you put headers correctly
#include<iostream.h>
#include<string.h>
Refer this it work perfectly.
#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;
}

Why I cannot cout a string?

Why I cannot cout string like this:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
When I do this, I get the following error:
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**
It is amazing, that even this is not working:
string text ;
text = "hello" ;
cout << "String is : " << text << endl ;
You need to include
#include <string>
#include <iostream>
You need to reference the cout's namespace std somehow. For instance, insert
using std::cout;
using std::endl;
on top of your function definition, or the file.
There are several problems with your code:
WordList is not defined anywhere. You should define it before you use it.
You can't just write code outside a function like this. You need to put it in a function.
You need to #include <string> before you can use the string class and iostream before you use cout or endl.
string, cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first.
Above answers are good but If you do not want to add string include, you can use the following
ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();
return os;
}
You do not have to reference std::cout or std::endl explicitly.
They are both included in the namespace std. using namespace std instead of using scope resolution operator :: every time makes is easier and cleaner.
#include<iostream>
#include<string>
using namespace std;
Use c_str() to convert the std::string to const char *.
cout << "String is : " << text.c_str() << endl ;
If you are using linux system then you need to add
using namespace std;
Below headers
If windows then make sure you put headers correctly
#include<iostream.h>
#include<string.h>
Refer this it work perfectly.
#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;
}