#include <iostream> #include <string> #include <fstream> using namespace std; int main() { int i=0,j=0; string of,search,yn,ad,line,output,text; string syntaxarray[20][3]; ifstream read; ofstream write; void read_file(); read.open("syntax.dat"); write.open("syntax1.dat"); cout<<"Learn C++ with Kok Leong\n"; cout<<"Enter on to start or off to exit>> "; cin>>of; if(of.compare("on")==0) { cout<<"Learn C++ with Kok Leong\n"; cout<<"History of C++:\n"; cout<<"In this semester I have learn many syntax in this subject CSC120." <<"There are if,switch,loop statement.\n"; cout<<"Search by word>> "; cin>>search; while(search!="off") { if(search.compare("argument")==0) { while(read>>text) { syntaxarray[i][j]=text; j++; if(j==3) { j=0; i++; } cout<<text; } } /*else if(search.compare("array")==0) { while(read>>text) { syntaxarray[i][j]=text; j++; if(j==3) { j=0; i++; } cout<<text; } } else if(search.compare("break")==0) { while(read>>text) { syntaxarray[i][j]=text; j++; if(j==3) { j=0; i++; } cout<<text; } } else if(search.compare("continue")==0) { while(read>>text) { syntaxarray[i][j]=text; j++; if(j==3) { j=0; i++; } cout<<text; } } else if(search.compare("declaring")==0) { while(read>>text) { syntaxarray[i][j]=text; j++; if(j==3) { j=0; i++; } cout<<text; } } else if(search.compare("dowhile")==0) { while(read>>text) { syntaxarray[i][j]=text; j++; if(j==3) { j=0; i++; } cout<<text; } } else if(search.compare("for")==0) { while(read>>text) { syntaxarray[i][j]=text; j++; if(j==3) { j=0; i++; } cout<<text; } }*/ else { cout<<search<<" not found\n"; cout<<"Do you want to add "<<"\""<<search<<"\" in a file[Y/N]>> "; cin>>yn; if((yn=="Y")||(yn=="y")) { cout<<"Enter \""<<search<<"\" description>> \n"; cin>>output; write<<output; } else cout<<"thank you\n"; cout<<"Search by word>> "; cin>>search; } cout<<"Search by word>> "; cin>>search; } } else if(of.compare("off")==0) cout<<"Thank you for you use my program\n"; else cout<<"Invalid word\n"; read.close(); write.close(); return 0; } |
Argument ------------- The arguments passed to the functions have been passed by value. This means that when calling a function with parameters, what we have passed to the function were copies of their values but never the variables themselves. ------------- For example: void duplicate (int& a, int& b, int& c){a*=2;b*=2;c*=2;} int main (){int x=1, y=3, z=7; duplicate (x, y, z);cout << "x=" << x << ", y=" << y << ", z=" << z; Array --------- An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. --------- For example:int billy [] = {16, 2, 77, 40, 12071}; int n, result=0; int main (){ for ( n=0 ; n<5 ; n++ ){result += billy[n];}cout << result return 0;} Break -------- Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end. -------- For example:for (n=10; n>0; n--){ cout << n << ", ";if (n==3){cout << "countdown aborted!";break;}} Continue ------------ The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration. ------------ For example:for (int n=10; n>0; n--) {if (n==5) continue;cout << n << ", ";}cout << "FIRE!\n"; Declaring -------------- Defined all of the functions before the first appearance of calls to them in the source code. -------------- For example:void odd (int a);void even (int a); Do-while ----------- Its functionality is exactly the same as the while loop, except that condition in the do-while loop is evaluated after the execution of statement instead of before, granting at least one execution of statement even if condition is never fulfilled. ----------- For example, do {cout << "Enter number (0 to end): ";cin >> n;cout << "You entered: " << n << "\n";} while (n != 0); For --------- Its main function is to repeat statement while condition remains true, like the while loop. But in addition, the for loop provides specific locations to contain an initialization statement and an increase statement. So this loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration. --------- For example:for (int n=10; n>0; n--) {cout << n << ", "; |
For --------- Its main function is to repeat statement while condition remains true, like the while loop. But in addition, the for loop provides specific locations to contain an initialization statement and an increase statement. So this loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration. --------- For example:for (int n=10; n>0; n--) {cout << n << ", "; |