-1

I am writing a program that reads and writes to a .txt file with c++. Here is a partially edited sample of my code:

#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
#include <windows.h>
#include <sstream>
#include <algorithm>

//functions
template <typename T>
string num_to_string(T pNumber)
{
     ostringstream oOStrStream;
     oOStrStream << pNumber;
     return oOStrStream.str();
}
bool isdot(const char &c)
{
     return '.'==c;
}
//needed for string_to_num()

float string_to_num(string s)
{
     s.erase(remove_if(s.begin(), s.end(), &isdot ),s.end());
     replace(s.begin(), s.end(), ',', '.');
     stringstream ss(s);
     float v = 0;
     ss >> v;
     return v;
}

//code

string line = 10/20;
//the line taken from the .txt file
float add_numerator = 5;
float add_denominator = 10;
//these are values just for example

for(int i = 0; i < line.size(); i+= 1) {
     if (numerator_finished == false){
          if (line[i] != "/"){
               numerator += line[i];
          }else {
               numerator_finished = true;
          }
     }else {
          denominator += line[i];
     }
}
float numerator_temp = string_to_num(numerator);
float denominator_temp = string_to_num(denominator);
numerator_temp += add_numerator;
denominator_temp += add_denomitator;

numerator = num_to_string(numerator_temp);
denominator = num_to_string(denominator_temp);

string add_string = numerator + "/" + denominator;
//add_string is what the line in the .txt file is changed to with not shown code

If this code was run, it should be that add_string = "15/30". Yet it will not compile because of this line:

if (line[i] != "/"){

Because of this line this error appears:
"ISO C++ forbids comparison between pointer and integer [-fpermissive]"

I do not understand how this is a pointer and an integer when they are both strings.

Can this error be explained and a fix shown?

Andrew Feldman
  • 107
  • 1
  • 8

1 Answers1

0

I do not understand how this is a pointer and an integer when they are both strings.

They are not both strings! line is a string, but line[i] is a char.

And "/" is a character array (string literal).

Use '/' instead to get a character literal, which'll compare against a char very nicely.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055