Hey everyone,
I'm fairly new to programming. I've done C and i'm now learning C++. According to my curriculum i'm now considered an intermediate programmer. But I still consider myself an amateur!
Well here is the prgm i'm having problems with:
I'm using C++ Borland Builder Ver. 6 and I can't get the switches to work properly!Code:#include <iostream> #include <stdlib.h> #include <iomanip> #include <conio.h> using namespace std; template <typename Type> int indsearch (Type *array, int size, Type vsearch) { for (int x=0; x<=size; x++) { if (array[x]==vsearch) return x+1; } return -1; } template <typename Type> int countx (Type *array, int size, Type vsearch) { int occur=0; for (int x=0; x<=size; x++) { if (array[x]==vsearch) occur++; } return occur; } template <typename Type> Type maxValue (Type *array, int size) { Type maxv = array[0]; for (int x=1; x<=size; x++) { if (array[x]>maxv) maxv=array[x]; } return maxv; } int main() { cout<<setiosflags (ios::fixed); cout<<setprecision(2); int isize, irange, isearch; //?size: how many elements in array. ?range: range for random #'s int *iptr; //?search: Number to be searched for in array float fsize, frange, fsearch; float *fptr; cout<<"Please enter INTEGER array size: "; cin>>isize; iptr=new int [isize]; if (iptr==0) {exit(1);} //not working cout<<endl<<"Please enter INTEGER array range: "; cin>>irange; cout<<endl<<"Please enter FLOAT array size: "; cin>>fsize; fptr=new float [fsize]; if (!fptr) {exit(1);} //not working cout<<endl<<"Please enter FLOAT array range: "; cin>>frange; //************************ARRAY SIZE AND RANGE RETRIEVAL END****************** randomize(); //random number generator for INT for (int i=0; i<isize; i++) { iptr[i] = random(irange)+1; cout<<iptr[i]<<endl; } cout<<endl; for (int j=0; j<fsize; j++) //random number generator for FLOAT { fptr[j] = frange*((float)rand()/RAND_MAX); //(float)rand()/RAND_MAX will provide cout<<fptr[j]<<endl; //a product between 0-1 as a float. } int choice; do{ cout<<setw(20)<<endl<<"MAIN MENU"; cout<<endl<<endl<<"Please Choose an Array to check: "; cout<<endl<<"1. Integer Array"; cout<<endl<<"2. Float Array"; cout<<endl<<"3. Exit"; cout<<endl<<"-> "; cin>>choice; //clrscr(); if (choice==1) { do{ cout<<endl<<"1. Search for value in array"; cout<<endl<<"2. Check number of occurences in array"; cout<<endl<<"3. Check maximum value in array"; cout<<endl<<"4. Go Back"; cout<<endl<<"-> "; cin>>choice; //clrscr(); switch(choice) { case 1: { cout<<endl<<"Please enter value to be searched: "; cin>>isearch; isearch=indsearch (iptr, isize, isearch); if (isearch >= 0) cout<<endl<<"VALUE FOUND--INDEX: "<<isearch<<endl; else cout<<endl<<"VALUE NOT FOUND"<<endl; cout<<endl<<endl<<"Press any key to continue.."; getch(); //clrscr(); } break; case 2: { cout<<endl<<"Please enter value to be searched: "; cin>>isearch; int ioccur=countx(iptr, isize, isearch); cout<<endl<<"Value was found "<<ioccur<<" time(s)"; cout<<endl<<endl<<"Press any key to continue.."; getch(); //clrscr(); } break; case 3: { int imax=maxValue (iptr, isize); cout<<endl<<"The maximum value is: "<<imax<<endl; } break; } }while(choice!=4); //leaves 1st switch } if (choice==2) { do{ cout<<endl<<"1. Search for value in array"; cout<<endl<<"2. Check number of occurences in array"; cout<<endl<<"3. Check maximum value in array"; cout<<endl<<"4. Go Back"; cout<<endl<<"-> "; cin>>choice; //clrscr(); switch(choice) { case 1: { cout<<endl<<"Please enter value to be searched: "; cin>>fsearch; fsearch=indsearch (fptr, fsize, fsearch); if (fsearch >= 0) cout<<endl<<"VALUE FOUND"; else cout<<endl<<"VALUE NOT FOUND"; cout<<endl<<endl<<"Press any key to continue.."; getch(); //clrscr(); } break; case 2: { cout<<endl<<"Please enter value to be searched: "; cin>>fsearch; int foccur=countx(fptr, fsize, fsearch); cout<<endl<<"Value was found "<<foccur<<" time(s)"; cout<<endl<<endl<<"Press any key to continue.."; getch(); //clrscr(); } break; case 3: { float fmax = maxValue (fptr, fsize); cout<<endl<<"The maximum value is: "<<fmax<<endl; } break; } }while (choice!=4); //leaves 2nd switch } }while (choice!=3); //quits main program getch(); delete []iptr; delete []fptr; }
1st Switch:
-Case 1: OKAY
-Case 2: OKAY
-Case 3: ALWAYS RETURNS 40
2nd Switch:
-Case 1: 'VALUE NOT FOUND'
-Case 2: 'Appears 0 time(s)'
-Case 3: OKAY
I've been working on this prgm for a few days now and can't fix it!
Please help a newbie?