Saturday, May 22, 2010

What am i doing wrong with my C++ program?

#include %26lt;iostream.h%26gt;


int main() {


int num, num2,avg, numValues, numValues2;


double maxNum, minNum;


int sum = 0;





numValues2 = 1;


do{


cout %26lt;%26lt; "Enter a value in the range of 5 to 10: ";


cin %26gt;%26gt; num2;


if(num2 %26lt;= 5 || num2 %26gt;= 10)


cout %26lt;%26lt; "This value is not in the proper range please try again" %26lt;%26lt; endl;


numValues2++;


}while (numValues2 %26gt; 0 %26amp;%26amp; numValues2 %26lt; 5);





maxNum = num;


minNum = num;


numValues = 1;


do {


cout %26lt;%26lt; "Enter number" %26lt;%26lt; numValues %26lt;%26lt; ": " ;


cin %26gt;%26gt; num;


if (num %26gt; maxNum)


maxNum = num;


if (num %26lt; minNum)


minNum = num;


sum+= num;


numValues++;


avg = sum / numValues;


}while (numValues %26gt; 0 %26amp;%26amp; numValues %26lt; 8);


cout %26lt;%26lt; endl;


cout %26lt;%26lt; "The Sum is: " %26lt;%26lt; sum %26lt;%26lt; endl;


cout %26lt;%26lt; "The Average is: " %26lt;%26lt; avg %26lt;%26lt; endl;


cout %26lt;%26lt; "The largest number is: " %26lt;%26lt; maxNum %26lt;%26lt; endl;


cout %26lt;%26lt; "The smallest number is: " %26lt;%26lt; minNum %26lt;%26lt; endl;





return 0;


}

What am i doing wrong with my C++ program?
Well from what I see the largest number is right.





I cant see why the smallest number isn't right and i dont know what you mean by stopping the loop.





Edit:


I found it change





int num, num2,avg, numValues, numValues2;


double maxNum, minNum;


to





int num2, avg, numValues, numValues2;


double num, maxNum, minNum;
Reply:Always assign default values to your variables. Not assigning default values to your variables is what's causing problems.





Another thing, when you know how many times users are going to input numbers then use a for-loop not a do-while or a while loop.





Also, num, sum and avg need to be doubles. Here is a cleaner version of your code:





#include %26lt;iostream.h%26gt;


int main()


{


int numValues = 0;


double sum = 0;


double currentNum = 0;


double maxNum = 0.0;


double minNum = 0.0;





do{


cout %26lt;%26lt; "Enter a value in the range of 5 to 10: ";


cin %26gt;%26gt; numValues ;


if(numValues %26lt;= 5 || numValues %26gt;= 10)


cout %26lt;%26lt; "This value is not in the proper range please try again" %26lt;%26lt; endl;


}while (numValues %26lt;= 5 || numValues %26gt;= 10);








for(int i = 0; i %26lt; numValues; i++)


{


cout %26lt;%26lt; "Enter number" %26lt;%26lt; (i + 1) %26lt;%26lt; ": " ;


cin %26gt;%26gt; currentNum;


if(i == 0)


maxNum = minNum = currentNum;


else


{


if (currentNum%26gt; maxNum)


maxNum = currentNum;


else if (currentNum%26lt; minNum)


minNum = currentNum;


}


sum+= currentNum;


}





avg = sum / (double) numValues;


cout %26lt;%26lt; endl;


cout %26lt;%26lt; "The Sum is: " %26lt;%26lt; sum %26lt;%26lt; endl;


cout %26lt;%26lt; "The Average is: " %26lt;%26lt; avg %26lt;%26lt; endl;


cout %26lt;%26lt; "The largest number is: " %26lt;%26lt; maxNum %26lt;%26lt; endl;


cout %26lt;%26lt; "The smallest number is: " %26lt;%26lt; minNum %26lt;%26lt; endl;





return 0;


}
Reply:I found few minor mistakes in your program.





They are wrong declaration of variables.


Using do - while instead of while or for loop


arg variable should be outside the loop


etc..





so i have typed your program in my own style





Hope you will like it. If you have any problem call me at m_gopi_m@yahoo.co.in





#include%26lt;iostream.h%26gt;


#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





void main()


{


int i,j,n,num,max,min;


double sum=0,avg;





clrscr();


cout%26lt;%26lt;"\n\n Enter the number of numbers: ";


cin%26gt;%26gt;n;





cout%26lt;%26lt;"\n Enter the numbers,\n\n";


cout%26lt;%26lt;" num 1 : ";


cin%26gt;%26gt;num;





max=num;


min=num;


sum=num;





for(i=1;i%26lt;n;i++)


{


if(num%26gt;max)


max=num;


else if(num%26lt;min)


min=num;





sum+=num;





cout%26lt;%26lt;" num "%26lt;%26lt;i+1%26lt;%26lt;" : ";


cin%26gt;%26gt;num;


}


avg=sum/n;





clrscr();


cout%26lt;%26lt;"\n\n\n\n\t\t\t\tRESULTS " ;


cout%26lt;%26lt;"\n\n The smallest number is\t\t"%26lt;%26lt;min;


cout%26lt;%26lt;"\n The biggest number is\t\t"%26lt;%26lt;max;


cout%26lt;%26lt;"\n The sum of the given number is "%26lt;%26lt;sum;


cout%26lt;%26lt;"\n Their average is\t\t"%26lt;%26lt;avg;


getch();


}
Reply:I would love to help you, but from your code it isn't clear what you're trying to do --I'm afraid your code is just THAT messy...





Note that num should probably be a double, and avg shouldn't be calculated inside the loop, but outside of it. That's just two major bugs that I see, but please describe what you want your program to do in English so I can give you a complete answer.


No comments:

Post a Comment