Stats: 3,191,493 members, 7,944,430 topics. Date: Monday, 09 September 2024 at 04:14 PM |
Nairaland Forum / Science/Technology / Programming / I Need Help On C (4462 Views)
Tutor On C# Needed / Help Needed On C# / I Need Training On C# And Java (2) (3) (4)
Re: I Need Help On C by babajeje123(m): 5:18am On Mar 13, 2017 |
Please, I need help on this c-code #include <stdio.h> Screenshot of my output file is attached. I don't know where the enclosed came from. Please, help
|
Re: I Need Help On C by stack1(m): 10:46am On Mar 13, 2017 |
It might have to do with the file contents, somehow, what fscanf is reading in isnt matching the requirements for in A and B, that is a mismatched type is been read, so garbage would be stored in A & B then printed out, either non-integers are being read or some kind of overflow is occuring, may be paste the content of the input file |
Re: I Need Help On C by babajeje123(m): 12:08pm On Mar 13, 2017 |
stack1:Thank you @ Stack. I have corrected the data type bug. However, I still don't get the output file well formatted. Here is the screenshot with the new codes #include <stdio.h> "matricno" and "MIT801" are missing in the output file.
|
Re: I Need Help On C by stack1(m): 12:04am On Mar 14, 2017 |
babajeje123: if the output isnt well formatted, uses spaces and tabs to adjust, also use printf's minimum-width specification |
Re: I Need Help On C by babajeje123(m): 9:11pm On Mar 15, 2017 |
I still need help o. I have read tried my best to resolve this output. Please, programmers in the house, guide me. |
Re: I Need Help On C by ChinenyeN(m): 5:15am On Mar 16, 2017 |
What exactly needs to be resolved? Please, be as specific as you can be. |
Re: I Need Help On C by babajeje123(m): 10:05am On Mar 16, 2017 |
@ChinenyeN. #include <stdio.h> "matricno" and "MIT801" are missing in the output file. [/quote] |
Re: I Need Help On C by ChinenyeN(m): 7:40pm On Mar 16, 2017 |
Babajeje123, looking at your code and the output screen shot from earlier, the first thing that comes to mind is how the data is stored in the Broadsheett.txt file. What sort of whitespace separates the fields? Is it a regular space or a tab or something else? Try replacing the blank space in your fscanf format string with \t and tell us what happens with the output. |
Re: I Need Help On C by babajeje123(m): 5:42pm On Mar 21, 2017 |
ChinenyeN: I did what you suggested but nothing was printed on the new file that was created. I think the problem is with fscanf and fprintf statements. More suggestion would be appreciated. |
Re: I Need Help On C by ChinenyeN(m): 5:43am On Mar 22, 2017 |
Quite honestly, babajeje123, I do not understand why you are having this particular problem. I just recreated your entire code almost down to the letter on my own machine and got the desired output. In other words, both matricno and MIT801 showed up. Attached are the screenshots. The first screenshot is the code, obviously. I essentially retyped your code. The only true difference is that I changed the file names for the sake of easy typing on my part. The second screenshot is the output after I compiled and ran the program. The third screenshot is the .txt file where the original data is stored. Because your fscanf function is using "%s %s %s %s", I figured that the .txt file must be stored in that same manner. So, everything is separated by a single space. As far as I can see, your code should get you the desired result. So, now I'm back to wondering how your Broadsheett.txt file is structured. babajeje123:This is surprising. According to the man pages for fscanf, the type and number of white space technically doesn't matter. Whether \t, \n or simply a single space, the output should still have been the same. The fact that nothing printed out to the new file raises suspicions. Earlier in this thread, you mentioned that you are working on the Windows platform with an IDE. I can't help but wonder now if this is an IDE/compiler issue as opposed to the code. It can't possibly be the code. I wrote and compiled your code in a Linux environment with gcc and the standard command line arguments. Everything runs as expected.
|
Re: I Need Help On C by ChinenyeN(m): 8:27am On Mar 22, 2017 |
Babajeje123, my suggestion to you, would be to not use fscanf. In place of fscanf, try fgets. I imagine that the records in the original data .txt file are all separated by a newline character. If so, fgets can be used to grab each record, and then strtok for instance can be used to tokenize each string in the record. It's more code, and it would require that other parts of your program be rewritten, but at least it gives you enough fine control to help ensure that all the desired fields are accounted for. You can still use fprint to generate the new file. There's no reason why that should change, because fscanf seems to be the real problem here, as far as I can tell. |
Re: I Need Help On C by babajeje123(m): 2:25pm On Mar 22, 2017 |
ChinenyeN:Okay. Thank you so much. I will try the suggestions and get back to you. |
Re: I Need Help On C by babajeje123(m): 7:50am On Mar 23, 2017 |
ChinenyeN:Everything is fine now. I just reformatted by input file by using a single space between each data and that was all. However, I have a similar issue when I changed the data type MIT801 & MIT802 int from char and introduced 'total' as int type. #include <stdio.h> What do you think I'm missing? Thanks once again.
|
Re: I Need Help On C by ChinenyeN(m): 6:37am On Mar 24, 2017 |
Well, babajeje123, the first line of your input file is technically headers. So, you'd have to read them in with the %s specifier, because the %d specifier results in an incorrect data type which then give you garbage values. Here's my recommendation. Consider reading the first line separately in some way. In other words, write your code to consider the header names, which are strings and not integers. Then, once the headers have been taken care of, you can switch to using the %d specifier and evaluate the rest of the input file. 1 Like |
Re: I Need Help On C by babajeje123(m): 10:39am On Mar 24, 2017 |
ChinenyeN:You are very smart! This is what I did and I got the desired output. I think I just need to be more calm, read more and wider than what we are being taught in class. I was thinking if you could be my c mentor. Is that possible ![]() |
Re: I Need Help On C by ChinenyeN(m): 4:38pm On Mar 25, 2017 |
I don't consider myself that great of a programmer or that smart of a person that I would be someone else's mentor. But, I don't mind being available when I can to help you out when you get stuck. It's usually just a matter of critical thinking. 1 Like |
Re: I Need Help On C by babajeje123(m): 4:21pm On Mar 26, 2017 |
Thanks bro. I've been able to generate the broadsheet with marks and grades. However, I think the lines of codes that generate the grades should be concise. Also, I need to calculate the GPA of each student. Do you have an idea of how to go about this? My new code is given below #include <stdio.h> When you run this, you'll have "assignment makes pointer from integer without a cast" as a warning. What does it mean?
|
Re: I Need Help On C by babajeje123(m): 4:22pm On Mar 26, 2017 |
ChinenyeN: |
Re: I Need Help On C by ChinenyeN(m): 7:11am On Mar 27, 2017 |
Babajeje123, the error comes from the fact that in your code, you're attempting to assign a type char (single character) to a type char * (string). In C, single quotes indicate single characters, i.e. 'B', while double quotes are for strings, i.e. "B". Some people might call the string a "string literal". Anyway, the point is that you have two different data types, because characters and strings are not the same thing in the C language. So, you've got three options to solve your problem. The first would be to cast the single characters from type char to type char *: i.e. grade2 = (char *) 'B'; You would have to do this for each line where you are assigning a char to a char *. I would not recommend this approach though, mostly because it is unnecessary. The second option would be to change your code by changing all single quotes to double quotes: i.e. grade2 = "B"; I would not recommend this either, mostly because it too is unnecessary. Just like the casting option, you would have to change each line where you are assigning a char to a char *. So, those two options aside, the issue can be taken care of far quicker with this last option. In simple terms, just change your earlier declaration from char * to char. i.e. basically remove the asterisk. char *grade, *grade2, *grade3, *grade4, *grade5; -> char grade, grade2, grade3, grade4, grade5; This is the option I would recommend. Not only will it affect every every line where you are assigning a char to char *, it will be much cleaner that code full of unnecessary casting. Plus, it is more sensible than changing every necessary line from single quotes to double quotes. Long story short, simply changing your char *grade, etc. declaration should take care of the compiler warning. |
Re: I Need Help On C by ChinenyeN(m): 7:21am On Mar 27, 2017 |
As for making your code more concise.... Considering that you are repeating the same if/else if conditions, why not write a loop statement that will contain the if/else if blocks and evaluate all the mit* values within the loop. This will effectively get rid of all the repeated if/else if blocks. |
Re: I Need Help On C by babajeje123(m): 1:22pm On Apr 02, 2017 |
ChinenyeN:Thank you for your help. I've been able to complete the assignment! Everything works perfectly. Do you think C is such a program to give too much attention? I'm just thinking of mastering it for exam sake and dump it when exams are over. |
Re: I Need Help On C by ChinenyeN(m): 5:26pm On Apr 02, 2017 |
Babajeje123, congratulations on finishing your assignment. It's good to know that the program compiles and executes as expected. About C, if you never intend on doing anything relatively low-level, then you likely will never have to worry about using C after your exam. There are other general purpose programming languages that will do the same thing as C, but with less lines of code. C shines more so as a bootstrapping language (the language that interpreters and compilers are sometimes initially written in), and it shines especially more so as a systems programming language (the language that kernels and OS's and OS utilities are mostly written in). If you don't have any need for any of these software engineering routes in the future, then you likely will have little need for C. It doesn't mean you can't use C for other things besides system programming. C is still a general purpose programming language. It can be used for practically anything. Aside from that, C still runs the world, and it seems like it will always be around for the foreseeable future. C is just so intertwined with current computer architecture that the only way for C to become irrelevant is if we start designing and building computers differently. Until then, there will always be a need for someone to pay attention to C. 1 Like |
Re: I Need Help On C by uzoexcel(m): 5:07pm On Apr 04, 2017 |
Take lecture vids on newBoston.org |
Re: I Need Help On C by babajeje123(m): 2:27am On Apr 05, 2017 |
Re: I Need Help On C by uzoexcel(m): 6:33am On Apr 05, 2017 |
babajeje123: thenewboston.com |
Re: I Need Help On C by uzoexcel(m): 6:33am On Apr 05, 2017 |
Re: I Need Help On C by celis(m): 7:51pm On May 02, 2017 |
thanks for this! Please i dont even know where to start from. I've been running from one error to the other all day. i can see you are reading from .txt . I taught the question says to read from excel file. Can you help a brother with the final code please? babajeje123: |
Re: I Need Help On C by babajeje123(m): 11:46am On May 03, 2017 |
celis:Yeah, we were asked to read from excel file but the output wasn't given me what I wanted. So, I had to use txt. With this code you quoted, you are 80% done. Try to figure out the rest. |
Re: I Need Help On C by babajeje123(m): 4:19am On May 09, 2017 |
ChinenyeN:I was attempting some mcqs and I encountered the following code blocks which left me confused. The first one #include <stdio.h>and the second one #include <stdio.h> Can you please explain why the answer to the first code would be 3 and not -3 and the second one, 43 and not 34? Thank you. |
Re: I Need Help On C by stack1(m): 1:14pm On May 09, 2017 |
Actually tboth code would give you varying results depending on compiler and architecture, it has to do with stuff C terms 1. Sequence points 2. Order of evaluation the explanation is long and twisted but please see this stack overflow post... http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points.. U mixed up your expectations tho; Then again its the second code that should give 3 and the first 43, heres y.. Second Code: j = i--- -2; would evaluate as or should in most cases, aslo it would be different on C99 and C11 compilers
the i has a post increment so remains 1, for the duration of that assignment -(-2) yields +2 so i +2 yields 3 First Code: If it yeilds 43, then the post increment wast performed first (as i noted such statements would yield erroneous/undefined behavior). Generally avoid stuff like this, and read the linked post thoroughly 1 Like |
Re: I Need Help On C by ChinenyeN(m): 3:22pm On May 09, 2017 |
And I will reiterate stack1's advice. Generally stay away from overuse or convoluted use of the increment and decrement operators. They are part of the list of C features that are "implementation dependent". That and for the sake of readability. Moderate use of the increment and decrement operators is alright though. They are still useful features. That aside, stack1 gave an excellent response. There's not much I have to say. 2 Likes |
Is Nairaland A Threat To Nairaland? / Java Internship Full Time / Naira Music - Nigerian Music App
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 104 |