this how far i get in parsing but still have 3 error

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1289 Views - Last Post: 12 November 2012 - 09:35 AM Rate Topic: -----

#1 antwone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-April 12

this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 11:47 AM

hey guys;
I'm beginner in c++ and i was trying to understand both File I/O and parsing and this how far i get in parsing but i don't know how to solve those error i split the program in two parts the first for scanning and testing tokens and the other for reserved words
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main () {
  char c[10], str[256];
  ifstream is;
  int i=0;
  string s[10];
  char x=!' ';
  stringstream ss[10];
  string m[10];



  cout << "Enter the name of an existing text file: ";
  cin.get (str,256);

  is.open (str);        

  while (is.good())     
  {
    c[i] = is.get();       
    if (is.good())
      cout << c[i]<<endl;

	if(isblank(c[i])) //c[i]=' '
	{
		cout<<"white space"<<endl;
	}
	else
	{
		if(isalpha(c[i]))
		{
		cout<<"identfier"<<"& alpha"<<endl;
		ss[i] << c[i];

		ss[i] >> m[i];
		}
		else
			if(isdigit(c[i])
			{
				cout<<"identfier"<<"& digit"<<endl;
			}
	
		

	}
  		
	i++;
	
  }
  
  cout<<"\n";
  for(int k=0;k<=10;k++)
	  cout<<m[k];
  cout<<"\n";

  is.close();           
  
 vector< string > v; // get a vector to hold your words

    ifstream fin( "test.txt" );

    string line; // to hold each line you read from the file
    while( getline( fin, line ) )
    {
        istringstream iss( line ); // construct iss object
        string word;
        while( iss >> word ) // now get words on line into vector
            v.push_back( word );
    }


		string test[10],test1="help";

    // show all the words and test them to see if they match like comparing to get Reserved Words    
    for( size_t z = 0; i < v.size(); ++z )
	{
		
  
		v[z]=test[z];
        if(test[z]=test1)
			cout<<"found it"<<test[z]<<endl;
	}


  return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: this how far i get in parsing but still have 3 error

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,303
  • Joined: 25-December 09

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 11:49 AM

Quote

but i don't know how to solve those error


And the errors are? If they are compile errors post the complete error messages exactly as they appear in your development environment. If you don't have compile errors then ask specific questions based on the code you provided.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 antwone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-April 12

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 11:54 AM

sorry forget to post them.
error C2065: 'isblank' : undeclared identifier
and i check the function http://www.cplusplus...cctype/isblank/

error C2451: conditional expression of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' is illegal
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

and i didn't get the clue of this one.
Was This Post Helpful? 0
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 12:24 PM

Are you sure you have checked that link? The prototype of the function is int isblank ( int c ); and you are passing a char type to it if(isblank(c[i]))..


Besides that, this is not comparison if(test[z]=test1) its assignment operator, use == for comparison...
Was This Post Helpful? 0
  • +
  • -

#5 antwone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-April 12

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 12:36 PM

ok it give zero error put for the part two of the program
 vector< string > v; // get a vector to hold your words

    ifstream fin("test.txt");

    string line; // to hold each line you read from the file
    while( getline( fin, line ) )
    {
        istringstream iss( line ); // construct iss object
        string word;
        while( iss >> word ) // now get words on line into vector
            v.push_back( word );
    }


		string test[10],test1="help";

    // show all the words and test them to see if they match like comparing to get Reserved Words    
    for( size_t z = 0; i < v.size(); ++z )
	{
		
  
		v[z]=test[z];
        if(test[z]==test1)
			cout<<"found it"<<endl;
	}


the program didn't check the file i wrote in the( test.txt [ any help ] )
but i didn't get answer
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,225
  • Joined: 31-December 10

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 12:40 PM

View PostAKMafia001, on 11 November 2012 - 03:24 PM, said:

Are you sure you have checked that link? The prototype of the function is int isblank ( int c ); and you are passing a char type to it if(isblank(c[i]))..


Besides that, this is not comparison if(test[z]=test1) its assignment operator, use == for comparison...

The fact that it's a char doesn't matter, it will be converted promoted to an int. It's actually called "integral promotion". Here's a quote from "The Annotated C++ Reference Manual" page 31:

Quote

Conversions for the fundamental types are defined in C++ as they are in ANSI C. Basically, if an int can represent all the values of the original type, the value is converted to an int; otherwise it is converted to an unsigned int.

This works fine:
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
	const char* str = "Hello World";
	size_t len = strlen(str), i;
	
	for(i = 0; i < len; ++i)
	{
		if(isblank(str[i]))
		{
			printf("blank found at index = %ld\n", (long) i);
		}
	}
	
	return 0;
}


And here is the output:

Quote

$ ./ctypeTest
blank found at index = 5

This post has been edited by vividexstance: 11 November 2012 - 12:48 PM

Was This Post Helpful? 1
  • +
  • -

#7 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 12:48 PM

Well! Look what you have got here, declared array of strings string test[10] and then used it directly v[z]=test[z]; without assigning anything to it...
Was This Post Helpful? 0
  • +
  • -

#8 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,225
  • Joined: 31-December 10

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 12:51 PM

What's the point of the test array anyways? Why can't you just use the vector?
Was This Post Helpful? 0
  • +
  • -

#9 antwone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-April 12

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 12:54 PM

View PostAKMafia001, on 11 November 2012 - 12:48 PM, said:

Well! Look what you have got here, declared array of strings string test[10] and then used it directly v[z]=test[z]; without assigning anything to it...

when i tried to compare directly i get error ,so i used an intermediate ; can y give me more detail i didn't get what y said, sorry i still beginner .
Was This Post Helpful? 0
  • +
  • -

#10 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 12:55 PM

You are correct, Char is always implicitly promoted to integers (or unsigned integers) before evaluation, and is called widening.

That's what I read long ago...
Was This Post Helpful? 0
  • +
  • -

#11 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 01:02 PM

Well! I said that, the code below you wrote:
string test[10],test1="help";

   // show all the words and test them to see if they match like comparing to get Reserved Words    
   for( size_t z = 0; i < v.size(); ++z )
   {	
	v[z]=test[z];
       if(test[z]==test1)
		cout<<"found it"<<endl;
   }


On line 1, you have declared string test[10] which contains nothing of your use and later on line 6 you assign that nothing test[] to your vector v[]. I'm not talking about comparison here, its that you have got nothing in string test[10] then what are you assigning to v[] then?

Hope I'm clear enough now...
Was This Post Helpful? 0
  • +
  • -

#12 antwone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-April 12

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 01:09 PM

View PostAKMafia001, on 11 November 2012 - 01:02 PM, said:

Well! I said that, the code below you wrote:
string test[10],test1="help";

   // show all the words and test them to see if they match like comparing to get Reserved Words    
   for( size_t z = 0; i < v.size(); ++z )
   {	
	v[z]=test[z];
       if(test[z]==test1)
		cout<<"found it"<<endl;
   }


On line 1, you have declared string test[10] which contains nothing of your use and later on line 6 you assign that nothing test[] to your vector v[]. I'm not talking about comparison here, its that you have got nothing in string test[10] then what are you assigning to v[] then?

Hope I'm clear enough now...

I'm just trying to transfer the strings i have in the vector to array of strings to use it to compare with the reserved words i have later and here i used test1 as reserved word i want to know if it in the file or not .
Was This Post Helpful? 0
  • +
  • -

#13 antwone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-April 12

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 01:34 PM

View Postantwone, on 11 November 2012 - 12:36 PM, said:

ok it give zero error put for the part two of the program
 vector< string > v; // get a vector to hold your words

    ifstream fin("test.txt");

    string line; // to hold each line you read from the file
    while( getline( fin, line ) )
    {
        istringstream iss( line ); // construct iss object
        string word;
        while( iss >> word ) // now get words on line into vector
            v.push_back( word );
    }


		string test[10],test1="help";

    // show all the words and test them to see if they match like comparing to get Reserved Words    
    for( size_t z = 0; i < v.size(); ++z )
	{
		
  
		v[z]=test[z];
        if(test[z]==test1)
			cout<<"found it"<<endl;
	}


the program didn't check the file i wrote in the( test.txt [ any help ] )
but i didn't get answer

can someone tell me why it didn't check that part .
Was This Post Helpful? 0
  • +
  • -

#14 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,225
  • Joined: 31-December 10

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 02:27 PM

You can't see what's wrong with this for loop?
for( size_t z = 0; i < v.size(); ++z )

You can just use the vector like an array, so if(v[z] == test1) should work.

This post has been edited by vividexstance: 11 November 2012 - 02:28 PM

Was This Post Helpful? 0
  • +
  • -

#15 antwone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-April 12

Re: this how far i get in parsing but still have 3 error

Posted 11 November 2012 - 02:32 PM

View Postvividexstance, on 11 November 2012 - 02:27 PM, said:

You can't see what's wrong with this for loop?
for( size_t z = 0; i < v.size(); ++z )

You can just use the vector like an array, so if(v[z] == test1) should work.

i correct the loop but for for using the vector directly it give error
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2