• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Need help these questions

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help these questions. I didnt solve it.

How to write these in C programming language ?

Assume that you are given the structure below

typedef struct StudentMark { char name[20];
char surname[20];
int midterm;
int final;
}STUDENT_MARK;

1-) Write down a program which contains
a-) A function to get the user entered name, surname, and exam marks into dynamically allocated STUDENT_MARK structure (your function MUST check input validity i.e entered marks must between [0..100]).

b-) A function to write down the entered VALID structures into a file named as marks_YOUR_STUDENT_ID.txt. In this filename YOUR_STUDENT_ID part will be the programmer’s (i.e yours  student ID.

2-) Write a program which contains

a-) A function to read a file named as marks_YOUR_STUDENT_ID.txt which contains STUDENT_MARK structures’ data.

b-) A function to calculate the average of each student’s exam marks and writes the result onto screen as
“The student NAME SURNAME’s midterm mark is MIDTERM, final mark is FINAL and his/her average is AVERAGE”
NAME, SURNAME, MIDTERM, FINAL parts are the values read from file and the AVERAGE is the value calculated according to the formula (midterm * 0.4 + final * 0.6).

 
Marshal
Posts: 80143
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

We don't give out complete solutions, but please show us what you have achieved so far and we shall try to help you improve it.
 
Smith Kelly
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

We don't give out complete solutions, but please show us what you have achieved so far and we shall try to help you improve it.

 


This is my code. I couldnt complete this code. Thanks for your warnings

https://www.onlinegdb.com/ryqJb6E_8?fbclid=IwAR3rMN7fqWsPLujx5whyDnDQQ67LEeKzMXWlcqTnPRVKxB9lFULqDQuK1ns
 
Campbell Ritchie
Marshal
Posts: 80143
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the code here, using the code button and picking C++ from the dropdown list just to the right of the code button. Many people are reluctant to open such third party links.
 
Smith Kelly
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please post the code here, using the code button and picking C++ from the dropdown list just to the right of the code button. Many people are reluctant to open such third party links.





 
Campbell Ritchie
Marshal
Posts: 80143
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for using the code button, but you didn't quite get it right: [code=cpp] goes before the code and [/code] goes afterwards. Just this once, I have corrected it and doesn't it look better (), but you still need to tidy up your indentation and code formatting. It may seem fussy of me to go on about formatting, but you can hide all sorts of strange errors from yourself with bad formatting.
Why have you got two functions to print the student? Why does the function taking the pointer use s.xyz rather than ps->xyz? Does that code even compile? What has Karl got to do with it (line 17)?
Why has final got an underscore attached?
What do the braces {...} in line 28 mean? Does that bit compile?

Have you tried any reading from files or the keyboard, or writing to files? I can't see any of that in your code? Do you have a function to calculate the average from the two marks? I suggest, once you get that code to run, work out an average() function taking the two marks. Get that working and show us it, and enhance it to fulfil the instructions you were given. Then you can move on to the next stage.

When are you supposed to hand that assignment in?
 
Smith Kelly
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my  new code. Can you look again ? What is missing ? Can you help me ? thanks

 
Campbell Ritchie
Marshal
Posts: 80143
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Smith Kelly wrote:. . . Can you look again ?  . . .

No.

That code is illegible because of poor formatting.
 
Smith Kelly
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Smith Kelly wrote:. . . Can you look again ?  . . .

No.

That code is illegible because of poor formatting.



Thanks for your warnings.

Sorry for illegible.
Is it okay now ?  

 
Campbell Ritchie
Marshal
Posts: 80143
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Smith Kelly wrote:. . . Sorry . . .

Apology accepted . . . but


Is it okay now ? . . .

I am afraid, no. At least I can see where the individual functions start, but I can't see the structure of each function. Don't double‑space your code. Leave blank lines (probably only one empty line) between successive functions and after your #includes or typedefs. After each { move one indent's level to the right, and each } is one indent left of what preceded it. Maybe getting a decent text editor (for Windows® I like NotePad++) and setting its automatic indentation option and conversion of 1 tab → 4 spaces will make it easier for you. You will have no end of difficulty finding errors in your code if you format it badly.
 
Smith Kelly
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you look again ? Now it is good. I think you can read.
 
Campbell Ritchie
Marshal
Posts: 80143
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kindly don't edit posts which have been answered. Please post the corrected code in a new post.
Please don't quote the whole of a previous post.

Any improvement was minimal because you didn't indent your code.
 
Smith Kelly
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is good now. You can read it. If you dont want to help to me  You should say it. You can help a little.

 
Campbell Ritchie
Marshal
Posts: 80143
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Smith Kelly wrote:. . . If you dont want to help to me  You should say it. . . .

It more a case of, if you don't make the effort to provide code in a form suitable for people to help you, you won't get any help.
 
Smith Kelly
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Smith Kelly wrote:. . . If you dont want to help to me  You should say it. . . .

It more a case of, if you don't make the effort to provide code in a form suitable for people to help you, you won't get any help.




I spent a lot of effort but you don't want to help. This forum is  A friendly place for programming greenhorns! Big Moose Saloon  But you arent friendly. Thank you for not helping.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Smith Kelly wrote:I spent a lot of effort but you don't want to help.


How about let's all step back a little bit here. Smith, your code is horribly formatted and it still is illegible. Adding more spaces between lines doesn't help because the main problem with your code's legibility is the lack of proper indentation.

Please go to https:// codebeautify.org/c-formatter-beautifier https://www.techiedelight.com/tools/clike , copy-paste your code into the left window, click on the "Beautify / Format" button in the middle, and then copy-paste the formatted code from the right window back here. Then maybe that will show the kind of effort Campbell has been asking for. Until then, please don't insist that the code you have posted so far is readable because it just simply isn't in its current state.

Edit: Actually, of all the ones I tried when I searched for "Online C code formatter", this one seems to give the best results: http://format.krzaq.cc/
 
Smith Kelly
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand you. Is it okay now ? . We had a communication problem. Sorry for not understanding.  I have been working with this code since morning and need help. Can you read it ?


 
Campbell Ritchie
Marshal
Posts: 80143
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a lot better. I would compress lines 16‑21 like this, however:-

Now, what happens when you run that code? What happens to line 100? Does it compile? I couldn't get it to compile; my gcc couldn't find itoa(...). Why are you using so many lines to print into the file? Is there something like fprintf(), which will allow you to print everything into the file in one instruction? Can you write
fprintf(file, "%s, %s, %d, %d\n",
     s->name, s->last, s->midterm, s->final);
or something like that?
Is everything else working? Did you work out how to calculate the averages?
 
Rancher
Posts: 508
15
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Is there something like fprintf(), which will allow you to print everything into the file in one instruction?


Also, in the loop conditions you have (mark != NULL). It looks like you think mark will become NULL when it goes off the end of the marks[] array, but it won't - C is too low level for that. The only way it can go NULL is if something sets it to NULL, and your code isn't doing that.

I can see you check marks for NULL at the top of the function, which is good. But I would suggest negating the if() so that you have:
which removes a load of indentation. There's a few other places where you could do the same sort of thing.

And:
would be better as:
No need for the intermediate void* variable, and use the size of what the pointer is pointing at instead of the size of a type which might or might not be the correct type (code safety).

Move the mark variable definition above the checks for name/surname length, to allow you to replace:
with:
Code safety again - use the actual size of where the string is going to be stored, rather than a constant which might or might not be the same thing. And note it must be >=, not > - a C string ends in the \0 string terminator character which isn't included in the value returned by strlen(). That is, strlen("") = 0, but "" needs 1 byte of storage to hold the \0.
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
should be:
You want to avoid hard-coded values in output messages as well as the code. And you need the \n at the end of the string because printf() doesn't add one. It's possible that you won't even see the error message without it - sometimes an OS will buffer output until it can output a whole line.
 
Smith Kelly
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your helping.

Yes it is working. It wrote the file but I couldnt write average. I'm so confused .

The program wrote to the file  -> ( smith,merth,12,45 )


I can't compile the program according to what you say.

Can you please ? change the error of the code I wrote to test and understand
can you write here ?    The program writes to the file. What is missing from the desired location according to the question? I've been working with this for a long time.

C programming is very hard to learn.

 
Campbell Ritchie
Marshal
Posts: 80143
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Smith Kelly wrote:. . . C programming is very hard to learn.

Programming in other languages is just as difficult.

Please explain what is working and what is going wrong. What error messages are you receiving? How are you calculating the average?
 
Smith Kelly
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The program is working and  wrote the file like this  -> ( smith,merth,12,45 ) but I didnt calculate avarage and The program didnt write anything to console.  Can you write my mistakes and write full correct  code here ? I am going to look what is my mistakes.
I am so confused. Please help me
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like what you've done so far is basically ok - it's creating the file consisting of one entry with the values you've specified in the createStudentMark() call in main(). It's missing part of 1a - it should prompt the user for the values instead of just hard-coding them in the function call ie. output "ID:" and wait for the user to enter their ID, output "Name:" etc.

It's not writing anything to console because there's no code in there that writes to console (apart from error messages).

What you need to do next is write some code to read and process the file you've created. So fopen() the file for reading, and probably use scanf() to read in the data into a STUDENT_MARK structure. Then use the formula provided in the question to calculate the average and output to console.
 
Smith Kelly
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 I didnt understand what do you mean. Can you write correct code(program) here ? I am going to look what is my mistakes.If you write here  for correct full code. I can understand easier . I couldnt complete my code
 
Smith Kelly
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//============================================================================
// Name        : StudentInformationFile.cpp
// Author      :
// Version     :
// Copyright   :
// Description : Hello World in C++, Ansi-style
//============================================================================

I did the program. Program is working with c++ editor But I have to compile with Codeblocks and I have some errors in the program.

Error are like these . Can you help me
I couldnt use Img button
https://hizliresim.com/DHyVXN


 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Smith Kelly wrote:I couldnt use Img button
https://hizliresim.com/DHyVXN

I don't see anything there, apart from 'error3' which isn't very helpful

However, if I build it using g++ in Linux I do get:
That's because of the NULL at the end of the printf(); you just need to remove it.

Having done that, it builds and runs, and I can enter my name and marks. But then it crashes ('segfault'), and that's due to this:
Your buf1[] array is only size 1, but you are trying to print a string such as "42" into it, which requires 3 bytes of storage ('4', '2', '\0'). Same problem with buf2[].

However, as Campbell pointed out a while ago, you don't need to do all those fputs(); just do a single fprintf() eg.
 
Campbell Ritchie
Marshal
Posts: 80143
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Smith Kelly wrote:. . . I couldnt use Img button . . .

Please don't. Many people are reluctant to open unidentified links like that. Please copy'n'paste the text from your terminal window or IDE. Those error messages all look pretty straightforward.
What does line 6 mean? You were told to use typedef; why didn't you write typedef? You have to follow the instructions strictly if you want good marks, even though I think that is over‑specifying the assignment. I think it would be better to let you work out the type of the struct for yourself.
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What does line 6 mean? You were told to use typedef; why didn't you write typedef?

It's C++, in which you don't need to use typedefs for structs. But as you point out, the question did say use typedef, and in fact the OP asked:

How to write these [programs] in C programming language ?

To the OP - does it need to be in C? If so, you need to remove/change the few bits of C++-specific code.

Another point - the question asked for separate programs; one to create the marks file, and one to read/process it. Shouldn't be too difficult to split your single program as required.
 
Campbell Ritchie
Marshal
Posts: 80143
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:. . . in fact the OP asked:

How to write these [programs] in C programming language ?

. . .

Yes, and however bad my C is, my C++ is so much worse I wouldn't have gone near this question otherwise.
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have received some good advice.  Also, it has been a good two months since you posted your original question.   I just have one piece of criticism:



The format string in scanf is troublesome.  Look up buffer overflow on the internet to understand why.

It should read

scanf(%19s",getUserMark.name);

However, that is not the end of it.  Your input could still be a name that is longer than 19 characters, and it will be waiting to be read after the scanf call.

The bottom line is that I/O (especially 'I' in I/O) in C is quite hard and not easy to get it correct.
 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic