Thread: New to programming (of any kind)

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    7

    New to programming (of any kind)

    I would like some help adding a
    void getInputChar(string, char &); and a bool ignoreCaseCompare (char, char); to my program
    Code:
    // This program calculates student exam average.#include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
       int studScore;
       int studAvg; 
       int Exam1;
       int Exam2;
       int Exam3; 
       int examTotal;
       string studFirstName;
       string studLastName;
       char grade;
    
       // Get students first name.
       cout << "Enter student's first name. ";
       cin >> studFirstName;
       // Get students last name.
       cout << "Enter student's last name. ";
       cin >> studLastName;
       // Get exam 1 score.
       cout <<  "grade from exam 1 = ";
       cin >> Exam1;
       // Get exam 2 score.
       cout <<  "grade from exam 2 = ";
       cin >> Exam2;
       // Get exam 3 score.
       cout <<  "grade from exam 3 = ";
       cin >> Exam3;
       // Calculate exam total.
       studScore = Exam1 + Exam2 + Exam3;
       // Calculate exam total.
       studAvg = studScore/3;
       // End of Project 1
       // Beginning of Project 1.2
       // if else condition
       // statement to display grade letter.
       if (studAvg >= 90) 
        grade = 'A';
       else if (studAvg >= 80) 
        grade = 'B';
       else if (studAvg >= 70) 
        grade = 'C';
       else if (studAvg >= 60)
        grade = 'D';
       else
        grade = 'F';
       // Display the average.
       cout <<  "average is " 
         << studAvg 
      << " " 
      <<grade << endl;
       // Stop program
       system ("pause");
       return 0; 
    }
    Iam guessing it is pretty simple to do seeing that I am using char for the grades. But I am not a programmer by any means and I am on chapter 2 in the book and we are working on chapter 7. This stuff has just been very difficult for me to understand. So please show me an example of what to do because I am a visual learner and the words mean very little if i can not see it .

    Thanks

  2. #2
    Registered User
    Join Date
    Jul 2012
    Posts
    87
    Some hints: when you write a function, the idea is to make your program easier to read and less tedious, so functions do the same task for you, so you have it right to decide to come up with a getInput from user function. You seem to understand what parameters to pass that the function will need to work with. So try adding some code of what you think should go inside your getInput function first. You'll find it's not that difficult.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    I think it should be something like this.

    Code:
    // This program calculates student exam average.
    #include <iostream>
    #include <string>
    using namespace std;
    void getInput();
    int main()
    {
       int studScore;
       int studAvg; 
       int Exam1;
       int Exam2;
       int Exam3; 
       int examTotal;
       string studFirstName;
       string studLastName;
       char grade;
       getInput (First Name, Last Name);
       system ("pause");
       return 0; 
    }
    {
       void getInputChar **or should it just say void getInput** 
       (string StudFirstName, string studLastName, char grade &);
    But I have no idea. I dont even know where to take what I have stated.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    87
    You only wrote the function header: void getInput(); but you can't call the function in main if you haven't even implemented your getInput function! Also, why did you get rid of the parameters in the getInput(/**Where are the parameters?*/). If you have no parameters, all you function can do is ask is output to the console.

    Remember purpose of parameters is you want to change or access something. You need to really look up stuff like this, knowing how to write a function should be a basic. See: Functions in C and C++ - Cprogramming.com very easy to learn.
    Last edited by monkey_c_monkey; 08-01-2012 at 05:06 PM.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    Ok so I have gone back into my little program and added a function which was not that hard for the function i added. So now I am having a few issue one big (for me) and the other is very small, and I know it is very simple I am just over looking it somewhere.

    1) The small issue is that when I run my program if in the "Enter Student Name" area if I enter more that one character the program fails. However enter only one letter then the program runs successfully. I know this is very easy fix, but all my attempts have failed I tried unsigned char, string, long char and they have all failed what is the fix to this.

    2) Here is the larger issue I now wish to add a bool compare (int, int); so that I can ask the question "Enter students calculated average" then input the number that was calculated as the average and have the bool compare the two int if the number that is manually inputed at the question is not the same as the number outputed by the calculation I what the bool to do its job and not == say "Please verify Average" or something.

    Code:
    // This program calculates student exam average.
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    void getExamInfo(string, int &);
    void bool avgCompare(int, int&);
    
    int main()
    
    {
       string prompt = "Enter student's exam grade ";
       int studScore, studAvg, Exam1, Exam2, Exam3, CalAvg;
       unsigned char studName;
       char grade;
       
    
       // Get students first name.
       cout << "Enter student's name. ";
    
       cin >> studName;
    
       getExamInfo(prompt, Exam1);
       getExamInfo(prompt, Exam2);
       getExamInfo(prompt, Exam3);
       
       // Original code Replaced by getExamInfo function
       // Get exam 1 score.
       //cout <<  "grade from exam 1 = ";
       //cin >> Exam1;
       // Get exam 2 score.
       //cout <<  "grade from exam 2 = ";
       //cin >> Exam2;
       // Get exam 3 score.
       //cout <<  "grade from exam 3 = ";
       //cin >> Exam3;
    
       // Calculate exam total.
       studScore = Exam1 + Exam2 + Exam3;
    
       // Calculate exam total.
       studAvg = studScore/3;
    
       // End of Project 1
    
       // Beginning of Project 1.2
       // if else condition
       // statement to display grade letter.
       if (studAvg >= 90) 
           grade = 'A';
       else if (studAvg >= 80) 
           grade = 'B';
       else if (studAvg >= 70) 
           grade = 'C';
       else if (studAvg >= 60)
           grade = 'D';
       else
           grade = 'F';
    
       // Display the average.
       cout <<  "average is " 
            << studAvg 
            << " " 
            <<grade << endl;
    
        // Bool function code should start here
       // Verify grade
       cout << "Enter students' calculated average ";
       cin >> CalAvg;
    
    
       // Stop program
       system ("pause");
       return 0; 
    }
    void getExamInfo(string msg, int &exam)
    {
        cout << msg;
        cin >>exam;
    } 
    void bool avgCompare (int studAvg, int &CalAvg)
    {
    
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by oblaq View Post
    1) The small issue is that when I run my program if in the "Enter Student Name" area if I enter more that one character the program fails. However enter only one letter then the program runs successfully. I know this is very easy fix, but all my attempts have failed I tried unsigned char, string, long char and they have all failed what is the fix to this.
    A char is a single character. You need to use a string to input more than one character.

    2) Here is the larger issue I now wish to add a bool compare (int, int); so that I can ask the question "Enter students calculated average" then input the number that was calculated as the average and have the bool compare the two int if the number that is manually inputed at the question is not the same as the number outputed by the calculation I what the bool to do its job and not == say "Please verify Average" or something.
    What is your problem, exactly?

    Also, read this: SourceForge.net: Do not remove parameter names - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    1. right now studName is just a single character make it a string.

    2.
    Code:
    void bool avgCompare(int, int&);
    a function can only have a single return value ( type ) remove the void.
    judjing from the name of the functionI would say that it will not modify the second argument so why is it a reference to int ?

    Kurt

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    well the solution to program 1) was easy enough.

    problem 2) exact issue is how do i write the bool for this. Now I have discovered that the void and bool can not be in the same line statement. But the issue i am having is I dont know how to write the bool arguement and I could really use some help.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    What do you mean by "I dont know how to write the bool arguement"
    Do you mean how to call such a function or how to implement the function ?
    Kurt

  10. #10
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    Quote Originally Posted by ZuK View Post
    What do you mean by "I dont know how to write the bool arguement"
    Do you mean how to call such a function or how to implement the function ?
    Kurt
    I mean that i dont know how to construct the bool function.

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    ok I did some work to clarify what i what to do a little better.
    Code:
    // This program calculates student exam average.
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    void getExamInfo(string, int &);
    bool avgCompare(int, int &);
    
    
    int main()
    
    {
       string prompt = "Enter student's exam grade ";
       string studName;
       int studScore, studAvg, Exam1, Exam2, Exam3, CalAvg;
       char grade;
       
    
       // Get students first name.
       cout << "Enter student's name. ";
    
       cin >> studName;
    
       getExamInfo(prompt, Exam1);
       getExamInfo(prompt, Exam2);
       getExamInfo(prompt, Exam3);
       
       // Original project 1.1 code
       // Get exam 1 score.
       //cout <<  "grade from exam 1 = ";
       //cin >> Exam1;
       // Get exam 2 score.
       //cout <<  "grade from exam 2 = ";
       //cin >> Exam2;
       // Get exam 3 score.
       //cout <<  "grade from exam 3 = ";
       //cin >> Exam3;
    
       // Calculate exam total.
       studScore = Exam1 + Exam2 + Exam3;
    
       // Calculate exam total.
       studAvg = studScore/3;
    
       // End of Project 1
    
       // Beginning of Project 1.2
       // if else condition
       // statement to display grade letter.
       if (studAvg >= 90) 
           grade = 'A';
       else if (studAvg >= 80) 
           grade = 'B';
       else if (studAvg >= 70) 
           grade = 'C';
       else if (studAvg >= 60)
           grade = 'D';
       else
           grade = 'F';
    
       // Display the average.
       cout <<  "average is " 
            << studAvg 
            << " " 
            <<grade <<endl; 
       
       // Verify grade
       cout << "Enter students' calculated average ";
       cin >> CalAvg;
       if (CalAvg==studAvg)
       {
           cout << "Average is Correct." <<endl;
       }
       else if (CalAvg!=studAvg)
       {
            cout << "Houston we have a problem!" <<endl;
       }
      
       // Stop program
       system ("pause");
       return 0; 
    }
    void getExamInfo(string msg, int &exam)
    {
        cout << msg;
        cin >> exam;
    }
    bool avgCompare(int studAvg, int &CalAvg)
    {
    return 0;
    }
    So what i am wanting to do is to create a bool function call avgCompare that will take the place of this portion of code that i just created.
    Code:
    // Verify grade
       cout << "Enter students' calculated average ";
       cin >> CalAvg;
       if (CalAvg==studAvg)
       {
           cout << "Average is Correct." <<endl;
       }
       else if (CalAvg!=studAvg)
       {
            cout << "Houston we have a problem!" <<endl;
       }
    
    I am starting to understand why you would want to use functions because code can get way out of control and it is much easier to break any code longer than 6 lines into functions. But that dont mean i know how to write them! LOL!!
    Last edited by oblaq; 08-04-2012 at 02:59 PM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is so difficult? Move the code inside a function, then call it.
    And I have yet to see you fix what the link pointet out, either.
    Last edited by Elysia; 08-04-2012 at 03:26 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    So why not just try to remove that portion of code from main, move it into the body of avgCompare and call avgCompare from main.
    See what happens.
    Kurt

  14. #14
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    it was a simple as cutting it out of the body and putting it in the function.

    Now i am continueing to try and refine my code. So I have decided to create function called "getStudInfo" in which I am trying to combine the portion of the code that ask for the student name and exam scores.
    Here is the function that i wrote
    Code:
    void getStudInfo(string fname, string &lname, int &Exam1, int &Exam2, int &Exam3);
    {
       cout << "Enter student's first name. ";
       cin >> fname;
       cout << "Enter students's last name. ";
       cin >> lname;
    }
    Here is the prototype
    Code:
    void getStudInfo(string, int &);
    and here is the call
    Code:
    getStudInfo(fname, lname);
    now this is the error
    error C2664: 'getStudInfo' : cannot convert parameter 2 from 'std::string' to 'int &'
    So what is it i am doing wrong.

    Oh the part of my project that ask for the exam scores is in a function already so i figured I would watch some videos on how to have a function call another function.

    By the way Elysia I did read over the article you linked me to and it was informative but being that i am already very confused it was like speaking "Hello" in latin to a 5 year old; it sounds cool but they have no idea what you just said.
    Last edited by oblaq; 08-04-2012 at 06:57 PM.

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    36
    error C2664: 'getStudInfo' : cannot convert parameter 2 from 'std::string' to 'int &'
    The function prototype, and the function call within main() (or any other function) all must have the same number of arguments (the variables) and the same data types (int, string, float, bool, char etc ...) in the same order. I suggest reading this Functions (I) - C++ Documentation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. kind of new here and need help
    By yasharm in forum C Programming
    Replies: 7
    Last Post: 01-03-2007, 11:35 AM
  2. What kind of job...
    By Discolemonade in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-15-2005, 08:23 PM
  3. would anybody be kind enough to help?
    By almich in forum C++ Programming
    Replies: 10
    Last Post: 07-07-2004, 05:49 AM
  4. Getting three of a kind
    By Kons in forum C Programming
    Replies: 8
    Last Post: 08-19-2003, 12:44 PM
  5. What kind of car do you have?
    By Tleggo in forum A Brief History of Cprogramming.com
    Replies: 50
    Last Post: 06-13-2002, 03:56 PM