Reputation: 405
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;
}
Upvotes: 0
Views: 145
Reputation: 9314
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.
Upvotes: 1
Reputation: 2333
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
Upvotes: 1