Problem with how program runs.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2009
Posts: 17
Reputation: JainishP is an unknown quantity at this point 
Solved Threads: 0
JainishP JainishP is offline Offline
Newbie Poster

Problem with how program runs.

 
0
  #1
Apr 11th, 2009
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.

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cctype>
  4.  
  5. #define myline cout << "==----------------------------------------------==\n"
  6. #define mytab cout<<"\t"
  7. #define line cout <<"\n+---------------------------------------------+\n";
  8.  
  9. using namespace std;
  10.  
  11. bool firstTry = true;
  12.  
  13. #include "menuFoos.h"
  14. #include "arrFoos.h"
  15.  
  16. int main()
  17. {
  18.  
  19. char again;
  20.  
  21. int lower = 0, upper = 6;
  22.  
  23. //define my array
  24. const int size = 5;
  25. double arr[size];
  26.  
  27. //start of do-while to control a text menu interface
  28. do{
  29. displayMenu();
  30.  
  31. //first time, must start from option 1.
  32. //not first time, free to choose options.
  33.  
  34. while(firstTry == true)
  35. {
  36. {
  37. cout << "It Is Your First Try, We Will Create My Array First:\n";
  38. updateArr(arr, size);
  39. }
  40. return firstTry = false;
  41. }
  42.  
  43. int op = option(upper, lower);
  44.  
  45. if(op == 0)
  46. {
  47. return 0;
  48. }
  49. else if(op == 1)
  50. {
  51. updateArr(arr, size);
  52. }
  53. else if(op == 2)
  54. {
  55. displayArr(arr, size);
  56. }
  57. else if(op == 3)
  58. {
  59. sumArr(arr, size);
  60. }
  61. else if(op == 4)
  62. {
  63. aveArr(arr, size);
  64. }
  65. else if(op == 5)
  66. {
  67. minArr(arr, size);
  68. }
  69. else
  70. maxArr(arr, size);
  71.  
  72.  
  73. char again = choice();
  74.  
  75. }while(again=='y'); //end of do-while
  76.  
  77. system("pause");
  78. return 0;
  79. }

"menuFoos.h" header file
  1. //function prototype
  2. void displayMenu( ); //display a text menu
  3. char choice( ); // get user�s answer on yes or no
  4. int option(int, int); //read user�s option in the text menu
  5.  
  6.  
  7. //function definitions
  8. char again;
  9.  
  10. //not completed
  11. // get user�s answer on yes or no
  12. char choice()
  13. {
  14. //insert code....
  15. {
  16. line;
  17. cout << "Want To Know More About Your Array? y(yes) or n(no):";
  18. line;
  19. cin >> again;
  20. }
  21. return again;
  22. }
  23.  
  24. //read user�s option in the text menu
  25. int option(int upper, int lower)
  26. {
  27. int op;
  28. cout<<"Enter "<<lower<< "-"<<upper
  29. <<", "<< lower <<" for exit:"<<endl;
  30. cin >> op;
  31. while(cin.fail() || op > upper || op < lower)
  32. {
  33. cin.clear();
  34. fflush(stdin); //for windows.
  35. cout << "You did not enter valid option, "
  36. << "please enter again:"
  37. << lower <<" ~ "<< upper<<":\n";
  38. cin >> op;
  39. }
  40. return op;
  41. }
  42.  
  43. void displayMenu()
  44. {
  45. //system("command /c cls");
  46. cout << endl << endl;
  47. cout << "\t\t+++++++++++++++++++++++++++++++++++++++++++\n\n";
  48. cout << "\t\t\t Play with my Array now " << endl << endl;
  49. cout << "\t\t+++++++++++++++++++++++++++++++++++++++++++\n";
  50. cout << "\t\t\t1: creating/updating my array" << endl;
  51. cout << "\t\t\t2: displaying my array" << endl;
  52. cout << "\t\t\t3: summing all the elements" << endl;
  53. cout << "\t\t\t4: averaging the array" << endl;
  54. cout << "\t\t\t5: largest element and its index" << endl;
  55. cout << "\t\t\t6: smallest element and its index" << endl;
  56. cout << "\t\t\t0: exit" << endl;
  57. cout << "\t\t+++++++++++++++++++++++++++++++++++++++++++\n\n";
  58. cout << "\x3e"<<" Enter an integer (0-6), \n" ;
  59. cout << "\x3e"<<" Enter 0 to exit the program\n\n";
  60. }

"arrFoos.h" header file
  1. // data input functions
  2. double getNumber( ); // read a number from keyboard
  3. void printPostfix(int); // print some
  4.  
  5. // array manipulations functions
  6. void updateArr(double [ ], int); //create or update an array
  7. double sumArr(double [ ], int); //return the sum of an array
  8. double aveArr(double [ ], int); // return the average of an array
  9. void displayArr(double [ ], int); //display array elements
  10. double minArr(double [ ], int); //find and display the smallest element
  11. double maxArr(double [ ], int); //find and display the largest element
  12.  
  13. //function definitions
  14. int i;
  15.  
  16.  
  17. //not completed:
  18. //read a number from keyboard
  19. double getNumber()
  20. {
  21. double num;
  22. cout << "Enter a number";
  23. cin >> num;
  24. return num;
  25. }
  26.  
  27. //complete
  28. void printPostfix(int n)
  29. {
  30. if ((n % 10 == 1) && !(n == 11))
  31. cout << "st";
  32. else if ((n % 10 == 2) && !(n == 12))
  33. cout << "nd";
  34. else if ((n % 10 == 3) && !(n == 13))
  35. cout << "rd";
  36. else
  37. cout << "th";
  38. return;
  39. }
  40.  
  41.  
  42. //not completed
  43. //create or update an array
  44. void updateArr(double arr[], int size)
  45. {
  46. myline;
  47. cout<<" ---> Create/update my array by entering 5 integers:\n";
  48. myline;
  49.  
  50. int elementNumber = 0;
  51.  
  52. for(int i = 0; i < size; i++)
  53. {
  54. cout << "Enter " << elementNumber << printPostfix << " Element: ";
  55. cin >> arr[i];
  56. elementNumber++;
  57. }
  58.  
  59. myline;
  60.  
  61. cout<<"Congraduations! my array has been created "
  62. <<"or updated.\n\n";
  63. return;
  64. }
  65. //not completed.
  66. double sumArr(double arr[], int size)
  67. {
  68. double sum = 0;
  69. for(int i = 0; i < size; i++)
  70. {
  71. sum += arr[i];
  72. return sum;
  73. }
  74. }
  75.  
  76. //not completed
  77. double aveArr(double arr[], int size)
  78. {
  79. double sum = 0;
  80. double avg = 0;
  81. for(int i = 0; i < size; i++)
  82. {
  83. sum += arr[i];
  84. return sum;
  85. }
  86. return sum / size;
  87. }
  88.  
  89. //not completed
  90. void displayArr(double arr[], int size)
  91. {
  92. cout<<"\n Displaying my Array:";
  93. for(int i = 0; i < size; i++)
  94. {
  95. cout << "The 0" << printPostfix << "Element: " << arr[i] << endl;
  96. cout << "The 1" << printPostfix << "Element: " << arr[i] << endl;
  97. cout << "The 2" << printPostfix << "Element: " << arr[i] << endl;
  98. cout << "The 3" << printPostfix << "Element: " << arr[i] << endl;
  99. cout << "The 4" << printPostfix << "Element: " << arr[i] << endl;
  100. }
  101. cout << endl;
  102. }
  103. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Problem with how program runs.

 
0
  #2
Apr 11th, 2009
  1. while(firstTry == true)
  2. {
  3. {
  4. cout << "It Is Your First Try, We Will Create My Array First:\n";
  5. updateArr(arr, size);
  6. }
  7. return firstTry = false; //Don't return here, hehe.
  8. }

Should be something along the lines of..

  1. if(firstTry == true)
  2. {
  3. cout << "It Is Your First Try, We Will Create My Array First:\n";
  4. updateArr(arr, size);
  5. firstTry = false;
  6. }
Last edited by Clockowl; Apr 11th, 2009 at 9:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 17
Reputation: JainishP is an unknown quantity at this point 
Solved Threads: 0
JainishP JainishP is offline Offline
Newbie Poster

Re: Problem with how program runs.

 
0
  #3
Apr 12th, 2009
Originally Posted by Clockowl View Post
  1. while(firstTry == true)
  2. {
  3. {
  4. cout << "It Is Your First Try, We Will Create My Array First:\n";
  5. updateArr(arr, size);
  6. }
  7. return firstTry = false; //Don't return here, hehe.
  8. }

Should be something along the lines of..

  1. if(firstTry == true)
  2. {
  3. cout << "It Is Your First Try, We Will Create My Array First:\n";
  4. updateArr(arr, size);
  5. firstTry = false;
  6. }
Thanks, that worked. I got the program working.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Problem with how program runs.

 
0
  #4
Apr 12th, 2009
Great, no problem. Good question-post. I take it for granted that you know why the program works like it does now?
Last edited by Clockowl; Apr 12th, 2009 at 6:19 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC