Thread: question

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    57

    question

    i am trying to write a program that stores words as strings into a linked list, i got it down to three errors but cant seem to fix them. here are what the errors say.......

    --------------------Configuration: cpp2d - Win32 Debug--------------------
    Compiling...
    Cpp2e.cpp
    c:\documents and settings\owner\cpp2e.cpp(169) : error C2440: 'initializing' : cannot convert from 'char' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    c:\documents and settings\owner\cpp2e.cpp(240) : error C2440: '=' : cannot convert from 'char [30]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    c:\documents and settings\owner\cpp2e.cpp(248) : error C2440: '=' : cannot convert from 'char [30]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    Error executing cl.exe.

    Cpp2e.obj - 3 error(s), 0 warning(s)


    and heres the code
    Code:
    #include <iostream>
    #include <fstream>
    #include <stddef.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string>
    
    using namespace std;
    
    struct StackFrame
    {
    	char data;
    	StackFrame *link;
    };
    
    typedef StackFrame* StackFramePtr;
    
    
    class search
    {
    public:
        bool check();  //declare the function check
        void output(StackFramePtr top); //declare the function output
        void input(search sight, char *file);
    	void input2(search sight, char *file, StackFramePtr top);
    	int a;
    	int b;
    	int total3;
        
    private:
        int total;//variable for total number of strings
    	int total2;//variable for number of characters in temp array
    	int i;//variable for number of strings in array
    	int j;//variable for number of characters in a string
    	int m;//variable for number of characters in temp string
    	int n;//variable for the insert loop
    	int s;//variable for ?
        ifstream in_stream; //create an instream
    	ofstream out_stream;//creat an outstream
    	char array[1000][30];//create an array
    	char temp[1000][30];//create an array
    	char next;//variable for next character in
        
    	
    };
    
    int main()
    {
    	char *name;
        search sight1;
    	sight1.a = 0;
    	sight1.b = 0;
    	sight1.total3 = 0;
    	StackFramePtr top;
    	for(int h = 0; h < 1; h++)
    	{
    		if(h == 0)
    		{
    		   name = "washington.txt";
               sight1.input(sight1, name);
    	       sight1.input2(sight1, name, top);
    		}
    		if(h == 1)
    		{
    			name = "adams.txt";
    			sight1.input(sight1, name);
    			sight1.input2(sight1, name, top);
    		}
    		if(h == 2)
    		{
    			name = "jefferson.txt";
    			sight1.input(sight1, name);
    			sight1.input2(sight1, name, top);
    		}
    		if(h == 3)
    		{
    			name = "madison.txt";
    			sight1.input(sight1, name);
    			sight1.input2(sight1, name, top);
    		}
    	}
    	sight1.output(top);
    	cout << " " << sight1.total3;
    	return 0;
    	
    
    }
    
    void search::input(search sight, char *file)
    {
    for(s = 0; s < 1000; s++)
    {
       for(m = 0; m < 30; m++)
    	   temp[s][m] = 'c';
    }
    for(i = 0; i < 1000; i++)
    {
    	for(j = 0; j < 30; j++)
    		array[i][j] = 'c';
    }
    s = 0;
    m = 0;
    total2 = 0;
    total = 0;
    i = 0;
    j = 0;
    in_stream.open(file);
    in_stream.get(next);
    while(! in_stream.eof())//while next is not end of file
    {
         //invariant: next is not the end of the file and next is a part of the file
    	
    		if(ispunct(next))
    			in_stream.get(next);
    		if(isspace(next))//if next is space
    		{
    			in_stream.get(next);/*//get next character from file
    			array[i][j] = '\0';//put null to end current string
    		    j = 0;//bring number of characters back to zero
    			i++;//start a new string
    			total++;//add one to total number of strings*/
    		}
    		else//if next is not a space
    		{
    			while(isalnum(next))
    			//invariant: next is either a letter or a number and next is a part of the file
    			{
    				temp[s][m] = tolower(next);//put next into temp array
    			    m++;//go to next place in temp array
    				total2++;//add one to total number of temp characters in temp string
    				in_stream.get(next);//get next character from file
    			}
    		
    			temp[s][m] = '\0';
    			m = 0;//bring back to first character in temp string
    			if(check())//if temp string already in array
    			{
    				in_stream.get(next);//get next character from file
    			    s++;
    			}
                
    	        else//if temp string not already in array
    			{
    				for(n = 0; n < total2; n++)//for every character in temp array
    				//invariant: n is less than total2 and has a place in the array
    				{
    					array[i][j] = temp[s][n];//add temp character to current place in array
    				    j++;//go to next place in current array
    				}
    				array[i][j] = '\0';
    			    in_stream.get(next);//get next character from file
    				i++;//start a new string
    				total++;//add one to total number of strings
    				total2 = 0;//reset number of characters in temp string back to zero
    				s++;
    				if(! in_stream.eof())
    					j = 0;
    			}
    		}
    	
    }
    in_stream.close();
    }
    
    void search::output(StackFramePtr top)
    {
        while(top != NULL)
    	{
    		string result = top->data;
    		StackFramePtr temp;
    		temp = top;
    		top = top->link;
    		delete temp;
    		cout << result;
    	}
    
    }
    
    bool search::check()//function defenition
    {
    	int sum = 0;//represents sum of coorect letters
    	int answer = 0;
    	int numstring;//represents string of array
    	int x;
    	int l = 0;
    	int f = 0;
        int totalchar;
    	if(total == 0)
    		return false;
    	for(numstring = 0; numstring < total; numstring++)//loops through all strings in array
    	//invariant: numstring is less than c and a[numstring][f] is part of an array
    	{   
    		totalchar = 0;
            for(f = 0; array[numstring][f] != '\0'; f++)
    		//invariant: a[numstring[f] does not equal a null character and a[numstring][f] is part of an array
    		{
    			if(isalnum(array[numstring][f]))
    				totalchar++;
    		}
            l = 0;
    		sum = 0;
    		x = 0;
    		while((array[numstring][l] != '\0') || (temp[s][x] != '\0'))
    		// a[numstring][l] and b[t][x] are both not null characters and they are both parts of arrays
    		{
    			 if(array[numstring][l] == temp[s][x])
    			 {
    			    sum++;
    				if(temp[s][x] != '\0')
    			      x++;
    				if(array[numstring][l] != '\0')
    				  l++;
    			 }
    		     else
    			 {
    				 if(temp[s][x] != '\0')
    				   x++;
    				 if(array[numstring][l] != '\0')
    			       l++;
    			 }
    		     if((sum == total2) && (x == l) && (totalchar == sum))
    		        answer = 1;
    		}
    		
    	}
    	if(answer == 1)
    		return(true);
        else
    	    return(false);
    	
    }
    
    void search::input2(search sight, char *file, StackFramePtr top)
    {
    	int j = 0;
    	array[i][j] = '?';//add a question mark to current place in array to signal end of array
        i = 0;//reset current string to first string
    	j = 0;//reset current character to first character
    	top = new StackFrame;
    	top->data = array[i];
    	top->link = NULL;
    	i++;
    	while(array[i][j] != '?')//while not end of array
    	//invariant: array[i][j] is not a question mark and array[i][j] is part of an array
    	{
    		StackFramePtr temp;
    		temp = new StackFrame;
            temp->data = array[i];
    		i++;
    		temp->link = top;
    		top = temp;
    	}
    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    String is not the same things as char[], and char[] is not the same thing as char. Data is a char, but you try and assign string result to equal it. You also try and assign data to equal array[i]. You need to make all of them char[]'s or strings. And if you use char[] you have to use string copy and not =. Or at least I hear anyways, I only really use strings

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    169 - like it says, you can't do that (string = char), use "+=" string operator instead.
    240,248 - you have type "char" on the left of "=", and type "char[30]" on the right - "char = char[30]" ain't right.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM