Oh, and also, I'm not allowed to use strings when I read the names....Im supposed to read in one character at a time...
Oh, and also, I'm not allowed to use strings when I read the names....Im supposed to read in one character at a time...
Well, of course it doesn't. There's no call to printf, putchar, etc. There is nothing in that sample that actually prints anything. nonpuz gave you some sample code that will help you figure out what you need to do. It is not a copy-paste solution. We don't do that around here. We are here to help you learn, and that wont happen if we just hand out complete solutions.
Okay, then fgets is out. You will have to use fgetc. The documentation can be a bit intimidating at times. One tip for fgetc, make sure the variable you read into is declared as an int. fgetc returns an int, so it can return all possible char values, plus special values like EOF. Thus, a sample loop for reading a file with fgetc is:
I'm with Matticus here, you're coding all willy-nilly. You aren't taking the time to sit down and think through your problem before you start trying stuff. It's like trying to build an entire house without a single drawing or blueprint. You're bound to screw up, pour concrete in the wrong place, cut your lumber incorrectly, etc. The general process I follow for programming is something like:Code:int ch; while ((ch = fgetc(fp)) != EOF) // do something with ch
- Read the problem, and make sure you understand what it is asking
- Figure out how to solve this by hand, on paper. Ignore things like details of opening a file, whether to use while or for loops, etc. Come up with a solution in plain English, or whatever your native language is. Note, if you don't know how to solve this yourself, there is no way to program a computer to do it.
- Try out your solution. Run through some examples with paper and pencil. You can make a fake "file" with data, on a separate sheet of paper, and "read" through that one char at a time. Keep track of every little step you take, write it down. That will be the basis for your algorithm.
- Take those steps written down in English, and translate them to pseudo code. Psuedo code should have roughly the same basic structure as your actual code, but you get to ignore the details and some of the error checking for now.
- Once you have your pseudo code, begin turning it into real C code. Work in small chunks, write no more than 5-10 lines at a time, then test that. By test, I mean compile your code with maximum warnings, and fix all errors and warnings, and run your code to make sure it behaves the way you expect.
- If you encounter problems, fix them immediately, before you move on. That way, if an error pops up, you know it was in the last 5-10 lines of code, and it's easy to fix.
- Continue the previous two step until you have fully implemented your solution to the problem.
I suggest that, if you post anything back, it should be a description of how to solve the problem in English and/or pseudo code. We'll deal with the actual coding, and little issues like details of how fgetc works, once you have a working design.
I'm really sorry that I'm speeding through this, but it's due by today midnight, and I'm just really nervous -___- Thanks for clearing the fgetc thing out! I'm using that in my code now, so the getNames function works well. However, now I have a problem with my getSales function. this getSales function basically does the same thing as the getNames, only now it reads from a file of integers instead of characters, so I had to use fscanf in the getSales function...when I run it, the output looks like this:
instead of this:Code:2520252520222322252625223028252625304520 30252021272524262023242028262425301035322829303515161514121512192024201810151216 32303329000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000 Process returned 0 (0x0) execution time : 0.094 s Press any key to continue.
I don't see any problems with my code...how would i make it look like that??Code:4 25 20 25 25 20 22 23 22 25 26 25 22 30 28 25 26 25 30 45 20 30 25 20 21 27 25 24 26 20 23 24 20 28 26 24 25 30 10 35 32 28 29 30 35 15 16 15 14 12 15 12 19 20 24 20 18 10 15 12 16 32 30 33 29
here's my code:
THANKS FOR ALL THE HELP AND THANK YOU FOR BEING PATIENT WITH ME!!Code:#include <stdio.h> #define INPUT_FILE_GET_NAMES "names.txt" #define INPUT_FILE_GET_SALES "sales.txt" #define MAX_ROWS_NAMES 25 #define MAX_COLS_NAMES 20 #define MAX_ROWS_SALES 25 #define MAX_COLS_SALES 6 //Function Declarations int getNames(char namesArray[][MAX_COLS_NAMES]); int getSales(int salesArray [][MAX_COLS_SALES]); int main() { //Local Declarations char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0}; int salesArray[MAX_ROWS_SALES][MAX_COLS_SALES] = {0}; int i = 0; int j = 0; int row = 25; int col = 20; //Statements getNames (namesArray); getSales (salesArray); return 0; } int getNames(char namesArray[][MAX_COLS_NAMES]) { //Statements FILE *fp; int i, j, c; int col=20; int row=25; //open the sequential access file fp = fopen(INPUT_FILE_GET_NAMES,"r"); if(fp == NULL) { printf("Error, can't open file!!!\n"); exit(101); } while ((c=fgetc(fp)) != EOF) { putchar(c); } fclose(fp); for(i=0;i<row;i++) { for(j = 0; j < col; j++) { printf("%c", namesArray[i][j]); } } return 0; } int getSales(int salesArray[][MAX_COLS_SALES]) { //Statements FILE *fl; int i, j; int col=6; int row=25; //open the sequential access file fl = fopen(INPUT_FILE_GET_SALES,"r"); if(fl == NULL) { printf("Error, can't open file!!!\n"); exit(101); } else while((fscanf(fl,"%d",&salesArray[i][j]))!=EOF) //scanf and check EOF { for(i=0;i<row;i++) { for(j = 0; j < col; j++) { fscanf(fl,"%d\n", &salesArray[i][j]); printf("%d", salesArray[i][j]); } } } fclose(fl); return 0; }
Last edited by kal123456; 01-31-2013 at 02:41 PM.
First, indent your code properly. Messy code is hard to debug.
Then for example look at your row and col variables and tell us how many lines and how many columns are in your file.
There may be still other problems with your code.
Bye, Andreas
Ok, i'll indent it right now!
and in the getSales function, i wrote:so i made the max number of columns be 6, and the mas number of rows 25. I also defined MAX_COLS_SALES and MAX_ROWS_SALES at the very top of my program.Code:int col=6; int row=25;
what other problems might there be with my code?
Well, since it's due at midnight, you have 10.5 hours left, so that's plenty of time. And skipping the "figure out how to actually solve the problem" phase is never going to work. You'll never get there by mashing away at the keyboard, hoping some random attempt will magically solve your problem. I'll stress it again: programming is about problem solving. Writing code is secondary. Solve the problem, and much of the code "writes itself".
First, since you have your getNames function working, and getSales is very similar, consider using the same basic logic for both. Your getNames function basically does:
So why not follow the same logic for getSales? Just read the whole file into salesArray, then, when you're all done with that, print out the whole array.Code:while not at end of file read once character store it in namesArray for each row in namesArray for each column in namesArray print that char
Second, if you define constants at the top of your program, you should actually use them, everywhere. You only use them in some places. Instead of having local variables like row and col, just use the constants in your for loops:
Do likewise for MAX_COLS_SALES, and for your MAX_NAMES constantsCode:for (i = 0; i < MAX_ROWS_SALES; i++)
Lastly, take a look at the output you're getting:
Then, figure out how your output should look. Write it out on paper. You didn't give an example, so I'm guessing it should look something like this:Code:2520252520222322252625223028252625304520 30252021272524262023242028262425301035322829303515161514121512192024201810151216 32303329000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000
Describe, in English, the difference between what you actually see, and what you want to see. Something like: "The output I actually get is all the right numbers, but they're mashed together on one line. The output I want to see is...". That should be a huge clue to what you need to do to fix your program. Once again, you can't fix the problem if you don't understand it. Your first step must be to understand the problem.Code:25 20 25 25 20 22 23 22 25 26 25 22 30 28 25 26 25 30 45 20 30 25 20 21 ...
Ohh ok, that makes sense that i would rather use what I already defined then creating new integers, thanks! and I fixed up my code, I made the getSales function look exactly like the getNames function, with a few minor changes, and now it outputs the 4 columns correctly!! I need the first row and column (in this case, the number 16 for names.txt and number 4 for sales.txt) to be ignored and not printed out....how would I do that? would I do that in the functions, or in the main?
And yes, in the future I will try to start my assignments earlier so I can actually have some time to write my plan of action haha, it's just that it's due tonight and I only have 3 free hours cause the rest are class, so I'm REALLYYY nervous right now!!
here's my code right now:
Code:#include <stdio.h> #define INPUT_FILE_GET_NAMES "names.txt" #define INPUT_FILE_GET_SALES "sales.txt" #define MAX_ROWS_NAMES 25 #define MAX_COLS_NAMES 20 #define MAX_ROWS_SALES 25 #define MAX_COLS_SALES 6 //Function Declarations int getNames(char namesArray[][MAX_COLS_NAMES]); int getSales(int salesArray [][MAX_COLS_SALES]); int getTotalPersons(char namesArray[][MAX_COLS_NAMES]); int main() { //Local Declarations char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0}; int salesArray[MAX_ROWS_SALES][MAX_COLS_SALES] = {0}; int i = 0; int j = 0; int row; int col; //Statements getNames (namesArray); getSales (salesArray); getTotalPersons (namesArray); return 0; } int getNames(char namesArray[][MAX_COLS_NAMES]) { //Statements FILE *fp; int i, j, c; //open the sequential access file fp = fopen(INPUT_FILE_GET_NAMES,"r"); if(fp == NULL) { printf("Error, can't open file!!!\n"); exit(101); } while ((c=fgetc(fp)) != EOF) { putchar(c); } fclose(fp); for(i=0;i<MAX_ROWS_NAMES;i++) { for(j = 0; j < MAX_COLS_NAMES; j++) { printf("%c", namesArray[i][j]); } } return 0; } int getSales(int salesArray[][MAX_COLS_SALES]) { //Statements FILE *fl; int i, j, c; //open the sequential access file fl = fopen(INPUT_FILE_GET_SALES,"r"); if(fl == NULL) { printf("Error, can't open file!!!\n"); exit(101); } while ((c=fgetc(fl)) != EOF) { putchar(c); } fclose(fl); for(i=0;i<MAX_ROWS_SALES;i++) { for(j = 0; j < MAX_COLS_SALES; j++) { printf("%c", salesArray[i][j]); } } return 0; } int getTotalPersons(char namesArray[][MAX_COLS_NAMES]) { printf("%c", namesArray[0][0]); return 0; }
But don't you see that the data file you have shown us, only has 4 columns and 16 lines (omitting the first line)? Thus when you read in the numbers one after another you are out of sync (i.e. the position in the file doesn't correspond to the position in your array).
The first line tells you the number of columns in the file (4) and I assume your program should use this information.
In getNames() you are not storing the names in the array. You will need them if you want to create the final report.
Bye, Andreas
Just copying the broken function getNames() won't help.
Bye, Andreas
@AndiPersti Thanks, I actually got my program to print the table correctly! the only problem is that now I want the first element of the array not to be read...for example, the names.txt file looks like this:
I don't want the 16 at the top to be printed out...how do I ignore that in my program?Code:16 Kelly, Victor Lam, Gary Nagasake, David Nguyen, Jeff Nguyen, Michael Sinn, Scott Smith, Jacob Son, Thai Tavares, Maribel Tran, Diane Tsukamoto, Andrew Wang, Mary Young, Daniel Wells, Steve Wong, Justin Johnson, Mary
Read it into a variable, like num_rows. That is how many names there will be, and how many rows of data you will print out.
Now that I think about it, you need to read the number at the top of sales.txt into a variable too. That tells you how many columns of sales data you have for each person/row.
so would i do something like this?
since the first row and column of the array is 16 in this case? the teachers doesnt really want us to output the number of column of sales data I have. The number of columns was just given for our own reference.Code:int num_rows = namesArray[0][0];
No, simply use fscanf with the "%d" format specifier, to read into num_rows.
I JUST DID THAT AND IT WORKED!!! thanks so much!!