Error Correction in C++

  • Thread starter Thread starter puneet28
  • Start date Start date
  • Replies Replies 13
  • Views Views 7,674

puneet28

InsanE
Messages
295
Location
Roorkee
ISP
Hostel LAN - I think 100MBPs :P
Can you please rectify error in this program.

I am not getting output of last three cout statements.

Code:
#include <iostream>
#include <cmath>
using namespace std;
float vel (float, float);
int main ()
{
    float p,hg,h,d,w,qc,qr,bs,ac,wpr,s,r,v,ss1,ss2,dl=8.0,b=1.5;
    cout<<"Enter Power Potential in MW = ";
    cin>>p;
    cout <<"Enter Gross Head in meters = ";
    cin>>hg;
    float x,y,z;
    cout<<"\nFor Lined Channel ..press 0\nFor Unlined Channel ..press 1\n";
    cin>>x;
    cout<<"\nFor Channel in Cutting ..press 2\nFor Channel in Filling ..press 3\n";
    cin>>y;
    cout<<"\nFor Power Channel ..press 4\nFor Head Race Channel ..press 5\n";
    cin>>z;
    if (x==0 && y==0)
    {
        cout<<"Channel is Lined and Section is in Cutting\n";
        ss1=1, ss2=1;
    }
    else if (x==0 && y==1)
    {
        cout<<"Channel is Lined and Section is in Filling\n";
        ss1=1, ss2=1.5;
    }
    else if (x==1 && y==0)
    {
        cout<<"Channel is Unlined and Section is in Cutting\n";
        ss1=1, ss2=1.5;
    }
    else
    {
        cout<<"Channel is Unlined and Section is in Filling\n";
        ss1=1, ss2=2;
    }
    if (z=4)
    {
        cout<<"Power Channel\n";
        bs=(1/200);
    }
    else  
    {
        cout<<"Head Race Channel";
        bs=(1/400);
    }
    h=0.9*hg;
    qr=(p*1000)/(9.81*0.80*h);
    qc=0.0;
    d=0.0;
    while(qc<qr)
    {
        d=d+0.005;
        if (d<=4.0)
        {
            w=d;
            ac=w*d;
            wpr=(w+(2*d));
        }
        else if (d<=8.0)
        {
            w=2*d;
            ac=w*(w+ss1*d);
            wpr= (w+ (2*d*sqrt(pow(ss1,2.0)+1)));
        }
        else
        {
            w=2*d;
            ac=(w+ss1*dl)*dl+(w+2*ss1*dl+2*b+ss2*(d-dl)*(d-dl));
            wpr=(w+(2*dl*sqrt(1+pow(ss1,2.0))+(2*b)+(2*(d-dl)*sqrt(1+pow(ss2,2.0)))));
        }
        r=ac/wpr;
        v=vel(r,bs);
        qc=ac*v;
    }
    cout<<"Depth of the channel = "<<d<<endl;
    cout<<"Width of the channel = "<<w<<endl;
    cout<<"Discharge of channel = "<<qc<<endl;
}
float vel (float r,float bs)
{
    float n=0.018,vc;
    vc=(1/n)*sqrt(bs)*pow(r,2.0/3.0);
    return vc;
}
 
if (z=4). It should be if (z==4).
 
OMG cannot solve these equations, bcz I am a CS branch student and this is irrelevant to me.

One is for sure it should be z==4 and there can be chances of type mismatch or value over flow.

Float value is ranged 1.8E-38 3.4E+38

so if your value exceed this range used unsigned float. This might solve your problem
 
Man i gotta say use some some indentations while writing code otherwise it's eye soaring for the others...... No offense
 
looks like your code is stuck over here while(qc<qr) because when the condition is met there's no exit point and the while loop keeps running
 
Also I don't see why you are using decimal numbers with pow function. A float number raised to anything will return a floating value only.
 
Also I don't see why you are using decimal numbers with pow function. A float number raised to anything will return a floating value only.

your trust from IIT brand will be broken, if I tell you reality. Prof here marked mistake in my tutorial sheet when I wrote 2 in place of 2.0 for float 😀 😀
 

Top