C++ errors for C code -


2nd edit:

thank guys far!!! have corrected in terms of plain c assignment has written in. obviously, knowledge of c terrible. literally teaching myself c write this, using of c++ knowledge so. hence confusion of language literals. still having issues firstcheck() function though. explain directly below current code.

the firstcheck() function not working should. in readfile() function have split text given file array line line. firstcheck() should take array "mystring" , read first string until " " occurs (basically first word/character/etc.) can evaluate it.

updated assem.c

#include "assem.h"  int readfile(file *file, struct assem *item)  { size_t =0; item->counter = 0;                                                                      //this sort of constructor, if will.  size_t maxlines = sizeof item->mylines / sizeof *item->mylines;                                                                                         //breaks down text file line line  while(i < maxlines && fgets(item->mystring, sizeof item->mystring, file))               //and stores them "mylines array"     {         item->mylines[i] = strdup(item->mystring);         i++;         item->counter++;     }     return 0; }  void printfile(struct assem item)                                               //just printing things..prints new line in front {             printf("\n");     for(int s = 0; s < item.counter; s++)         {             printf("%s\n", item.mylines[s]);         }             printf("\n"); }  int firstcheck(struct assem item)                {     char *myword [7] = {null};      for(int s = 0; s < item.counter; s++)            {         while(strcmp( fgets( item.mystring, sizeof item.mystring, stdin ), " " ) != 0 )                      {                 myword[s] = strdup(item.mylines[s]);             }                    }          for(int s = 0; s < item.counter; s++)         {             printf("%s\n", myword[s]);         }  return 0;        } 

updated "assem.h"

#include <stdio.h>  #include <stdlib.h>  #include <string.h>  struct assem         {                 char mystring[101];                     //buffer                 char *mylines[20];                      //this store lines text file..up 20 lines                 int counter;                            //counter number of lines                                                         //printing stuff...prints file directly whats stored in array          };          int readfile(file *filetoberead, struct assem *item);       //takes in file needs read , splits lines         int firstcheck(struct assem item);         void printfile(struct assem item); 

updated "main.c"

#include "assem.c"      int main()     {         struct assem test;         file *mips;         mips = fopen("/home/rpf0024/cse2610/program1/mips.asm", "r");         readfile(mips, &test);         printfile(test);         firstcheck(test);         fclose(mips);         return 0;     } 

1st edit old: writing code getting errors im not sure how fix them. explanation these? errors within firstcheck() function attempts read string until first ' ' character in *mylines[] array. basically, trying read first word until space occurs, store in array can evaluate it.

here following errors: errors associated firstcheck() function way @ bottom of .c file..i have line numbers commented.

assem.c:35:31: error: iso c++ forbids comparison between pointer , integer [-fpermissive] assem.c:37:31: error: cannot convert ‘char**’ ‘const char*’ argument ‘1’ ‘char* strdup(const char*)

.c file

#include "assem.h"  int assem::readfile(file *file)  { int =0; counter = 0;                                            //this sort of constructor, if will.                                                          //breaks down text file line line  while(fgets(mystring, 101, file) != null)               //and stores them "mylines array"     {         mylines[i] = strdup(mystring);         i++;         counter++;     }     return 0; }  void assem::printfile()                 //just printing things..prints new line in front {             printf("\n");     for(int s = 0; s < counter; s++)         {             printf("%s\n", *(mylines+s));         }             printf("\n"); }  int assem::firstcheck()                  {     char *myword [7];      for(int s = 0; s < counter; s++)        //33         {         while(gets(*(mylines+s)) != ' ')    //35                         {                 myword[s] = strdup(mylines); //37             }                    } return 0;        } 

.h file

#include <stdio.h> #include <stdlib.h> #include <string.h>  class assem {      public:         char mystring[101];                     //buffer         char *mylines[20];                      //this store lines text file..up 20 lines         int counter;                            //counter number of lines         int readfile(file *filetoberead);       //takes in file needs read , splits lines         int firstcheck();         void printfile();                       //printing stuff...prints file directly whats stored in array  }; 

first of all, need decide whether you're writing c or c++. c not support classes, , should not use c-style strings, i/o, , memory management routines in c++ code. mixing elements of 2 languages recipe heartburn.

c , c++ different languages happen share lot of syntax , semantics, , well-written c program doesn't or behave well-written c++ program.

now obligatory rant out of way, 2 main type errors follows:

while(gets(*(mylines+s)) != ' ')  

gets (which should never never never use in either c or c++ for reason) returns value of type char *, character literal ' ' has type char (in c++; in c, has type int). char , char * different, incompatible types, , cannot comparisons between them.

your other type error

 myword[s] = strdup(mylines);  

mylines array of char *, in context "decays" type char **. strdup expects argument of type char *, hence error. need pass specific element of array strdup:

myword[s] = strdup( mylines[s] ); 

additionally, cannot compare c-style strings using == or != operators; must use library function strcmp, such as

while ( strcmp( buffer, " " ) != 0 )   ... 

note we're doing comparison against string literal " " instead of character literal ' '.

if want write c code, need change assem class struct, , move function delcarations outside of struct definition, , pass struct type argument functions:

/**  * .h file  */ #include <stdio.h> #include <stdlib.h> #include <string.h>  struct assem {     char mystring[101];      char *mylines[20];      int counter;                             };  int readfile(file *filetoberead, struct assem *item );        int firstcheck( struct assem item ); void printfile( struct assem item);                        

then need remove assem:: prefix function definitions , use struct assem argument:

int readfile(file *file, struct assem *item)  {   size_t = 0;   item->counter = 0;    /**    * number of array elements in item->mylines    */   size_t maxlines = sizeof item->mylines / sizeof *item->mylines;    while( < maxlines && fgets( item->mystring, sizeof item->mystring, file ) )    {     item->mylines[i] = strdup(item->mystring);     i++;     item->counter++;   }       return 0; }  void printfile( struct assem item )  {   printf("\n");   for(int s = 0; s < item.counter; s++)   {       printf("%s\n", item.mylines[s])); // use subscript notation; it's                                          // easier on eyes   }   printf("\n"); }  int firstcheck( struct assem item )                  {   char *myword [7] = {null};    for(int s = 0; s < item.counter; s++)           {     /**      * in original code, overwriting mylines array,      * contained read input file; i'm not sure      * want here, i'm using mystring instead.      */     while( strcmp( fgets( item.mystring, sizeof item.mystring, stdin ), " " ) != 0 )      {                                                               myword[s] = strdup(item.mystring);      }              }    /**    * @ point, you've dynamically allocated memory elements    * of mywords array, aren't using array outside of    * function , aren't freeing memory when you're done, meaning    * have memory leak.    *    * if mywords array doesn't need exist outside of function,    * need free each element before exiting.    */   return 0;        } 

and save .c file , compile c compiler such gcc.

if want write c++ code, should use fstream, string, , vector types instead of file , array types, , should take advantage of c++ features std::copy function template , stream iterators:

#include <string> #include <vector> #include <fstream> #include <iterator> #include <algorithm>  class assem {   public:      // don't need temporary mystring buffer      std::vector< std::string > mylines;      // since vectors know how big are, don't need separate     // counter attribute.      int readfile( std::istream& filetoberead );     int firstcheck( );     int printfile( ); };  int assem::readfile( std::istream& filetoberead ) {   /**    * use copy function template stream iterator     * read input file mylines vector    */   std::copy(      std::istream_iterator<std::string>( filetoberead ), // start      std::istream_iterator<std::string>( ),              // end     back_inserter( mylines )                            // destination   );   return 0; }  void assem::printfile( ) {   /**    * use same copy method write each string in mylines vector    * standard output (again using stream iterator), separated     * newlines.      */   std::cout << std::endl;  // write leading newline   std::copy(     mylines.begin(),                                          // start     mylines.end(),                                            // end     std::ostream_iterator< std::string >( std::cout, "\n" )   // destination   ); }  int assem::checkfirst( ) {   std::vector< std::string > mywords;    ( std::vector::size_type s = 0; s < mylines.size(); s++ )     std::cin >> mywords;    return 0; }  int main( void ) {   std::ifstream filetoberead( "myfile.txt" ); // or whatever name   if ( filetoberead )   {     assem myassem;     myassem.readfile( filetoberead );     filetoberead.close();     ...   }   ... } 

then save .cpp file , compile c++ compiler, such g++. notice c++ code don't need muck strdup or worry fixed array sizes, making code bit cleaner.

there other issues of program logic (what return values of readfile , checkfirst going used for, , 0 right value purpose), should @ least on right track.

hopefully.

edit

i realized logic in c++ checkfirst routine not correct translation of original code. know said shouldn't mix c-style strings , i/o in c++ code, don't have choice. here's should similar:

#include <cstring>         // c++ header file c-style string routines ...  char buf[n];               // n large enough input plus 0 terminator while ( std::cin.get( buf, sizeof buf ) && std::strcmp( buf, " " ) != 0 )   mywords.push_back ( std::string( buf ) ); 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -