Need Help understanding arrays and pointers

This is a discussion on Need Help understanding arrays and pointers within the C++ Programming forums, part of the General Programming Boards category; Hi guys! I have this homework to do and I am not sure how to get this done, so if ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    45

    Unhappy Need Help understanding arrays and pointers

    Hi guys! I have this homework to do and I am not sure how to get this done, so if anyone could help it would be great.
    This is what is asked:
    Design and run a program that takes a numerical score and outputs a letter grade. Specific numerical scores and letter grades are listed below:
    90-100 = Grade A
    80-89 = Grade B
    70-79 = Grade C
    60-69 = Grade D
    0-59 = Grade F
    In this program, create 2 void functions titled getScore and printGrade with an int argument. The function getScore should have a Reference parameter and printGrade should have a Value parameter.

    The function getScore will prompt the user for the numerical score, get the input from the user, and print the numerical score. The function printGrade will calculate the course grade and print the course grade. (Be careful and note that the assignment requires you to input the grade into getScore and not directly into the main function.)

    This is what I did so far. It compiles without giving me any error but the output stays blank.
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <stdlib.h>
    using std::cout;
    using std::cin;
    using std::endl;
    using namespace std;
    int score;
    
    
    void getScore();
    void printGrades();
    
    void getScore (int&)
    {
     std::cout << "Please input the grade score: ";
             std::cin >> score;
            std::cout << "You entered the score of"<<score<<endl;
    		return;
    }
    void printGrades(int )
    {
        if (score >= 0 && score <= 59)
        {
            std::cout << "The grade is an F.";
        }
        else if (score >= 60 && score <= 69)
        {
            std::cout << "The grade is a D.";
        }
        else if (score >= 70 && score <= 79)
        {
            std::cout << "The grade is a C.";
        }
        else if (score >= 80 && score <= 89)
        {
            std::cout << "The grade is a B.";
        }
        else if (score >= 90 && score <= 100)
        {    
            std::cout << "The grade is an A.";
        }
    return;}
    
    int main()
    {
    void getScore();
    void printGrades();
     return 0;
    }
    Thanks for your help.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Instead of making score a global variable, make it a local variable inside main and pass it to your functions. You should then name the variable parameters that your functions use. Everything else looks ok.

    BTW, this has nothing to do with arrays or pointers.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Daved,
    Thanks. I'll try that.
    BTW, this has nothing to do with arrays or pointers.
    funny since this is given after the array & pointer chapter reading

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Ok I have made some changes to the program but I still have a problem.
    I get the first function working fine but the second function gives a grade of F whatever the score I enter. Something is definitely not done \right, but don't know what. Could anyone help me point out where I'm making a mistake.
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <stdlib.h>
    using std::cout;
    using std::cin;
    using std::endl;
    using namespace std;
    int score;
    int *grade;
    
    void getScore();
    void printGrades();
    
    int getScore (int score)
    {
    std::cout << "Please input the grade score: ";
              std::cin >> score;
            std::cout << "You entered the score of "<<score<<endl;
    		return score;
    }
    void printGrades(int *grade)
    {
         grade = &score;
    
        if (*grade >= 0 && *grade <= 59)
        {
            std::cout << "The grade is an F.";
        }
        else if (*grade >= 60 && *grade <= 69)
        {
            std::cout << "The grade is a D.";
        }
        else if (*grade >= 70 && *grade <= 79)
        {
            std::cout << "The grade is a C.";
        }
        else if (*grade >= 80 && *grade <= 89)
        {
            std::cout << "The grade is a B.";
        }
        else if (*grade >= 90 && *grade <= 100)
        {    
            std::cout << "The grade is an A.";
        }
    return;}
    
    int main()
    {
    getScore(score);
    printGrades(grade);
     return 0;
    }

  5. #5
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33
    I'm not entirely sure, but change
    Code:
    if (*grade >= 0 && *grade <= 59)
        {
            std::cout << "The grade is an F.";
        }
    to
    Code:
    if (((*grade) >= 0) && ((*grade) <= 59)))
        {
            cout << "The grade is an F.";
        }
    and see if that helps.

    Edit: Oh, and the same for D-A as well.
    ------------------------------------------------
    cout<<"Hello World!\n";
    ------------------------------------------------

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    nickodonnell ,

    Thanks! Good idea but I just tried that but i still get the same result of F.
    worth a try though.

  7. #7
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    if the teacher says to use a reference, use a reference, not a pointer, they are not the same thing.

  8. #8
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    by the way, the problem is that you have a local variable in your getScore function named score, so when you use it in cin, that's where it gets stored, in the local variable, not in the global. Get rid of the global variable. Use a reference to int in your getScore function, and use an int value as a parameter to your other function, that's what makes sense.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    rockytriton. That helps me really ! Thanks.

  10. #10
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    and make getScore() return void because since you are using a reference to return the value, you don't need to return it as a return value.

  11. #11
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Well I must be really dumb because even with all your help, I'm not getting this right.
    I took out the global variable, put a local one. But I'm still not understanding how to pass this variable from one function to another.
    Here's what I did:
    Code:
    void getScore(int& score)
    {
    std::cout << "Please input the grade score: ";
              std::cin >> score;
    		  score= score + score;
            std::cout << "You entered the score of "<<score<<endl;
    		}
    void printGrades(int score)
    {
    	if (score >= 0 && score <= 59)
        {
            std::cout << "The grade is an F.";
        }
        else if (score >= 60 && score <= 69)
        {
            std::cout << "The grade is a D.";
        }
        else if (score >= 70 && score <= 79)
        {
            std::cout << "The grade is a C.";
        }
        else if (score >= 80 && score <= 89)
        {
            std::cout << "The grade is a B.";
        }
        else if (score >= 90 && score <= 100)
        {    
            std::cout << "The grade is an A.";
        }
    }
    
    int main()
    {
    int score;
    score = 0;
    getScore();
    printGrades(int score);
     return 0;
    }
    And these are the error messages I'm getting:
    Code:
    (45) : error C2144: syntax error : 'void' should be preceded by ')'
    (45) : error C2660: 'getScore' : function does not take 0 arguments
    45) : error C2059: syntax error : ')'
    (46) : error C2144: syntax error : 'int' should be preceded by ')'
    (46) : error C2660: 'printGrades' : function does not take 0 arguments
    (46) : error C2059: syntax error : ')'
     - 6 error(s), 0 warning(s)
    Is there a tutorial or something that could explain this to me clearly. I have seen a bunch of them but they always use main and not 2 functions.
    Thanks yfor your help.
    Last edited by flicka; 10-04-2005 at 09:36 PM.

  12. #12
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    you forgot to pass score when you call getScore();

    getScore(score);

    and with printScore(); when you call it, you don't put the type in the call, it's just:

    printScore(score);

  13. #13
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Thanks rockytriton!
    It's working.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21