Amuture programmer

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
Last edited on
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?
I still don't understand this. please elaborate//
look at my code for exmple...

#include <iostream>
#include <string>


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);

return 0;
}

int main(){
double cost1;
double cost2;
double cost3;
double cost4;
double cost5;
double cost6;
double cost7;
double cost8;
double total;


string line;
string code1;
string code2;
string code3;
string code4;
string code5;
string code6;
string code7;
string code8;


cout << endl;
cout << endl;

determinediscount();

code1 = getcode(1);
code2 = getcode(2);
code3 = getcode(3);
code4 = getcode(4);
code5 = getcode(5);
code6 = getcode(6);
code7 = getcode(7);
code8 = getcode(8);

cost1 = getcost(1);
cost2 = getcost(2);
cost3 = getcost(3);
cost4 = getcost(4);
cost5 = getcost(5);
cost6 = getcost(6);
cost7 = getcost(7);
cost8 = getcost(8);

total = cost1 + cost2 + cost3 + cost4 + cost5 + cost6 + cost7 + cost8;


cout << endl;
cout << "Module1: R" << cost1 << endl;
cout << "Module2: R" << cost2 << endl;
cout << "Module3: R" << cost3 << endl;
cout << "Module4: R" << cost4 << endl;
cout << "Module5: R" << cost5 << endl;
cout << "Module6: R" << cost6 << endl;
cout << "Module7: R" << cost7 << endl;
cout << "Module8: R" << cost8 << endl;

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
First error/unnecessary code I spotted:

About 7 lines down:
Remove int main();

You don't need an function prototypes because none of the functions call the others.
Last edited on
don't get it. ohw, ja I didn't understand the concept of Prototyping. Thanks
Topic archived. No new replies allowed.