Reading inputs to vectors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vic victorov

    Reading inputs to vectors

    Hello i am new to C programing and i have a problem that should be very easy to deal with.
    I need to make a program that read number from the command line and stores them in to a vector and prints them after. Furthermore when ever it reads a 0 as an input it needs to omit it. So far i have done this by:
    Reading the inputs:
    Code:
    	while( scanf("%f",&data) != EOF){            
    		index++;  
    		if(data != 0){
    			if(last_x == NULL) { 	
    				 last_x = (eltype *) malloc(sizeof(eltype));		
    				 x->elements = last_x;
    				 last_x->data = data;
    				 last_x->index = index;
    				 last_x->elements = NULL;				 
    			}
    			else{
    				last_x->elements = (eltype *) malloc(sizeof(eltype));
    				last_x = last_x->elements;
    				last_x->data = data;
                                    last_x->index = index;
                                    last_x->elements = NULL;
                                    			}}}
    printing the inputs
    Code:
    void printVector (struct svector *x){
    	eltype * current_x = x-> elements;
    int i;
    	for(i=1; i <= x->size; i++){
    		if(current_x == NULL || current_x->index > i)
    			printf("0 ");
    		else {
    			printf("%f ", current_x->data);
    			current_x = current_x->elements;
    		}
    	}
    	printf("\n");
    	scanf("what up");	
    }
    And this works fine. But i need when the user presses ENTER meaning anew line to store it in a different vector and keep a reference to it. I program with Java and that can be done easily by having a list "listContai ner" that can store lists inside. And once reading a newline command to create a new list and store it in the listContainer. Finlay i can print each list separately by traversing the listContainer and printing each item in the lists it contains. The problem is i don't know how can this be done in C or if it is possible to do it. Can someone help me please.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Using the word vector is slightly confusing since a std::vector is a standard data type in C++.

    However you have clearly already implemented a linked list of eltype all you need to do is expand this implementation so that you not only point to the next element in the list, last_x->elements, but you also have an additional pointer to the head of the next list.

    Or keep the current list exactly as is and implement a new head list that has pointers to both the head of the next list and the first element of this list.

    Generally in pure C casting the output of malloc is considered to be bad practice. If you need to do it that suggests you are actually compiling as C++ not C and if you are using C++ you may as well use std::vector or std::list and save yourself the hassle of implementing your own lists.

    Generally it is considered bad practice to not check the return value of malloc and make sure it has not returned NULL.

    Comment

    Working...