CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 37 of 37
  1. #31
    Join Date
    Jul 2010
    Posts
    13

    Re: Simple GradeSystem

    Quote Originally Posted by GCDEF View Post
    Please delete your post. He derives no benefit from you doing it for him. Perhaps you noticed most of us here could easily correct his mistakes, but chose to give guidance instead of doing the work. That's how this board works. So what, he hands in his (your) assignment having learned nothing. How is that helping? Will you be here to do his next assignment too?
    ok, first of all... I am learning this on my own... this is not for school... and yes don't give me the answer... but give me what I need to learn... like an example... when you give me the answer I have to go through and learn to see what I did wrong and may assume something that is wrong is right... (if you get what I mean)

    To everyone else... I will work on the code again when I get back... and yes I am reading what the compiler is saying, but I just don't know how to fix it... (said isnt it?) well... thats what happens when you are learning...

    be back in 5 days... see you all then (wish I had a laptop or netbook)

  2. #32
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Simple GradeSystem

    Quote Originally Posted by GCDEF View Post
    Please delete your post. He derives no benefit from you doing it for him. Perhaps you noticed most of us here could easily correct his mistakes, but chose to give guidance instead of doing the work. That's how this board works. So what, he hands in his (your) assignment having learned nothing. How is that helping? Will you be here to do his next assignment too?
    If you read the post you would see that I explained each single change I had made to the original code. 90% percent of the code I posted was from the OP (that's why there was still some redundant code where I already had posted a different solution). My original purpose was to split the code changes so that little code follows an explanation. Unfortunately that would have required more time than I had yesterday and some of the corrections required changes to different functions. But I will delete the post and do the split today.

    Regards, Alex

  3. #33
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Simple GradeSystem

    Quote Originally Posted by Zeveso View Post
    Ok, I think i got those changes right... but still the program is no where near runable

    Current Errors...
    [Code]
    gradeSystem.cpp: In function 'void writeGrade(int, int)':
    gradeSystem.cpp:13: error: too few arguments to function 'int findLetter(int, char)'
    gradeSystem.cpp:38: error: at this point in file
    gradeSystem.cpp: In function 'void saveError()':
    gradeSystem.cpp:56: error: expected primary-expression before 'int'
    gradeSystem.cpp:56: error: 'nowTime' was not declared in this scope
    gradeSystem.cpp:59: error: 'theTime' was not declared in this scope
    gradeSystem.cpp: In function 'int nowTime()':
    gradeSystem.cpp:69: error: 'theTime' was not declared in this scope
    gradeSystem.cpp: In function 'int main()':
    gradeSystem.cpp:90: error: no match for 'operator>>' in 'std:perator>> [with _CharT2 = char, _Traits2 = std::char_traits<char>, _CharT = char, _Traits = std::char_traits<char>](((std::basic_istream<char, std::char_traits<char> >&)(& std::cin)), ((char*)(& studentName))) >> std::endl'
    /usr/include/c++/4.3/istream:123: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>:perator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]

    ...
    /
    I hope, you already achieved to solve some of the errors above by applying the advice you got .. but if not I will give you samples of the correct statements to solve each single error:

    You currently call the function findLetter with no arguments (you commented the argument list). But your current findLetter has the prototype 'int findLetter(int, char)' and requires 2 arguments. So the following call would compile:

    Code:
        findLetter(studentAverage, ' ');
    You see that I didn't pass the letterGrade which was argument of function writeGrade. That is for two reasons:

    (1) the letterGrade in writeGrade is of type int and not of type char
    (2) you would pass it as input argument by value to findLetter what makes no sense cause it is a return value of findLetter.

    The (1) you can solve easily cause the writeGrade doesn't need an input argument letterGrade as it determines the letter from studentAverage by calling findLetter. Hence, you simply can remove the 'int letterGrade'.

    For (2) same applies to the 'char letterGrade' which you don't have to pass to findLetter and remove it from arguments.

    Code:
    ...
    int findLetter(int studentAverage){
      char letterGrade = '?';
      // use your own code from here
    
    ...
    
    void writeGrade(int studentAverage)
    {
      // get letter from function
      char letterGrade =  = findLetter(studentAverage);
      // use your own code from here
      ...
    One more thing is (what didn't give an error) that the writeGrade didn't get the student name as an argument and writes the student average instead.

    Code:
    ...
    void writeGrade(int studentAverage, const char * studentName)
    {
       ...
       gradeFile << "Student: " << studentName << endl;
       ...
    I also added the 'else if' to the conditions in findLetter cause the conditions will mutually exclude each other. So if one condition is met, the others don't need to be checked anymore.

    Code:
    ...
    int findLetter(int studentAverage){
      ...
      if ( studentAverage > 94 ){
        letterGrade = 'A';
      }
      else if ( 94 >= studentAverage && studentAverage > 85 ){
      ...
    I also added gradeFile.close() to the writeGrade. That way the output was flushed to the file and you could look at it while the program is still running. Moreover, some compilers don't like it if a file was opened and opened and never closed ;-)

    Code:
    ...
    void writeGrade(int studentAverage, const char * studentName)
    {
       ...
       gradeFile.close();
    }
    The error 'gradeSystem.cpp:56: error: expected primary-expression before 'int' ' in saveError was because you have the 'int' in a function call. Argument types generally are wrong syntax when *calling* a function but only when declaring or defining (implementing) it.

    So you would need to change

    Code:
    nowTime(int theTime);
    to

    Code:
       theTime = nowTime();
    However, as it seems that you want 'theTime' to be a string expression of the time, the return type should be a char* rather than an int.
    Hence, both the 'theTime' variable and the function 'nowTime' should have type char*.

    Code:
    char * nowTime()
    {
       ...
    }
    
       ...
       // call the function and get char pointer to a local variable
       char * theTime = nowTime();

    The nowTime implementation was pretty wrong mostly caused by wrong function calls.

    As told before you should return a char* rather than an int.

    You could get that char* by calling ctime function passing a pointer to the time_t of the current systemtime which you retrieved by a call to time function.

    Code:
       char * strtime = ctime(&now);
    The ctime returns a pointer to a (internal) char array which is null-terminated. After assigninging it to a local char* variable you simply can return it.

    In the main function you have a 'while ( canWe = true )' with = instead of == . That way the while loop is endless cause you were assigning 'true' for any cycle.

    change the while statement to

    Code:
       while (canWe == true)
       {
           ...
    and you can end the loop by setting canWe to false later.

    In the following code you get an iostream error because of 'cin >> canWe;'.

    That is cause the istream operator>> doesn't take a right-hand bool variable.

    You could solve the issue by using an int variable instead of bool like

    Code:
       int canWe = 1;
       while (canWe)
       {
           ...
           cin >> canWe;
       }
    though the above is very unsafe. If the user enters a non-digit the cin stream would be spoiled and the program hangs. To avoid that, I would recommend using getline for input instead of cin>> . To do so you should add <string> and <sstream> to the includes and use getline wherever you used cin>>.

    Code:
        string studentName;   // change from char array to string.
        string input;
        int studentAverage = 0;
        ...
        getline(cin, studentName);
        ...
            // cin >> studentAverage;
            getline(cin, input); 
            studentAverage = atoi(input.c_str());
            ...
            // cin >> canWe;
            getline(cin, input); 
            if (input == "0")
                canWe = false;
        ...
    You see that the getline always returns a string input. That's why in case of 'int studentAverage' or 'bool canWe' we need some kind of conversion. For a simple and error-ignoring way the atoi function is suitable. If you want to go the C++ way you may use a stringstream for convering the input to the studentAverage:

    Code:
            istringstream iss(input);
            if (!(iss >> submitAmount))
            {
               cout << "Please submit only numbers !" << endl << endl;
               continue;
            }

    Finally the calls of 'theAverage' and 'writeGrade' must have proper input arguments:

    Code:
        studentAverage = theAverage(totalAmount, gradeNumber);
        writeGrade(studentAverage, studentName.c_str());
    The c_str() returns a const char * from a std::string. Alternatively you could use 'const string &' as argument type of studentName and you can omit the c_str().

    Regards, Alex

  4. #34
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Simple GradeSystem

    You're still doing his work for him, and we don't do that here.

  5. #35
    Join Date
    Jul 2010
    Posts
    13

    Re: Simple GradeSystem

    Ok, you taught me a couple things for sure. Must say... this was a good learning / wakeup exercise for me. Still not there yet. Working on it, but you did explain many things for me very well.

    So I should use char* for all stings instead of char myString[255]?

    Also I have not emplimented getline yet because I just want to get the program working first. Then want to do that and clean up the loop some more.

    Also, some of those things were so obvious... I can't believe I did not see them. Thanks on those.

    If you have time can you explain on how to get the time and use it in a string better? Because I don't get it at all and I feel that is my main problem with my code right now next to not knowing about functions well enough. I am going to read up on both of those after I post this.

    If you see anything else, or can think of anything else that can help me let me know.

    Thank you very much!!!

    PS. Before I tried writing this I did all of the small steps. Like I made a program to write things, and I made one to display the date and time, and I made one that did math... but putting it all together has been a pain.

    Code:
    Error Log:
    g++     gradeSystem.cpp   -o gradeSystem
    gradeSystem.cpp: In function 'char* nowTime(char*)':
    gradeSystem.cpp:74: warning: deprecated conversion from string constant to 'char*'
    gradeSystem.cpp: In function 'int main()':
    gradeSystem.cpp:51: error: too few arguments to function 'int theAverage(int, int, int)'
    gradeSystem.cpp:117: error: at this point in file
    gradeSystem.cpp:119: error: request for member 'c_str' in 'studentName', which is of non-class type 'char [255]'
    make: *** [gradeSystem] Error 1
    Code:
    #include <iostream>
    #include <fstream> // open, read, and write files
    
    //for time
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    //for getline;
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    char findLetter(int studentAverage, char letterGrade){
    //take the number and find the letter it is == to
    //then store that letter in letterGrade
      if ( studentAverage > 94 ){
        char letterGrade = 'A';
      }
      else if ( 94 >= studentAverage && studentAverage > 85 ){
        char letterGrade = 'B';
      }
      else if ( 85 >= studentAverage && studentAverage > 78 ){
        char letterGrade = 'C';
      }
      else if ( 78 >= studentAverage && studentAverage > 69 ){
        char letterGrade = 'D';
      }
      else if ( studentAverage < 69 ){
        char letterGrade = 'F';
      }
      return letterGrade;
    }
    
    void writeGrade(int studentAverage, char studentName[255], char letterGrade){
    /*
    Use the variable gradeAverage to determin the the grade was a A, B, C, D, or F. Then open of the person's file, and write the grade.
    */
      char findLetter(int studentAverage, char letterGrade, char studentName[255]);
      ofstream gradeFile;
      gradeFile.open ("grades.txt", ios::out | ios::app);
      gradeFile << "Student: " << studentName << endl;
      gradeFile << "Grade: " << letterGrade << endl;
      gradeFile << "Average: " << studentAverage << endl << endl;
      gradeFile.close();
    }
    
    int theAverage(int totalAmount, int gradeNumber, int studentAverage){
    /*
    Here we will average all the numbers together inside of a for loop
    */
      studentAverage = totalAmount / gradeNumber;
      return studentAverage;
    }
    
    void saveError(char * nowTime, char * theTime){
    // save error in log.txt
      theTime = nowTime;
      ofstream logFile;
      logFile.open ("log.txt", ios::out | ios::app);
      logFile << "Date: " << theTime << endl;
      logFile << "Error: File does not exist or cannot be opened." << endl;
      logFile << "Please create file, or create a new file." << endl << endl;
    }
    
    char * nowTime(char * theTime){
    
      time_t now;
      time(&now);
    
      theTime = "%s", ctime(&now);
    
    return theTime;
    }
    
    int main(){
      //startup variables that we will use
      char studentName[255];
      int studentAverage;
      int gradeNumber; // amount of student Averages
      int totalAmount; //will hold all averages
      int submitAmount; //amount submited during for loop session
      char letterGrade; // will store the lettergrade before it is written to grades.txt
      char * theTime; // will hold the current time
      bool canWe; // asks if we can continue
      
    
      // find out who we will be grading
      cout << "Hello user!" << endl;
      cout << "This program lets you average the grade of your student and write there grade in a text file." << endl;
      cout << "Who will be writting a grade for today?" << endl << "Name: ";
      cin >> studentName;
      cout << endl << "Ok, we will be grading " << studentName << "today!" << endl;
    
      //make canWe true
      canWe == 1;
    
      // make a loop for finding out the average
      while ( canWe == true ){
      for ( int gradeNumber = 0; gradeNumber++; ){
      cout << "Please submit the next grade." << endl << "Grade: ";
      cin >> submitAmount;
      totalAmount = submitAmount + totalAmount;
      cout << "Thank you, the grade has been added." << endl;
      cout << "Would you like to add another grade or would you like to quite?" << endl;
      cout << "Type 1 to contine or type 2 to quite and press <enter>." << endl << "1 or 0: "; // may need to be switched to true or false
      cin >> canWe;
      cout << endl;
      return totalAmount;
      }
      }
    
      //theAverage();
      studentAverage = theAverage(totalAmount, gradeNumber);
      //writeGrade();
      writeGrade(studentAverage, studentName.c_str());
      
    
    return 0;
    }

  6. #36
    Join Date
    Jun 2008
    Posts
    592

    Re: Simple GradeSystem

    Code:
    char findLetter(int studentAverage, char letterGrade){
      if ( studentAverage > 94 ){
        char letterGrade = 'A';
      }
      else if ( 94 >= studentAverage && studentAverage > 85 ){
        char letterGrade = 'B';
      }
      else if ( 85 >= studentAverage && studentAverage > 78 ){
        char letterGrade = 'C';
      }
      else if ( 78 >= studentAverage && studentAverage > 69 ){
        char letterGrade = 'D';
      }
      else if ( studentAverage < 69 ){
        char letterGrade = 'F';
      }
      return letterGrade;
    }
    each char letterGrade is specific to its scope { }. All you did was create temporaries. get rid of the char in front of each letterGrade and add char letterGrade; to the top of the function. this way letterGrade will be local to the function and not to each if statement's scope.

    Your comparison test( highlighted in blue ) is brutal and I would not dare to make sense out of it.

    Start with this
    Code:
    char ConvertScoreToGrade( int Score )
    {
        char Grade;
    
             if( Score >= 90 ){ Grade = 'A'; }
        else if( Score >= 80 ){}
        else if( Score >= 70 ){}
        else if( Score >= 60 ){}
        else {}
    
        return Grade;
    }
    Finish this code and ouput your results to the console. test it by supplying known variables to this function and make sure the results are correct.

    Code:
      cin >> studentName;
    If you tested this code, you would know this is wrong unless last names do not matter. Try inputting John Doe. You should use std::getline and test the input for correctness.

    50&#37; of your code is wrong.

    You don't have the basics down yet and you are trying to build an application blindly. This is not good.

    Take your time and read a few of these pages

    http://www.cppreference.com/
    http://www.cplusplus.com/doc/tutorial/
    http://wikipedia.org/
    http://en.wikipedia.org/wiki/C%2B%2B_classes
    http://www.research.att.com/~bs/bs_faq.html

    Come back with a more compilable program. Please note that if you code does compile, it does not mean it does what you think.

    Start with the basics and finish my ConvertScoreToGrade and test it.
    Last edited by Joeman; August 4th, 2010 at 10:04 PM.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  7. #37
    Join Date
    Jul 2010
    Posts
    13

    Re: Simple GradeSystem

    @Joeman

    You are right... will finish this and check all of it out. I was not trying to build an application to use it, but trying to apply what I thought I had learned. At least this proved one thing, I don't know it yet and need to read more. I have them down 1 by 1, but putting everything together sucks... I am not capable yet.

    Anyways... ill get back to you all after I read what Joeman posted.

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured