### Riemann sum program not running

Hey guys. I just finished writing this program and it's not running. I'm thinking that there is something wrong with the syntax? Any hints would be appreciated. Thank you.

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 1 #include 2 #include 3 using namespace std; 4 5 //equations 6 double f1(double x){ 7 return ((2*pow(x,5))+(pow(x,3))-(10*x)+(2)); 8 } 9 10 double f2(double x){ 11 return ((6*pow(x,2))-(x)+(10)); 12 } 13 14 double f3(double x){ 15 return ((5*x)+(3)); 16 } 17 18 double f4(double x){ 19 return ((2*pow(x,3))+(120)); 20 } 21 22 double f5(double x){ 23 return (2*pow(x,2)); 24 } 25 26 27 28 29 //this function calculates the area of rectangles and adds them up 30 double rect_area(double a, double b, double n, int func_choice){ 31 double area =0; 32 double x=0; 33 double width; 34 width =((b-a)/n); 35 36 for(int i=1; i<=n; i++){ 37 x+=width; 38 if(func_choice==1){ 39 area += (f1(x) * width); 40 } 41 if(func_choice==2){ 42 area+= (f2(x) * width); 43 } 44 if(func_choice==3){ 45 area+= (f3(x) * width); 46 } 47 if(func_choice==4){ 48 area+= (f4(x) * width); 49 } 50 if(func_choice==5){ 51 area+= (f5(x) * width); 52 } 53 } 54 return area; 55 } 56 57 //this function calculates the area of trapezoids and adds them up 58 double trap_area(double a, double b, double n, int func_choice){ 59 double area = 0; 60 double x = 0; 61 double width; 62 width = ((b-a)/n); 63 64 for(int i; i<=n; i++){ 65 x+=width; 66 if(func_choice==1){ 67 area += (f1(x) * width); 68 } 69 if(func_choice==2){ 70 area+= (f2(x) * width); 71 } 72 if(func_choice==3){ 73 area+= (f3(x) * width); 74 } 75 if(func_choice==4){ 76 area+= (f4(x) * width); 77 } 78 if(func_choice==5){ 79 area+= (f5(x) * width); 80 } 81 } 82 return area; 83 } 84 85 86 int main(){ 87 int reset; 88 int func_choice; 89 double a, b; 90 int method; 91 double rect_num; 92 double trap_num; 93 double width; 94 double area = 0; 95 96 //do while loop to repeat if user wants 97 do{ 98 //user picks a function 99 cout << "Choose a function(1,2,3,4,5,other(quit)): " << endl; 100 cout << "f1(x)=2x^5 + x^3 - 10x +2" << endl; 101 cout << "f2(x)=6x^2 -x + 10" << endl; 102 cout << "f3(x)=5x + 3 " << endl; 103 cout << "f4(x)=2x^3 +120 " << endl; 104 cout << "f5(x)=2x^2 " << endl; 105 cin >> func_choice; 106 107 //if user chooses something other than a given number, program quits 108 if(func_choice < 1 || func_choice > 5){ 109 return 0; 110 } 111 112 //method of integration 113 cout <<"Which method would you like to use?(rectangle(1), trapezoid(2), both(3)): " << endl; 114 cin >> method; 115 116 //starting values 117 cout <<"Input a start value: " << endl; 118 cin >> a; 119 cout <<"Input an end value: " << endl; 120 cin >> b; 121 122 //calculates area based on users choice 123 switch(method){ 124 case 1: 125 cout <<"How many rectangles would you like?" << endl; 126 cin >> rect_num; 127 area = rect_area(a,b,rect_num,func_choice); 128 cout << "The area of " << func_choice << " is " << area << " from " << a << " to " << b << " with " << rect_num << " rectangles " << endl; 129 break; 130 131 case 2: 132 cout <<"How many trapezoids would you like?" << endl; 133 cin >> trap_num; 134 area = trap_area(a,b,trap_num,func_choice); 135 cout << "The area of " << func_choice << " is " << area << " from " << a << " to " << b << " with " << trap_num << " trapezoids " << endl; 136 break; 137 138 case 3: 139 cout <<"How many rectangles would you like?" << endl; 140 cin >> rect_num; 141 cout <<"How many trapezoids would you like?" << endl; 142 cin >> trap_num; 143 144 area = rect_area(a,b,rect_num,func_choice); 145 cout << "The area of " << func_choice << " is " << area << " from " << a << " to " << b << " with " << rect_num << " rectangles " << endl; 146 area = trap_area(a,b,trap_num,func_choice); 147 cout << "The area of " << func_choice << " is " << area << " from " << a << " to " << b << " with " << trap_num << " trapezoids " << endl; 148 break; 149 150 default: 151 while(method<1, method>3){ 152 cout << "not a valid input" << endl; 153 cout << "please choose a given method" << endl; 154 method =0; 155 cin >> method; 156 } 157 } 158 159 //question to recieve input for the condition of the do while loop 160 cout << "would you like to compute another area?(1 for yes, 0 for no)" << endl; 161 cin >> reset; 162 163 }while(reset==1); 164 return 0; 165 }
Let me make the problem more clear. The program compiles, but there is no output and it keeps running as if it is in an infinite loop.
I don't know where exactly the the problem is, but what is this?
at line: 151
 while(method<1, method>3)
It is supposed to force the user to pick one of the three methods. I'm new to switches, so I wasn't completely sure if it would work.
@apati21 maybe this is the -infinite loop- but im not sure
You don't need the while loop in the default switch case. Anything that is not 1, 2, or 3 will go to the default switch case and display the error.

Unless the user inputs a string, but that's a different error.
Thanks guys. I figured it out what was wrong. The word I was using to start the program after I compiled was a reserved word in unix. I took the while loop out. Does anyone know how I would get the program to ask for another input if they don't input one of the 3 choices?
I think that you could do this

 1234567891011121314151617181920212223 method=0; //<-- //starting values cout<<"Input a start value: " << endl; cin >> a; cout<<"Input an end value: " << endl; cin >> b; while(method<1||method>3){ //method of integration cout <<"Which method would you like to use?(rectangle(1), trapezoid(2), both(3)): " << endl; cin >> method; switch(method){ case 1: //your code case 2: //your code case 3: //your code default: cout << "not a valid input\nplease choose a given method" << endl; }//end switch }//end loop while
Topic archived. No new replies allowed.