By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

Reading inputs to vectors

P: n/a
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:
Expand|Select|Wrap|Line Numbers
  1.     while( scanf("%f",&data) != EOF){            
  2.         index++;  
  3.         if(data != 0){
  4.             if(last_x == NULL) {     
  5.                  last_x = (eltype *) malloc(sizeof(eltype));        
  6.                  x->elements = last_x;
  7.                  last_x->data = data;
  8.                  last_x->index = index;
  9.                  last_x->elements = NULL;                 
  10.             }
  11.             else{
  12.                 last_x->elements = (eltype *) malloc(sizeof(eltype));
  13.                 last_x = last_x->elements;
  14.                 last_x->data = data;
  15.                                 last_x->index = index;
  16.                                 last_x->elements = NULL;
  17.                                             }}}
printing the inputs
Expand|Select|Wrap|Line Numbers
  1. void printVector (struct svector *x){
  2.     eltype * current_x = x-> elements;
  3. int i;
  4.     for(i=1; i <= x->size; i++){
  5.         if(current_x == NULL || current_x->index > i)
  6.             printf("0 ");
  7.         else {
  8.             printf("%f ", current_x->data);
  9.             current_x = current_x->elements;
  10.         }
  11.     }
  12.     printf("\n");
  13.     scanf("what up");    
  14. }
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 "listContainer" 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.
Nov 24 '10 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 8,916
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.
Nov 24 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.