structs ... homework proj

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wandafoda
    New Member
    • Jul 2006
    • 7

    structs ... homework proj

    Sorry guys....another homework question and if you don't wanna help out..i understand.
    just to say..im' a chemical engineer and we are required at will to do whatever...thas t why i have to take C++ and would like to also understand what im' doing.....to further my career..

    i'll include the program after the description

    So my teacher gave the class an algorithm we have to manipulate..... .its a struct and i understand what is going on in her program....but i have to make the program input a file 'fileInput.txt' and it follows this patter

    >4
    1 2 3 4

    the 4 means there are 4 variables...but thats my first problem......i do'nt konw how to make c++ read the '>' as a seperate variable then make the '4' be somthing else...there are no spaces between the '>' and the '4'.
    Secondly...i do'nt konw where in the program to define my variables.....i tried making them global variables (i might be wrong on that defintion)...an d i tried defining them in the ' int main() '

    i know what to normally do if it wasn't a struct.......bu t when i try to do that in this...i get about 40 errors.

    basically its ...
    ifstream errors.

    if you guys could help me out...thanx

    "wandafoda".... ..i'm a dude..but being shady incase my prfo check the internet...sinc e this isn't allowed
    here is the program.

    #include <iostream>


    struct node {
    int data;

    node* next;
    };

    node* insertAtHead(no de* head, int newitem) {
    node* n = new node;
    n->data = newitem;
    n->next = head;
    head = n;
    return head;
    }

    node* removeAtHead(no de* head) {
    node* del;
    if ( head != NULL) {
    del = head;
    head = head->next;
    delete del;
    }
    return head;
    }

    void printList(node* head) {
    node* ptr;
    for (ptr = head; ptr != NULL; ptr=ptr->next)
    std::cout << ptr->data << ' ';
    std::cout << std::endl;
    }

    bool isEmpty(node* head) {
    if ( head != NULL )
    return false;
    else
    return true;
    }

    int main( ) {


    node* head = NULL;

    head = insertAtHead(he ad, 4);
    head = insertAtHead(he ad, 2);
    head = insertAtHead(he ad, 3);
    printList(head) ;
    while ( !isEmpty(head) )
    head = removeAtHead(he ad);
    return 0;
    }
  • wandafoda
    New Member
    • Jul 2006
    • 7

    #2
    umm... i don't want the answer....if thats ok with you guys...just a suggestion or hint of waht to do...and then if/when i get it to work..i'll post my solution....sor ry again cuz i know this is a seroius programmer bored...but i figure this helps you guys remmeber some of the stuff that you might take for granted..
    thanks

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      OK here are some functions you may wish to use

      fopen - opens a file
      fclose - closes a file
      fgets - reads a line of data from a file
      strtol - converts a string containing digits into its binary equivilent (i.e. char [] to int)

      Remember if you have a string it's is just an array of characters which you can access individually

      i.e.

      char line[100]

      use fgets to read line

      if (line[0] == '>')
      {
      it's the number of numbers
      }

      Are those enough hints?

      Comment

      • wandafoda
        New Member
        • Jul 2006
        • 7

        #4
        just to clarify

        head = insertAtHead(he ad, 4);
        head = insertAtHead(he ad, 2);
        head = insertAtHead(he ad, 3);

        thats whats being manipulated.... its going to be a loop
        head = insertAtHead(he ad, var1);

        secondly...do i need to use pointers or can i do it how it is. thanx.
        I'm still playing around with the stuff in your post banfa..
        Last edited by wandafoda; Jul 21 '06, 05:42 PM.

        Comment

        • wandafoda
          New Member
          • Jul 2006
          • 7

          #5
          here is my int main segment
          i know there is a mistake in the loop, but could you tell me if i am using the fgets function and strtol function correctly...tha nx

          The errors i am getting flag at the pFile=fopen that part. so i need help wtih that too

          int main( ) {
          node* head = NULL;
          FILE * pFile;
          pFile=fopen('fi leInput.txt',"r ");
          char variables [10000];
          fgets(variables ,10000, pFile);
          char stuff;
          stuff= variables[1];
          int counter = strtol(stuff);

          int i;
          int var1;
          while(i=0, i<counter, i++)
          {

          var1= variables[i+2];
          head = insertAtHead(he ad, var1);
          }

          // head = insertAtHead(he ad, 2);
          // head = insertAtHead(he ad, 3);
          printList(head) ;
          while ( !isEmpty(head) )
          head = removeAtHead(he ad);
          fclose(pFile);
          return 0;
          }

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            ------------------------------------------------------------------------------------------------------------------
            pFile=fopen('fi leInput.txt',"r ");

            You've got the wrong quotes round the file name use

            pFile=fopen("fi leInput.txt","r ");

            ------------------------------------------------------------------------------------------------------------------
            char variables [10000];

            I would say that 10000 is slightly excesive I would have thought 1000 would be ample since commonly people don't put more than 80 - 100 chacaters on the line of a text file.

            ------------------------------------------------------------------------------------------------------------------
            fgets(variables ,10000, pFile);

            Should work but bad form, what if you change the size of variables, the code will not be robust to that change try this instead

            fgets(variables , sizeof variables, pFile);

            this will be robust to a change in size of variables as the compiler works the size out for you

            ------------------------------------------------------------------------------------------------------------------
            char stuff;
            stuff= variables[1];
            int counter = strtol(stuff);

            This is completely wrong the definition of strtol is

            long strtol ( const char * string, char** endptr, int radix );

            Forget the variable stuff you are going to want something that looks more like

            char *pEnd;
            int counter = strtol(&variabl es[1], &pEnd, 10);

            Comment

            • wandafoda
              New Member
              • Jul 2006
              • 7

              #7
              Wanted to say thanx again banfa.
              Incase anyone is interested, here is the final code....i just manipulated the int main portion. it might not be the most concise way to do it, but it worked.

              int main( ) {
              node* head = NULL;
              FILE * pFile; // used to store the variables that are inported
              pFile=fopen("li stInput.txt","r ");

              int i=0; //used as a counter

              int var1; // will hold the individual values after converted to int

              char variables [1000]; // creates an array to store the variables from teh file

              fgets(variables ,sizeof variables, pFile); // gets header line of the text file

              char *pEnd;

              int max = strtol(&variabl es[1], &pEnd, 10);
              // converts the second number in the array "variables"
              // into an integer to determine the number of elements in the text file




              fgets(variables ,sizeof variables, pFile); // gets the second row of numbers


              //the following loop converts the elements of the array, variables,into integers and puts them into the list

              while(i<max)
              {

              var1 = strtol(&variabl es[i], &pEnd, 10);
              head = insertAtHead(he ad, var1);
              // the following loop gets rid of some redundancy, for instance
              // if the first element of the text file was 777
              // variables[0] = 777, variables[1] = 77, variables[2] = 7, it also read the space as an element
              // and uses the previous value in that spot.
              // This loop also increases the value of "max", since 2nd integer might be at i=5.


              while(variables[i] !=' ')

              {
              i++;
              max++;


              }


              i++; // takes i to the point after the blank space

              }

              printList(head) ; // prints the list
              while ( !isEmpty(head) ) // removes elements from the list
              head = removeAtHead(he ad);

              fclose(pFile);// closes the file
              return 0;

              }

              Comment

              Working...