Hey, I am new at C++ and I find those codes you put up to be very copmlicated.
I am confused by the functions...
Look at this lecture slide example for instance...can you explain how we do those...
CODE:
#include <iostream>
#include <string>
using namespace std;
int getmark(int number) f
Introduction (continued)
int mark;
cout << Enter the mark for subject
<< number << endl;
cin >> mark;
if (cin.fail()) f
cout << Input failure << endl;
mark = 0;
gif ((mark<0) || (mark>100)) f
cout << Invalid mark << endl;
return 0;
g
return mark;
g
int main() f int mark1;
Introduction (continued)
int mark2;
int mark3;
int mark4;
string line;
double average;
mark1 = getmark(1);
mark2 = getmark(2);
mark3 = getmark(3);
mark4 = getmark(4);
average = (mark1+mark2+mark3+mark4)/4;
cout << The average is << average
<< endl;
cout << Press enter to terminate
<< endl;
// skip newline after last mark
getline(cin, line);
getline(cin, line);
return 0;
ok, well, take a look at some simple code using a function that accepts parameters and returns a value...
#include<iostream>
using namespace std;
int myfunc(int); // you can also put the name of the variable you use after the
// int
// in the function prototype such as int myfunc(int myvariablename); but
// it is not neccessary
// the first part of the function specifies the return type, INT in this case..
int main() // main function, it does not have arguments (notice the () ? )
{
int n00b=3;
cout << myfunc(n00b); // this will give the value of n00b to the function
// myfunc, and within this function, we can use the local variable name
// we have set..
return 0;
}
int myfunc(int nub)// the variable nub will be used inside the function
// and it will be destroyed after the function has finished
//the n00b variable will remain unaltered
{
return nub*1337; // returns the value of nub times 1337
}
you can also use myfunc(1337) or any other number passed as an argument
i believe this is called pass-by-value, whereas the other statement is pass-by-reference
I don't know what kind of function you're wanting to do.. But a function is pretty much a neatly packaged portion of reusable code. What *exactly* do you want it to do?
using namespace std;
//--Declare the function
int main();
double determinediscount();
int getcost(int number);
/*Function to calculate the discount, if there is any
[Extracts the fisrt four charectors of the student number
and chech if they are "= 2009 */
double determinediscount(){
//--Declare the discount
const double discount = 0.05;
//--Declare the string variables
string name;
string student_number;
string four_digits;
//--Output for the user to enter name into string variable name
cout << "Please Enter students Name: " << endl;
getline(cin, name);
//--Output for the user to enter student number into string variable student_number
cout << "Please Enter " << name << "'s StudentNumber:" << endl;
getline(cin, student_number);
//--Extracts the first four digits of student_number
four_digits = student_number.substr(0, 4);
cout << endl;
cout << endl;
//--Checks if four_digits is equal to 2009
if(four_digits == "2009"){
cout << "Student Gets Discount of 5%" << endl;
//--Returns the value of discount
return discount;
}
return 0;
}
/*function to enter the cost of each module. And also checks for errors and
if the are, it should repeatedly ask the user for valid input.
*/
int getcost(int number){
int cost;
cout << "Enter Cost for Module" << number << endl;
cin >> cost;
if (cin.fail()){
cout << "Error in Input" << endl;
cout << "Enter Cost for Module" << number << endl;
cin.clear();
string temp;
cin >> temp;
cin >> cost;
return getcost(number);
}
if ((cost < 0) || (cost > 10000)){
cout << "Cost seems to be too outstanding, please check and enter the right cost..." << endl;
return 0;
}
return cost;
}
int getcode (int number){
string code;
cout << "Enter Code for Module" << number << endl;
getline(cin, code);
cout << "The total is: " << total << endl;
cout << endl;
cout << ".Press enter to terminate."<< endl;
// skip newline after last mark
getline(cin, line);
getline(cin, line);
return 0;
}
The question was....
we suppos to use functions to get a discount(5%) if the student number starts with 2009, and a function for a deposit of 10% and A function to compute the balance after payment has been made