C++ I need to understand where to use pointers and double pointers - c++

I have this basic multi tool program which goal is to complete four functions on a file that contains some strings.. take a string, for example, take a line and put it in uppercase.
I get that it's not perfect yet, but i need to understand why my char* and char** are doing some mess.
thanks a lot for your time
#include <iostream> //to use enter and exit
#include <cstring>
#include <string>
#include <fstream> //to use files
using namespace std;
/*--------------------------FONCTION PART-----------------------------------*/
//define one fonction for each transformation
//FONCTION 1
void fonctionu (char* argv){
char pattern = argv[2];
char file = argv[3];
fstream data(file, ios::in);
if (data){
string line;
while (getline (data ,line)){
unsigned found = line.find(pattern);
if (found != string::npos) {
for (int i = 0; line[i]!='\0'; i++) {
if(line[i] >= 'a' && line[i] <= 'z') {
line[i] = line[i] - (32);// (ASCII)
cout<<line[i];
}} }
if (found == string::npos) {
for (int i = 0; line[i]!='\0'; i++) {
cout<<line[i];
}}}}}
//FONCTION 2
void fonctiond( char* argv){ //remove line with xyz
char pattern = argv[2];
char file = argv[3];
fstream data(file, ios::in);
if (data){
string line;
while (getline (data ,line)){
unsigned found = line.find(pattern); //as a reminder argv[2] is the pattern (warning : it need to be an unsigned to compare two unsigned)
if (found != string::npos) {
//delete the line of the file when there is the pattern
cout<<'\0'; }
if (found == string::npos){
for (int i = 0; line[i]!='\0'; i++) {
cout<< line[i];
}}}}}
//FONCTION 3
void fonctionc( char* argv){
char pattern = argv[2];
char file = argv[3];
fstream data(file, ios::in);
if (data ){
string line;
while (getline (data ,line)){
unsigned found = line.find(pattern); //as a reminder argv[2] is the pattern
if (found != string::npos) {
for (int i = 0; line[i]!='\0'; i++) {
cout<< "\033[31m"<< line[i]; //the line will be red
}
}
if (found == string::npos){
for (int i = 0; line[i]!='\0'; i++) {
cout<< line[i];
}}}}}
//FONCTION 4
//replace the pattern xyz by abc
void fonctions ( char* argv){
char pattern = argv[2];
char file = argv[3];
fstream data(file, ios::in);
if (data ){
string line;
while (getline (data ,line)){
unsigned found = line.find(pattern);
if (found != string::npos) {
for (int i = 0; line[i]!='\0'; i++) {
if(line[i] >= 'a' && line[i] <= 'z') {
line[i] = line[i] - (97); // (ASCII) //we creat a shift that allow to make the x became a 'a' and the y a 'b'...etc
cout<<line[i];}}
}
if (found == string::npos) {
for (int i = 0; line[i]!='\0'; i++) {
cout<<line[i];
}}}}}
/*--------------------------MAIN PART---------------------------------------*/
int main(char* argv){
// ipsacs : argv[0]
//option :
string a = argv[1]; //string ! to compare line 103,108 and 112
// pattern : argv[2];
//file : argv[3];
if (argv[1]=='\0' || argv[2]=='\0'){
cout <<"ERROR"<<'\n';}
if (a == "u"){
char fonctionu (argv);}
if (a== "d"){
char fonctiond(argv);}
if (a == "c"){
char fonctionc(argv);}
if (a == "s"){
char fonctions(argv);}
return 0;
}
/*--------------------------TEST PART--------------------------------------*/
/*
pour compiler : g++ -Wall ipsacs.cpp -o ipsacs
./ipsacs u xyz foo //how the program would receive arguments, basically
Le programme ne renvoie rien.
*/
here is the compiling log:
main.cpp:22:30: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
fstream data(file, ios::in);
^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note: initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
basic_fstream(const char* __s,
^~~~~~~~~~~~~
main.cpp: In function ‘void fonctiond(char*)’:
main.cpp:46:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
fstream data(file, ios::in);
^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note: initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
basic_fstream(const char* __s,
^~~~~~~~~~~~~
main.cpp: In function ‘void fonctionc(char*)’:
main.cpp:66:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
fstream data(file, ios::in);
^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note: initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
basic_fstream(const char* __s,
^~~~~~~~~~~~~
main.cpp: In function ‘void fonctions(char*)’:
main.cpp:88:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
fstream data(file, ios::in);
^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note: initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
basic_fstream(const char* __s,
^~~~~~~~~~~~~
main.cpp: At global scope:
main.cpp:109:5: warning: first argument of ‘int main(char*)’ should be ‘int’ [-Wmain]
int main(char* argv){
^~~~
main.cpp:109:5: warning: ‘int main(char*)’ takes only zero or two arguments [-Wmain]
main.cpp: In function ‘int main(char*)’:
main.cpp:113:20: error: conversion from ‘char’ to non-scalar type ‘std::string {aka std::basic_string}’ requested
string a = argv[1]; //string ! to compare line 103,108 and 112
~~~~~~^
main.cpp:121:26: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char fonctionu (argv);}
^
main.cpp:123:25: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char fonctiond(argv);}
^
main.cpp:125:27: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char fonctionc(argv);}
^
main.cpp:127:27: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char fonctions(argv);}
^

A char * is a pointer to a memory address holding a character, and is often used for c-strings.
A char ** is a pointer to a memory address holding a pointer to a memory address holding a character, and is often used for arrays of c-strings.
Your main signature if not a valid form, since there's no form of main which takes in only a character pointer. The form you are most likely looking for is int main( int argc, int **argv ). This takes in two parameters: argc being the number of arguments passed to the program, and argv containing the parameters, as c-strings, passed to the program.
In the main method, you would generally ensure that argc is correct, i.e. you have the correct number of parameters passed into your function (note: it's always at least 1 will argv[0] being the program name).
std::string a = argv[1] is fine, and it will cause the string pointed to by argv to be converted to a string. When calling your functions, you have char functionu(argv);. You will want to remove the char from that line; effectively, the way it is written, you are trying to create a char variable named functionu which has an initial value of argv.
Each of your functions, instead of taking in a char * should take in a char ** instead, then when you have something like char file = argv[3];, you would want to change this to char *file = argv[3];, or std::string file = argv[3];

Related

I need help fixing a C++ white space detector

I need help fixing the following code, it is a c++ whitespace detector that uses different words to produce different outputs:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
string Line;
string firstWord[99];
string secondWord[99];
int lineFunction(string line) {
string line[199];
for (int i = 0; i < line.length(); i++) {
while (isspace(line[i]) == false) {
firstWord += line[i];
}
secondWord += line[i];
}
}
int main() {
cin >> Line;
lineFunction(Line);
if (firstWord == "A") {
if (secondWord == "B") {
cout << "AB";
}
}
}
The expected result when I input A B (with a space) should be the letters AB (without a space, to test how it works) printed onto the screen, as a result of the if statements, but I am trying to make the output configurable. The errors that I receive when I try to run this are:
main.cpp: In function ‘int lineFunction(std::string)’:
main.cpp:12:20: error: declaration of ‘std::string line [199]’ shadows a parameter
string line[199];
^
main.cpp:13:30: error: request for member ‘length’ in ‘line’, which is of non-class type ‘std::string [199] {aka std::basic_string [199]}’
for (int i = 0; i < line.length(); i++) {
^~~~~~
main.cpp:14:31: error: no matching function for call to ‘isspace(std::string&)’
while (isspace(line[i]) == false) {
^
In file included from /usr/include/c++/6/cctype:42:0,
from main.cpp:2:
/usr/include/ctype.h:118:1: note: candidate: int isspace(int)
__exctype (isspace);
^
/usr/include/ctype.h:118:1: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string}’ to ‘int’
In file included from /usr/include/c++/6/bits/basic_ios.h:37:0,
from /usr/include/c++/6/ios:44,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from main.cpp:3:
/usr/include/c++/6/bits/locale_facets.h:2565:5: note: candidate: template bool std::isspace(_CharT, const std::locale&)
isspace(_CharT __c, const locale& __loc)
^~~~~~~
/usr/include/c++/6/bits/locale_facets.h:2565:5: note: template argument deduction/substitution failed:
main.cpp:14:31: note: candidate expects 2 arguments, 1 provided
while (isspace(line[i]) == false) {
^
main.cpp:15:23: error: no match for ‘operator+=’ (operand types are ‘std::string [99] {aka std::basic_string [99]}’ and ‘std::string {aka std::basic_string}’)
firstWord += line[i];
~~~~~~~~~~^~~~~~~~~~
main.cpp:17:20: error: no match for ‘operator+=’ (operand types are ‘std::string [99] {aka std::basic_string [99]}’ and ‘std::string {aka std::basic_string}’)
secondWord += line[i];
~~~~~~~~~~~^~~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:24:22: error: comparison between distinct pointer types ‘std::string* {aka std::basic_string*}’ and ‘const char*’ lacks a cast [-fpermissive]
if (firstWord == "A") {
^~~
main.cpp:25:27: error: comparison between distinct pointer types ‘std::string* {aka std::basic_string*}’ and ‘const char*’ lacks a cast [-fpermissive]
if (secondWord == "B") {
Here are some possible solutions to fix the errors.
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
// don't use
// using namespace std;
// don't use global variables
// string Line;
// string firstWord[99]; you need strings, not arrays
// string secondWord[99];
// use references to return multiple values and no return value
// alternatively you could return a std::pair
void lineFunction(std::string line, std::string &firstWord, std::string &secondWord) {
// string line[199]; you don't need this variable
// line.length() return unsigned int so you should compare with unsigned int
// this whole loop makes no sense
// for (unsigned int i = 0; i < line.length(); i++) {
// this would result in a infinite loop
// while (isspace(line[i]) == false) {
// if (!std::isspace(line[i])) {
// firstWord += line[i];
// }
// secondWord += line[i];
// }
unsigned int i;
for (i = 0; i < line.length() && !std::isspace(line[i]); ++i) {
firstWord += line[i];
}
for (++i; i < line.length(); ++i) {
secondWord += line[i];
}
}
int main() {
std::string line;
// std::cin only reads until first whitespace
// std::cin >> line;
std::getline(std::cin, line);
std::string firstWord;
std::string secondWord;
lineFunction(line, firstWord, secondWord);
if (firstWord == "A" && secondWord == "B") {
std::cout << "AB";
}
}

Having problems with cstring arrays

I am having trouble with my code with something with arrays. I am getting the following errors
In function ‘int main(int, const char**)’:
75: error: cannot convert ‘char*’ to ‘char (*)[81]’ for argument ‘1’ ion(char (*)[81], OneItem*, int&, int&)’
In function ‘void parseInformation(char (*)[81], OneItem*, int&, in
164: error: ISO C++ forbids comparison between pointer and integer
166: error: incompatible types in assignment of ‘const char [2]’ to
169: error: ISO C++ forbids comparison between pointer and integer
174: error: ISO C++ forbids comparison between pointer and integer
174: error: ISO C++ forbids comparison between pointer and integer
176: error: invalid conversion from ‘char*’ to ‘char’
The code doesn't line up with the line numbers. I have tried multiple things googled some things still haven't found a solution.
const int MAX_CHARACTERS = 80;
const int MAX_INVENTORY = 12;
typedef char OneLine[MAX_CHARACTERS + 1];
struct OneItem
{
char product[MAX_CHARACTERS + 1];
int quantity;
float unitPrice;
float totalPrice;
};
int main( const int argc, const char* argv[] )
{
OneLine fileName;
ifstream inFile;
OneLine readLine;
OneItem inventory[MAX_INVENTORY];
int readLineIndex;
int structureCounter = 0;
int averageQuantity;
float averagePrice;
float averageTotalPrice;
displayIntroduction();
getFileName( argc, argv, fileName );
if (!inFile)
{
cout << "File not found: " << fileName << endl;
}
else
{
inFile.open(fileName);
while(!inFile.getline(readLine, MAX_CHARACTERS, '\n').eof())
{
if (structureCounter < MAX_INVENTORY)
{
parseInformation(readLine,inventory, readLineIndex, structureCounter);
}
}
void parseInformation(OneLine readLine[],OneItem inventory[], int & readLineIndex, int & structureCounter)
{
int tempIndex = 0;
int valueCounter = 0;
OneLine tempArray;
while(readLine[readLineIndex] != '\n')
{
tempArray = "\0";
while(readLine[readLineIndex] == ' ')
{
readLineIndex += 1;
}
while(readLine[readLineIndex] != ' ' && readLine[readLineIndex] != '\n')
{
tempArray[tempIndex] = readLine[readLineIndex];
tempIndex += 1;
readLineIndex += 1;
}
if(valueCounter == 0)
{
for(int i = 0; i <= strlen(tempArray); i++)
{
inventory[structureCounter].product[i] = tempArray[i];
}
valueCounter += 1;
}
else if(valueCounter == 1)
{
inventory[structureCounter].quantity = atoi(tempArray);
valueCounter += 1;
}
else
{
inventory[structureCounter].unitPrice = atof(tempArray);
structureCounter += 1;
}
}
return;
Your definition of parseInformation is wrong. It says that readLine should be an array of OneLine, but it only wants a single OneLine. It should be:
void parseInformation(OneLine readLine,OneItem inventory[], int & readLineIndex, int & structureCounter)
This is causing all the errors you're getting, because you're calling the function with a single OneLine, not an array. And inside the function you're comparing readLine[readLineIndex] with a character, which requires readLine to be an array of characters, not an array of OneLine.
The OneLine typedef makes this an array already, you didn't need to add [] to the parameter declaration.
As Barmar said you need to change first argument, but there is one more problem, here:
OneLine tempArray;
while(readLine[readLineIndex] != '\n')
{
tempArray = "\0";
You cannot make such assign. tempArray needs to be of type const char* or you need to make it this way:
tempArray[some_index] = '\0';

expected primary-expression before ‘const’ errors

Please help. I am getting many errors.
sub2.cpp: In function ‘int main()’:
sub2.cpp:11:14: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
sub2.cpp:12:14: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
sub2.cpp:16:17: error: expected primary-expression before ‘const’
sub2.cpp:16:36: error: expected primary-expression before ‘const’
sub2.cpp:11:6: warning: unused variable ‘outer’ [-Wunused-variable]
sub2.cpp:12:6: warning: unused variable ‘inner’ [-Wunused-variable]
make: * [sub2] Error 1
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
char *Subtract(const char *outer, const char *inner);
int main()
{
char outer = "Bookkepper";
char inner = "keep";
char *word = new char[50];
word = Subtract(const char &outer, const char &inner);
cout << word << endl;
return 0;
}
char *Subtract(const char *outer, const char *inner)
{
int olen = strlen(outer);
int first_occ_idx = -1;
for(int i=0; i < olen; i++){
if(strncmp(outer+i, inner,strlen(inner)) == 0){
first_occ_idx = i;
}
}
if(first_occ_idx == -1){
return NULL;
}
int ilen = strlen(inner);
int xx = olen - ilen;
char *newstr = new char[xx];
int idx = 0;
for(int i=0; i < first_occ_idx; i++){
newstr[idx++] = outer[i];
}
for(int i=first_occ_idx+ilen; i < olen; i++){
newstr[idx++] = outer[i];
}
newstr[idx] = '\0';
return newstr;
}
In C++, string literals like "Bookkepper" (sic) are const character pointers, it's a little stricter than in C. So it should be:
const char *outer = "Bookkeeper"; // Note also spelling
rather than:
char outer = "Bookkepper";
In addition, you don't include types when calling a function, so:
word = Subtract(const char &outer, const char &inner);
would be better as:
word = Subtract(outer, inner);
Separately (and these are style suggestions only), the correct type for things that represent sizes (such as number of characters in a string) is size_t rather than int.
And it's usually considered good form to clean up all your dynamic memory explicitly so, before returning from main(), you could put:
delete[] word;

I dont quite understand why i am getting this error concerning vectors

My program takes two input files, one of which is a list of commands. Each line of the file is a new command, so i want to parse this file into an array. I was told to use a vector as it dynamically grows which would eliminate the need for a function to count the number of lines in the file and then use that data to create an array.
Here is what my program is right now:
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
//Prototypes
int parseLines(ifstream& countfile);
vector<string> parseLines(ifstream& countfile)
//counts number of lines in file passed to function
{
string line;
vector<string> lines;
int numberOfLines;
//reads through each line until end of file putting each line in a sperate entry of the vector
while(getline(countfile, line))
{
lines.push_back(line);
}
return lines;
}
int main (int argc, char* argv[])
{
int i;
if(argc != 3) cout << "Usage: calendar.out datafile inputfile";
//Create input streams to both files
ifstream apptsfp;
ifstream inputfp;
//Open streams to both files
apptsfp.open(argv[1]);
inputfp.open(argv[2]);
if(!inputfp.is_open())
{
cerr << "failed to open input file\n";
return 1;
}
vector<string> inputVector;
vector<string> apptsVector;
inputVector = parseLines(inputfp);
for(i=0; i<= inputVector.size(); i++)
{
cout << "Input " << i << ": " << inputVector[i] << endl;
}
return 0;
}
And i am getting this long error when i compile and i dont quite understand it:
$ g++ calendar.cpp
calendar.cpp: In function ‘std::vector<std::basic_string<char> > parseLines(std::ifstream&)’:
calendar.cpp:17:46: error: new declaration ‘std::vector<std::basic_string<char> > parseLines(std::ifstream&)’
calendar.cpp:14:5: error: ambiguates old declaration ‘int parseLines(std::ifstream&)’
calendar.cpp: In function ‘int main(int, char**)’:
calendar.cpp:58:37: error: no match for ‘operator=’ in ‘inputVector = parseLines((* & inputfp))’
calendar.cpp:58:37: note: candidate is:
In file included from /usr/include/c++/4.7/vector:70:0,
from calendar.cpp:9:
/usr/include/c++/4.7/bits/vector.tcc:161:5: note: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = std::basic_string<char>; _Alloc = std::allocator<std::basic_string<char> >]
/usr/include/c++/4.7/bits/vector.tcc:161:5: note: no known conversion for argument 1 from ‘int’ to ‘const std::vector<std::basic_string<char> >&’
I feel like i don't quite understand how to use vectors.
Prototype
int parseLines(ifstream& countfile);
Realization
vector<string> parseLines(ifstream& countfile)
Return types differs.
Not the question asked but another error is
for (i = 0; i <= inputVector.size(); i++)
should be
for (i = 0; i < inputVector.size(); i++)
The problem is in int parseLines(ifstream& countfile); function declaration. There is a conflict with later vector<string> parseLines(ifstream& countfile) definition:
Change first with:
vector<string> parseLines(ifstream& countfile);
Besides, as this function definition is before its use, you can delete its declaration.

Trouble with file input [C++]

I was trying for the first time to read data from a file, a .txt file. Below is my code:
Level::ReadStream(std::fstream data)
{
bool write = false;
char* p = data.tellg();
while(!data.eof())
{
int i = 0, j = 0;
if(&p == '[')
{
write == true;
}
if(write == true)
{
mapX[i] = p;
mapY[j] = p;
}
if(&p == '/n')
{
i++;
j++;
}
else
{
i++;
}
*p++
}
};
void Level::SetMapSize(std::fstream data)
{
int* p = data.tellg();
int sizeX = 0;
int sizeY = 0;
while(!data.eof())
{
if(&p != '[' && &p != ']' && &p != '\n')
{
sizeX++;
}
else if(&p != '\n')
{
sizeY++;
}
}
mapX.resize(sizeX);
mapY.resize(sizeY);
std::cout << sizeX << '\n' << sizeY;
};
The goal of these two functions are to:
1- read all the chars in the file and, if the current character is not a bracket, add it to an index map(X, Y), then increase the counter so the next non-bracket value will be put in the correct index.
2- read all the file and, as before, count the non-bracket values, so it can make mapX and mapY the correct size.
However, it's not working, and I got these errors:
C:\...\Level.cpp|58|warning: multi-character character constant|
c:\...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\ios_base.h|790|error: 'std::ios_base::ios_base(const std::ios_base&)' is private|
c:\...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iosfwd|47|error: within this context|
c:...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iosfwd|87|note: synthesized method 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)' first required here |
c:...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\streambuf|770|error: 'std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private|
c:...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iosfwd|78|error: within this context|
c:...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iosfwd|87|note: synthesized method 'std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)' first required here |
C:...\Level.cpp||In constructor 'Level::Level()':|
C:...\Level.cpp|24|note: synthesized method 'std::basic_fstream<char, std::char_traits<char> >::basic_fstream(const std::basic_fstream<char, std::char_traits<char> >&)' first required here |
C:...\Level.cpp|24|error: initializing argument 1 of 'void Level::SetMapSize(std::fstream)'|
C:...\Level.cpp|29|error: 'cout' was not declared in this scope|
C:...\Level.cpp|38|error: ISO C++ forbids declaration of 'ReadStream' with no type|
C:...\Level.cpp|38|error: prototype for 'int Level::ReadStream(std::fstream)' does not match any in class 'Level'|
C :...\Level.cpp|17|error: candidate is: void Level::ReadStream(std::fstream)|
C:...\Level.cpp||In member function 'void Level::SetMapSize(std::fstream)':|
C:...\Level.cpp|75|error: invalid conversion from 'std::streamoff' to 'int*'|
C:...\Level.cpp|81|error: ISO C++ forbids comparison between pointer and integer|
C:...\Level.cpp|81|error: ISO C++ forbids comparison between pointer and integer|
C:...\Level.cpp|81|error: ISO C++ forbids comparison between pointer and integer|
C:...\Level.cpp|86|error: ISO C++ forbids comparison between pointer and integer|
C:...\Level.cpp|95|error: 'cout' is not a member of 'std'|
||=== Build finished: 15 errors, 1 warnings ===|
Can anyone help?
EDIT: Ok, I changed everything. My new code is:
void Level::ReadStream()
{
long p = textData.tellg();
int counter = 0;
int i = 0;
int j = 0;
char ch;
while(p != textData.eof())
{
textData.seekg(counter);
textData >> ch;
if(ch != '[' && ch != ']')
{
mapX[i] = ch;
mapY[j] = ch;
i++;
if(ch == '\n')
{
i = 0;
j++;
}
}
counter++;
p = textData.tellg();
}
};
with mapX and mapY being 5 int long. Now it compiles, but hangs and crashes. I don't see why... Is there anyone that can help me?
It's \n not /n! That should clear some errors.
Also, you can't just take the pointer from tellg -- it returns the position, not the pointer to the position!
Basically try to read up on IO in C++, maybe start with this question:
How to read from a file char by char in C++?