Thread: Need help with a project

  1. #16
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    Quote Originally Posted by awsdert View Post
    The only thing I can think of right now is that system("PAUSE");
    Check what your cmdline is supposed to do upon receiving that command.

    Edit By the way what is the actual output of the program, does it print the string literals only or is there simply nothing in the window?
    But what does system("PAUSE") has to do with it? Everything in the program works well except that function, It wants me to enter names of the students, after that console just says: To continue, press any button... and its over.

    Now i know where the problem is, its in this line:

    int grades_f(students); [Warning] Parameter names [without types] in function declaration, how do i fix it? If i put int grades_f(&students); how am i supposed to put that structure in there?
    Last edited by Glorn; 02-05-2015 at 02:26 AM.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is not a function call:
    Code:
    int grades_f(students);
    Rather, it looks like a declaration of the grades_f function that has a parameter name, but not a parameter type. You probably wanted to call the function:
    Code:
    grades_f(students);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    Damn it, I forget more than I learn... the function doesnt work like its supposed to though, after entering grades for student1 it scrolls down and it's the end of the program, instead of asking me to enter grades for other students

    I increase both the iterator and the pointer, in a function call I have an array of structures, so what's the problem?
    Last edited by Glorn; 02-05-2015 at 02:44 AM.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 4
    
    
    struct data {
        char name[20];
        char surename[20];
    };
    struct student {
        struct data data_s;
        float average;
        int grades[10];
    };
    int grades_f(struct student* grades_in);
    
    
    int main(int argc, char* argv[])
    {
    
    
        struct student students[MAX];
        int counter = 0;
        printf("Name of the first student: ");
        gets(students[counter].data_s.name);
        while (counter < MAX) {
            printf("Student's surename: ");
            gets(students[counter++].data_s.surename);
            if (counter < MAX) {
                printf("Name of the next student: ");
                gets(students[counter].data_s.name);
            }
        }
        grades_f(students);
        system("PAUSE");
        return 0;
    }
    
    
    int grades_f(struct student* grades_in)
    {
        int i;
        for (i = 0; i < MAX; i++, grades_in++) {
            printf("Grades of student %d: \n", i + 1);
            scanf("%f", grades_in->grades);
        }
    }

  6. #21
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    reason I asked about the system("pause") is redundant now but I'll say it anyway, not all systems do the same thing the same way or with the same commands hence the check.
    Code:
    int grades_f(const struct student* grades_in);
    If you intended grades_in to be a constant pointer then you're mis-understanding something, const modifier makes the content of a variable constant not the variable itself so in this instance the struct is constant but not the pointer.
    Last edited by awsdert; 02-05-2015 at 02:57 AM. Reason: spelling mistake

  7. #22
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    Quote Originally Posted by awsdert View Post
    reason I asked about the system("pause") is redundant now but I'll say it anyway, not all systems do the same thing the same way or with the same commands hence the check.

    If you intended grades_in to be a constant pointer then you're mus-understanding something, const modifier makes the content of a variable constant not the variable itself so in this instance the struct is constant but not the pointer.

    I got it already, check post above for my current code, its not const anymore.

  8. #23
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Yeah my post came in just a second after yours, are you still getting the issue after removing const?

  9. #24
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    Quote Originally Posted by awsdert View Post
    Yeah my post came in just a second after yours, are you still getting the issue after removing const?
    I've got a different issue now, the function works, but I can't enter grades for more than 1 student, after that it's just scrolls down and the program is over.

  10. #25
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    no ideas right now but for iterators should get in the habit of putting ++ on left side for speed (unless you have good reason to put on right hand side like in your first loop)

  11. #26
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    Interesting, I thought this program is not sth extremely hard...

  12. #27
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    sth? dunno what that means but no the program is not hard, just sometimes there are underlying errors in code that programmers don't spot because it's just not obvious enough.

  13. #28
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    try the msvc compiler, it might shout about something gcc ignores because it is technically correct even if the result is not what is desired.

  14. #29
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    I think you've done pretty well in the time you allowed yourself. I know C (more so than some other languages) is a massive spew of gobbledegook when you start.

    If you still have time, I don't think the last few bits you mnetioned in your original post will be too bad. Printing stuff out is much either than reading stuff in. If you have any specific questions I'm sure we'll be happy to help.

    So I compiled your code with -Wall (GCC) and it did kick off pretty hard about you using gets(). Please don't use gets() -- use fgets() -- it is safer and good practice.

    Apart from the massive hissy fit about gets(), there were a couple of warnings you may with to do something about:

    test.c:48:1: warning: control reaches end of non-void function [-Wreturn-type]
    It's not the end of the world, but better to use void if you don't intend to return a value.

    This one was more worrying:
    test.c:46:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘int *’ [-Wformat=]
    scanf("%f", grades_in->grades);
    This will *not* end well!! When you come to try to print those ints, you'll not get what you were expecting. Nor if you try to print them as floats. There are lots of good references for the various printf specifiers (e.g. printf - C++ Reference) but really I only used %d (signed integer) for the first few years of using C.

    Oh, the only other thing. I'd recommend against using system(pause). It's more of a personal thing than anything I can strongly back up technically -- true, it only works in Windows but there's a pile of stuff that only works on Linux or other OSs. I guess I have a moral problem with it because it's so commonly taught to beginners (along with a few other Windowsisms) without a proper discussion of it being OS specific.

    Good luck with your project

  15. #30
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    I didn't even know that was a msw command, then again I never use the cmdline for doing anything other than viewing data anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-02-2013, 06:45 PM
  2. Replies: 5
    Last Post: 02-23-2013, 03:37 PM
  3. Classes Project (Not class project)
    By adam.morin in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2011, 01:48 AM
  4. Paid project - The Free Marketing Project
    By sharefree in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 10-27-2010, 02:15 PM
  5. your 11th & 12th grade c++ project or similar project
    By sleepyheadtony in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 01-13-2002, 05:14 PM

Tags for this Thread