ok so what does this do..
1.) read the text file(most likely a c++ code in text form) line by line and display it line by line...
2.) (as the title says) check for syntax errors. ( only minor ones like missing ";" or a lack of closing braces)


#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

void main()
  ifstream readFile("check.txt");

    cout << "Please check the file's existance"<<endl;

  char read[999];

    readFile.getline(read, 999);
    if(readFile) cout << read << endl;



sadly.. I don't know how to verify if the file read was a semi-colon or a curly brace
so I don't know how to start at the error detecting stuff...

can you please help me :( any attention would be greatly appreciated
thanks in advance :)

7 Years
Discussion Span
Last Post by ravenous

Now you have to check the characters of each line...checking for things like if, while, do and then check the last printable character to see if it is ';'.


Is there a reason you're choosing to use a c-style char array instead of std::string ? If not, I would use std::string . You can then do this:

std::string lineFromFile;
while(infile.fail() == false){
    getline(infile, lineFromFile);

    /* Check things to do with the line */

    std::cout << lineFromFile << std::endl;

You don't have to worry about the memory management, and it makes it easier to, for example, check the last character of the line to see if it's a ';' using something like

if(*(lineFromFile.rbegin()) == ';'){
    /* Do things here */

Also, you shouldn't use void main() , use int main() instead :)


uhmm can I check per character entered using string?... cause I need to also check errors like
cin>>> or if(x==1 ?


uhmm can I check per character entered using string?... cause I need to also check errors like
cin>>> or if(x==1 ?

Yup. You can access the characters in a string in at least three ways:

/* make a new string and assign a value to it */
std::string str1 = "This is a string";

/* Make a string and initialise it via its constructor */
std::str2("This is another string");

/* Access the string via the "at" operator, this checks that you're not */
/* trying to access an element that doesn't exist, like -2, or 10000    */
char ch1 = str1.at(3);

/* Access an element like you would for a C-style char array */
char ch2 = str1[3];

/* Use an iterator (as you would a pointer in a C-sytle array) to get characters in sequence */
std::string::iterator it = str2.begin();
while(it != str2.end()){
    std::cout << *it << std::endl;

You can also "tokenise" a std::string by using a std::stringstream (this might also work with a C-style array, but I'm not sure):

/* Make a string */
std::string str("String streams are fun!");

/* Make a string stream and initialise it with the string */
std::stringstream str_stream(str);

/* Now split the string into words using the string stream */
while(str.good() == true){
    std::string word;
    str_stream >> word;
    std::cout << word << std::endl;

Hope that helps. You can read more about the functions that you can perform with std::strng here.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.