| | |
Problem with how program runs.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 17
Reputation:
Solved Threads: 0
I am currently writing a simple program that matches a already written program, with certain specifications that are required. For the most part I have written what seems like working code, but I do have problems.
Here is the program that I have to make: Sample
1st problem: When the program first runs, it goes to updating the array, but the program ends after that. It's supposed to start over again, letting the user choose what they want to do next.
2nd problem: How do I display the printPostfix when displaying the array or updating it.
I know there is a lot of code below, but I've been trying for a while on how to fix the issue.
"menuFoos.h" header file
"arrFoos.h" header file
Here is the program that I have to make: Sample
1st problem: When the program first runs, it goes to updating the array, but the program ends after that. It's supposed to start over again, letting the user choose what they want to do next.
2nd problem: How do I display the printPostfix when displaying the array or updating it.
I know there is a lot of code below, but I've been trying for a while on how to fix the issue.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <cctype> #define myline cout << "==----------------------------------------------==\n" #define mytab cout<<"\t" #define line cout <<"\n+---------------------------------------------+\n"; using namespace std; bool firstTry = true; #include "menuFoos.h" #include "arrFoos.h" int main() { char again; int lower = 0, upper = 6; //define my array const int size = 5; double arr[size]; //start of do-while to control a text menu interface do{ displayMenu(); //first time, must start from option 1. //not first time, free to choose options. while(firstTry == true) { { cout << "It Is Your First Try, We Will Create My Array First:\n"; updateArr(arr, size); } return firstTry = false; } int op = option(upper, lower); if(op == 0) { return 0; } else if(op == 1) { updateArr(arr, size); } else if(op == 2) { displayArr(arr, size); } else if(op == 3) { sumArr(arr, size); } else if(op == 4) { aveArr(arr, size); } else if(op == 5) { minArr(arr, size); } else maxArr(arr, size); char again = choice(); }while(again=='y'); //end of do-while system("pause"); return 0; }
"menuFoos.h" header file
C++ Syntax (Toggle Plain Text)
//function prototype void displayMenu( ); //display a text menu char choice( ); // get user�s answer on yes or no int option(int, int); //read user�s option in the text menu //function definitions char again; //not completed // get user�s answer on yes or no char choice() { //insert code.... { line; cout << "Want To Know More About Your Array? y(yes) or n(no):"; line; cin >> again; } return again; } //read user�s option in the text menu int option(int upper, int lower) { int op; cout<<"Enter "<<lower<< "-"<<upper <<", "<< lower <<" for exit:"<<endl; cin >> op; while(cin.fail() || op > upper || op < lower) { cin.clear(); fflush(stdin); //for windows. cout << "You did not enter valid option, " << "please enter again:" << lower <<" ~ "<< upper<<":\n"; cin >> op; } return op; } void displayMenu() { //system("command /c cls"); cout << endl << endl; cout << "\t\t+++++++++++++++++++++++++++++++++++++++++++\n\n"; cout << "\t\t\t Play with my Array now " << endl << endl; cout << "\t\t+++++++++++++++++++++++++++++++++++++++++++\n"; cout << "\t\t\t1: creating/updating my array" << endl; cout << "\t\t\t2: displaying my array" << endl; cout << "\t\t\t3: summing all the elements" << endl; cout << "\t\t\t4: averaging the array" << endl; cout << "\t\t\t5: largest element and its index" << endl; cout << "\t\t\t6: smallest element and its index" << endl; cout << "\t\t\t0: exit" << endl; cout << "\t\t+++++++++++++++++++++++++++++++++++++++++++\n\n"; cout << "\x3e"<<" Enter an integer (0-6), \n" ; cout << "\x3e"<<" Enter 0 to exit the program\n\n"; }
"arrFoos.h" header file
C++ Syntax (Toggle Plain Text)
// data input functions double getNumber( ); // read a number from keyboard void printPostfix(int); // print some // array manipulations functions void updateArr(double [ ], int); //create or update an array double sumArr(double [ ], int); //return the sum of an array double aveArr(double [ ], int); // return the average of an array void displayArr(double [ ], int); //display array elements double minArr(double [ ], int); //find and display the smallest element double maxArr(double [ ], int); //find and display the largest element //function definitions int i; //not completed: //read a number from keyboard double getNumber() { double num; cout << "Enter a number"; cin >> num; return num; } //complete void printPostfix(int n) { if ((n % 10 == 1) && !(n == 11)) cout << "st"; else if ((n % 10 == 2) && !(n == 12)) cout << "nd"; else if ((n % 10 == 3) && !(n == 13)) cout << "rd"; else cout << "th"; return; } //not completed //create or update an array void updateArr(double arr[], int size) { myline; cout<<" ---> Create/update my array by entering 5 integers:\n"; myline; int elementNumber = 0; for(int i = 0; i < size; i++) { cout << "Enter " << elementNumber << printPostfix << " Element: "; cin >> arr[i]; elementNumber++; } myline; cout<<"Congraduations! my array has been created " <<"or updated.\n\n"; return; } //not completed. double sumArr(double arr[], int size) { double sum = 0; for(int i = 0; i < size; i++) { sum += arr[i]; return sum; } } //not completed double aveArr(double arr[], int size) { double sum = 0; double avg = 0; for(int i = 0; i < size; i++) { sum += arr[i]; return sum; } return sum / size; } //not completed void displayArr(double arr[], int size) { cout<<"\n Displaying my Array:"; for(int i = 0; i < size; i++) { cout << "The 0" << printPostfix << "Element: " << arr[i] << endl; cout << "The 1" << printPostfix << "Element: " << arr[i] << endl; cout << "The 2" << printPostfix << "Element: " << arr[i] << endl; cout << "The 3" << printPostfix << "Element: " << arr[i] << endl; cout << "The 4" << printPostfix << "Element: " << arr[i] << endl; } cout << endl; } }
cpp Syntax (Toggle Plain Text)
while(firstTry == true) { { cout << "It Is Your First Try, We Will Create My Array First:\n"; updateArr(arr, size); } return firstTry = false; //Don't return here, hehe. }
Should be something along the lines of..
cpp Syntax (Toggle Plain Text)
if(firstTry == true) { cout << "It Is Your First Try, We Will Create My Array First:\n"; updateArr(arr, size); firstTry = false; }
Last edited by Clockowl; Apr 11th, 2009 at 9:03 pm.
•
•
Join Date: Apr 2009
Posts: 17
Reputation:
Solved Threads: 0
•
•
•
•
cpp Syntax (Toggle Plain Text)
while(firstTry == true) { { cout << "It Is Your First Try, We Will Create My Array First:\n"; updateArr(arr, size); } return firstTry = false; //Don't return here, hehe. }
Should be something along the lines of..
cpp Syntax (Toggle Plain Text)
if(firstTry == true) { cout << "It Is Your First Try, We Will Create My Array First:\n"; updateArr(arr, size); firstTry = false; }
![]() |
Similar Threads
- Simple getline problem (C++)
- Elapse Time Since Program Start Working (C++)
- Problem with Program ...Need some advice.. (Java)
- serious crashing problem in Visual C++ 6.0 (C++)
- Help. This is a strange problem (C++)
- My Program runs but not correctly (C++)
- Problem trying to run old fortran based program in win xp (Windows NT / 2000 / XP)
- problem with my program!! (C++)
- Major Problem (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: Truncate file name
- Next Thread: scoring algorithm for sentences in a text
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





