tags:

views:

69

answers:

2

My instructor said the way to start this is to use the getline() function from out book, then get the numbers from the line, then have those numbers in matrix form, I do not understand why I would use getline?

//eventually this code should take in a square matrix and from 2x2 to 6x6 //the plan is to get it to read in a line, then get the numbers from the line, //then print out the numbers in a matrix form. That is the goal for today. //later I will try to get the actual matrix part working

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//error list for error checking will need later I guess (from my notes)
#define ENDOFFILE -1 
#define TOOMANYNUMS -2
#define LIMIT  256

//functions declared
int get_line(char line[], int);

//main
main(){
char line[255];
int num[6];
printf("Please input numbers %c: ", line);
get_line(line,LIMIT);


}

//functions 
  int get_line(char s[],int lim){
  int c, i;
    for (i=0;i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
  s[i] = c;
if(c=='\n'){
s[i]=c;
  ++i; 
}
s[i]='\0';
return i;
}
+1  A: 

getline is not just returning the lenth of the line, it's also copying the first line into the s parameter. So after your call of getline(line,LIMIT) (which doesn't btw, store the return value anywhere), the line variable will contain the first line.

Edit: I should also point out that your printf just above the call to getline is referencing the line variable, which is uninitialized and a char array, not a single character

Dusty
I am not sure what you mean, so I do not know how to fix this. I thought I knew what you meant and tried to fix it, but only got more errors.
pisfire
+1  A: 

The getline(char[], int) function reads characters from the console with getchar() and stores them in the array s[]. The array s[] points at the same memory as the line[] array in the main() function.

RD
@RD for testing purposed how do I print out the line I typed in? I called getline(), then: printf("%d",line); and I get the same output out no matter what is typed: 2673685.
pisfire
The variable line is a pointer to an array of memory locations containing chars. When you use %d with printf() you are asking for the integer value stored at line, which is 2673685. That is the memory address of the first location in your array. Try using %s or the function puts() instead.
RD
ok, Thank you. I understand and have it working now.
pisfire