I am new to C++ and have researched this everywhere and cannot seem to figure out how to compile this and have not idea why. It works in visual C++ but not Xcode. The error seems to be on the input stream. Any suggestions?
Error reads - "Implicit instantiation of undefined template 'std::_basic_ifstream >'
#include <iostream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "The file is providing the data.";
ifstream myFile("/Users/me/Desktop/somewords.txt"); // * error
int i;
string s;
double d;
myFile >> i >> s >> d;
cout << "here is your data " << endl;
cout << i << endl << s << endl << d << endl;
return 0;
}
You forgot to #include <fstream>, the header file that actually defines all your ifstream goodness. You included <iostream> twice (or at least tried to), perhaps one of those was meant to be <fstream>?
Related
So I read a file in a function and set values to a class. I would like to read those same values in another function (another .cpp file) and I can't get it to work.
This is the code where I read values from .txt file. This seems to work. I can cout the value that I read.
#include "branjeDatoteke.h"
#include "parametri.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
void branjeDatoteke() {
Parametri pin[101];
string line;
ifstream myfile("pin.txt");
if (myfile.is_open())
{
for (int i = 0; i <= 100 && getline(myfile, line); i++)
{
pin[i].setPin(line);
// cout << pin[i].readPin() << endl;
//cout << line << '\n';
}
myfile.close();
// cout <<"tole more delat: "<< pin[2].readPin() << endl;
}
else cout << "Unable to open file";
}
And this is the code where I want to get the same values again, but cout is not working. I just get blank console where the cout should be.
#include <iostream>
#include "pin.h"
#include "parametri.h"
#include <string>
#include "branjeDatoteke.h"
using namespace std;
void pinPass() {
Parametri pin[101];
string pinKoda;
branjeDatoteke();
cout << pin[0].readPin() << endl;
cout << "Vnesite pin: ";
cin >> pinKoda;
for (int i = 0; i <= 100; i++) {
if (pin[i].readPin() == pinKoda) {
cout << pin[i].readPin() << endl;
cout << "KODA JE PRAVILNA" << endl;
}
else if (i > 100) {
cout << "kode ni v sistemu" << endl;
}
}
}
Assuming your Parametri class is correct, the issue is you are using local variables so they are initialised every time you call the function. They are allocated on the stack, locally for the calling function and can't be used outside of the function that declares them, at least not the way you're doing it. If you call the function twice you also have to assume all local variables must be reinitialised. One way you could solve this would be promoting your pin variable to global, like so:
// your_file_one.cpp
Parametri pin[101];
void PinPass() {
...
}
If you want to use it in another cpp file, then you have to redeclare the variable in the other file as well, like follows:
// your_file_two.cpp
extern Parametri pin[101];
The extern keyword specifies the variable was declared in another compilation unit - for simplicity let's imagine each C++ file which is not a header file as a separate compilation unit.
So your code will look like:
#include "branjeDatoteke.h"
#include "parametri.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
Parametri pin[101];
void branjeDatoteke() {
string line;
ifstream myfile("pin.txt");
if (myfile.is_open())
{
for (int i = 0; i <= 100 && getline(myfile, line); i++)
{
pin[i].setPin(line);
// cout << pin[i].readPin() << endl;
//cout << line << '\n';
}
myfile.close();
// cout <<"tole more delat: "<< pin[2].readPin() << endl;
}
else cout << "Unable to open file";
}
And
#include <iostream>
#include "pin.h"
#include "parametri.h"
#include <string>
#include "branjeDatoteke.h"
using namespace std;
extern Parametri pin[101];
void pinPass() {
string pinKoda;
branjeDatoteke();
cout << pin[0].readPin() << endl;
cout << "Vnesite pin: ";
cin >> pinKoda;
for (int i = 0; i <= 100; i++) {
if (pin[i].readPin() == pinKoda) {
cout << pin[i].readPin() << endl;
cout << "KODA JE PRAVILNA" << endl;
}
else if (i > 100) {
cout << "kode ni v sistemu" << endl;
}
}
}
There are better ways of using global variables than declaring them many times and you may want to research these if you're going to write bigger programs. Also global variables are very useful in certain instances but must not be abused as they can make bigger applications much more difficult to read and maintain.
The Parametri array in your pinPass function is empty(or more precisely , has garbage values).You call the branjeDatoteke function from within pinPass , the
branjeDatoteke function then creates it's own Parametri array (WHICH IS DIFFERENT from the one in your pinPass function),reads the values from the file and displays it via cout.
When branjeDatoteke is done with it's work , all the local variables of that function , inlcuding the Parametri array are destroyed and your program jumps back to the pinPass function.
To do what you're trying to achieve , which is , presumably , have a common array for both the functions, you can either pass the array from pinPass to branjeDatokete , or you can tell branjoDatokete to allocate an array on the heap and then return a pointer to it.I guess the first approach fits better for what you're trying to achieve.
Every time when I run the code Compiler give the error of object already define and I don't know where I am making mistakes in the whole code.
Even if I do this all in one file it is working but I don't know why its not working in this style can anyone help me that where I am doing mistake in this code.
Any help would be appreciated.
Thank you
student.h
ifndef STUDENT
define STUDENT
class Student
{
public:
char student_no[10];
char student_name[20];
char student_address[20];
char student_score[20];
Student();
};
Student::Student()
{//constructor
student_no[0] = 0; student_name[0] = 0; student_address[0] = 0;
student_score[0] = 0;
}
#endif
student.cpp
using namespace std;
#include "writestr.cpp"
#include <fstream>
#include <string.h>
#include <iostream>
int main(){
char filename[20];
Student s;
cout << "Enter the file name:" << flush;
cin.getline(filename, 19);
ofstream stream(filename, ios::out);
if (stream.fail()) {
cout << "File open failed!" << endl;
return 0;
}
while (1) {
cin >> s; // read fields of person
if (strlen(s.student_name) == 0) break;
// write person to output stream
stream << s; // write fields of person
}
return 1;
}
Problems occured
This is the portion where I write the streaming code.
writestr.cpp
using namespace std;
#include "readper.cpp"
#include <fstream>
#include <string.h>
#include <iostream>
ostream & operator << (ostream & stream, Student & s)
{ // insert fields into file
stream << s.student_name << s.student_no << s.student_address
<< s.student_score;
return stream;
}
readper.cpp
using namespace std;
#include "student.h"
#include <fstream>
#include <string.h>
#include <iostream>
istream & operator >> (istream & stream, Student & s)
{ // read fields from input
cout << "Enter Student Name, or <cr> to end: " << flush;
stream.getline(s.student_name, 30);
if (strlen(s.student_name) == 0) return stream;
cout << "Enter Student Name: " << flush; stream.getline(s.student_name, 30);
cout << "Enter Student Id Number: " << flush; stream.getline(s.student_no, 30);
cout << "Enter Address: " << flush; stream.getline(s.student_address, 30);
cout << "Enter Score: " << flush; stream.getline(s.student_score, 15);
return stream;
}
You are defining (not just declaring) the constructor in the header file:
Student::Student()
{//constructor
student_no[0] = 0; student_name[0] = 0; student_address[0] = 0;
student_score[0] = 0;
}
This defines the constructor (generates the code) again and again in every cpp that includes the header file. Since this definition has no inline keyword, it may exist exactly once in the program, not multiple times. Defining the non-inline constructor in multiple translation units (cpp files) causes the error.
Possible solutions:
Move the constructor definition into the class, or
Prefix it by an inline keyword, or
Move it to one of the cpp files
Another issue: you include cpp files, which cause a lot more issues by declaring the same thing again and again. Simply add them to the project/makefile/etc, instead of including:
#include "writestr.cpp"
So I'm trying to make a DoSer program with C++ using system commands. this is a work in progress and I'm pretty new to C++ and I'm trying to use the ping command with a variable in the same line, look at the code and you will realise what I want
#include <iostream>
using namespace std;
int main()
{
int targ;
system("color a")
;system("title C++ DoSer ")
;cout << " What Site/IP Is Your Target?" << endl;
cin >> targ;
system("ping");targ("-t -l 65500")
;return 0;
}
but it keeps saying "targ cannot be used as a function". please help
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string targ;
system("color a");
system("title C++ DoSer ");
cout << " What Site/IP Is Your Target?" << endl;
cin >> targ;
ostringstream pingData;
pingData << "ping " << targ << " -t -l 65500";
system(pingData.str().c_str());
return 0;
}
I'm new to C++, could someone please explain to me why I received the below errors when I do use "std::getline"? Here's the code:
#include <iostream>
#include <string>
int main() {
string name; //receive an error here
std::cout << "Enter your entire name (first and last)." << endl;
std::getline(std::cin, name);
std::cout << "Your full name is " << name << endl;
return 0;
}
ERRORS:
te.cc: In function `int main()':
te.cc:7: error: `string' was not declared in this scope
te.cc:7: error: expected `;' before "name"
te.cc:11: error: `endl' was not declared in this scope
te.cc:12: error: `name' was not declared in this scope
However, the program would run and compile when I used "getline" with "using namespace std;" instead of std::getline.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "Enter your entire name (first and last)." << endl;
getline(cin, name);
cout << "Your full name is " << name << endl;
return 0;
}
Thank you!
The errors are not from std::getline. The error is you need to use std::string unless you use the using namespace std. Also would need std::endl.
You need to use std:: on all the identifiers from that namespace. In this case, std::string and std::endl. You can get away without it on getline(), since Koenig lookup takes care of that for you.
#include <iostream>
#include <string>
int main()
{
std::string name; // note the std::
std::cout << "Enter your entire name (first and last)." << std::endl; // same here
std::getline(std::cin, name);
std::cout << "Your full name is " << name << std::endl; // and again
return 0;
}
You just needed to state the namespace for various elements that are in the std namespace (alternatively, you can remove all the std::s and place a using namespace std; line after your includes.)
Try this:
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "Enter your entire name (first and last)." <<
std::endl;
while(getline(std::cin, name))
{
std::cout <<"Your name is:"<< name << '\n';
}
return 0;
}
I am getting an error when I try to output the return value from one of my methods:
Error: No operator "<<" matches these operands. Operand types are: std::ostream << std::string
Main.cpp
#include <iostream>
using namespace std;
#include "Book.h"
int main()
{
book.setTitle("Advanced C++ Programming");
book.setAuthorName("Linda", "Smith");
book.setPublisher("Microsoft Press", "One Microsoft Way", "Redmond");
book.setPrice(49.99);
cout << book.getBookInfo(); // <-= this won't compile because of the error above.
int i;
cin >> i;
return 0;
};
Method which should return string:
string Book::getBookInfo()
{
stringstream ss;
ss << title << endl << convertDoubleToString(price) << endl;
return ss.str();
}
#include <string> is missing.
How did the code get the definition of string? The header <string> also declares the stream inserter.