Help with a very basic C++ program

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-11-2009
Method4ever
Help with a very basic C++ program
okay so enclosed is the program i am working on for class, basically what it does is it will take the provided integers and get the avg of them as well as the difference, smallest and largest number; but my problem is that i need it to be where if the user enters a "0" then the program will go to the end and basically still show the avg of the numbers up till 0, the smallest, largest and difference as well, so if someone could please help/ teach me to do this it would be mostly appreciated. by the way i hope i posted this properly i am just getting used to writing codes.

--------------------------------------------------------------
Code:

```//Charles Shane Miller Jr. #include <cstdlib> #include <iostream> using namespace std; int main() {           int one; // integer uno           int two; // integer dos           int three; // integer tres           int four; // integer cuatro           int five; // integer cinco           int avg; // average           int smallest;           int largest;           int diff;           int zero;                     cout << "So what is lucky number one?" << endl;           cin >> one;           cout << "okay and what shall the terrible second integer be?" << endl;           cin >> two;           cout << "So what is unlucky number 3?" << endl;           cin >> three;           cout << "okay and que es numero cuatro por favor?" << endl;           cin >> four;           cout << "and last but not least, what is lucky number five? " << endl;           cin >> five;           avg = one * two * three * four * five / 5 ; //formula to take integers and get the average           cout << "" << endl;           cout <<"The average of the numbers is " << avg << endl; // outputs the answer in a user friendly way cout << "" << endl; if ( one < two ) // start of algorithm to find smallest number       smallest = one;   else       smallest = two; if ( smallest < three )       smallest = smallest; else       smallest = three; if ( smallest < four )     smallest = smallest;     else     smallest = four; if ( smallest < five )   smallest = smallest;   else   smallest = five;   cout << "the smallest is  " << smallest << endl; // end of algorithm to find smallest number   cout << "" << endl;   if ( one > two ) // start of algorithm to find largest number       largest = one;   else       largest = two; if ( largest > three )       largest = largest; else       largest = three; if ( largest > four )     largest = largest;     else     largest = four; if ( largest > five )   largest = largest;   else   largest = five; if ( 0 )  break;  else  { continue; }   cout << "the largest is  " << largest << endl;   cout << " " << endl;   diff = largest - smallest;   cout << "The difference of the laregst number and the smallest number is " << diff << endl;           system("PAUSE");           while (zero != 0);           return 0;           }```
--------------------------------------
• 02-11-2009
matsp
Code:

```if ( smallest < three )       smallest = smallest;```
So, don't do that, ok? Just reverse the order/direction of the compare, and do the smallest = three if the test shows three is greater...

Code:

```if ( 0 )  break;  else  { continue; }```
Doesn't do ANYTHING　ｕｓｅｆｕｌ。

--
Mats
• 02-11-2009
Method4ever
i meant to take the break out because it doesnt work with the bloodshed dev-c++ program, like it does all the functions i want it to i just want it to have the ability to if the user enters a 0 that it will stop the program and go to the end showing the largest/smallest numbers of the 5 that the user entered already, hope i am explaining it right but i want it to if the user enters a 0 go to the end of the program... :-/
• 02-11-2009
laserlight
You might want to learn about arrays and loops first. The program that you want to write will be much more easily implemented using an array and loops.
• 02-11-2009
Method4ever
any websites to teach this easier? i am sorry to be a hassle just this professor really doesnt teach she talks about it for a second and that is about it other than that i am rather lost :-/ sigh.
• 02-11-2009
QuestionKing
You need to understand:
Code:

`if ( 0 )`
translates to:
Code:

```if ( FALSE ) {     // this will 'never' be run }```
regardless of break or no break, you will always jump to else

Start here with the tutorial on if statements to understand the above, and continue through the end of the array section. (Some of that may or may not apply to what you have covered in class so far, but in either case the tutorial will help you in the long run.)
• 02-11-2009
Method4ever
okay, thanks for the help i will try to figure it out, again thanks!
• 02-11-2009
Method4ever
Code:

`          while (zero != 0);`
now cant i use that to end the program and if i do would it be like this:

Code:

```//Charles Shane Miller Jr. #include <cstdlib> #include <iostream> using namespace std; int main() {           int one; // integer uno           int two; // integer dos           int three; // integer tres           int four; // integer cuatro           int five; // integer cinco           int avg; // average           int smallest;           int largest;           int diff;           int zero;                     cout << "So what is lucky number one?" << endl;           cin >> one;           cout << "okay and what shall the terrible second integer be?" << endl;           cin >> two;           cout << "So what is unlucky number 3?" << endl;           cin >> three;           cout << "okay and que es numero cuatro por favor?" << endl;           cin >> four;           cout << "and last but not least, what is lucky number five? " << endl;           cin >> five;           avg = one * two * three * four * five / 5 ; //formula to take integers and get the average           cout << "" << endl;           cout <<"The average of the numbers is " << avg << endl; // outputs the answer in a user friendly way cout << "" << endl; if ( one < two ) // start of algorithm to find smallest number       smallest = one;   else       smallest = two; if ( smallest < three )       smallest = smallest; else       smallest = three; if ( smallest < four )     smallest = smallest;     else     smallest = four; if ( smallest < five )   smallest = smallest;   else   smallest = five;   cout << "the smallest is  " << smallest << endl; // end of algorithm to find smallest number   cout << "" << endl;   if ( one > two ) // start of algorithm to find largest number       largest = one;   else       largest = two; if ( largest > three )       largest = largest; else       largest = three; if ( largest > four )     largest = largest;     else     largest = four; if ( largest > five )   largest = largest;   else   largest = five;   cout << "the largest is  " << largest << endl;   cout << " " << endl;   diff = largest - smallest;   cout << "The difference of the laregst number and the smallest number is " << diff << endl;           system("PAUSE");           while (zero != 0);           return 0;           }```
thanks again
• 02-11-2009
Method4ever
well now i just need to know i got it working, sorry to bug you all, but how do i set it so when 0 is typed in the program automatically exits?
• 02-11-2009
QuestionKing
Ok. It looks like you have a good understanding of how to create conditionals.
(maybe I should point out while(zero !=0); still may not exactly do what you want it to do)
... now you should get into the structures, arrays, and switch sections, and repost your code after those sections. You should be well under way by the end of those sections.
(your program should be much shorter, include some for loops, and have fewer variables)
Feel free to ask questions if you need to.
• 02-11-2009
Method4ever
yeah reading into them problem with my conditionals lol is if u use a way to end the program prematurely then it messes up the largest number value for some odd reason :-/ bleh back to the drawing board i suppose.
• 02-11-2009
QuestionKing
Stick to it and look over them tutorials, compare to see how and when the tutorials match parts of your program you are writing. Compare to the sample code, and what it can do. You are making progress, good job so far.
• 02-11-2009
QuestionKing
You might also consider editing your posts and removing what appears to be a RL name from your code, but that is up to you. This is a public forum, anyone could be reading...
• 02-11-2009
Method4ever
Code:

```#include <iostream.h> int main () {     int i = 0;     int j,totals,x,z;     double avg;     int k = 0;     int largest = 0;     int smallest = j;     do     {         cout << "Enter a number (enter zero to exit): ";         cin  >> j;         i = i + j;         k = k + 1;         if (j > largest) {largest = j; }         if (j < smallest) {smallest = j;}     }     while (j != 0);     totals = k - 1;     avg = double(i) / (double(k) - 1);     cout << "\n\n\nThe number of numbers is " << totals << endl;     cout << "The average is " << avg << endl;     cout << "the largest number is " << largest << endl;     cout << "The Smallest Number is " << smallest << endl;     system("PAUSE");     return 0; }```
i was alternating between the two codes but now i cant get the smallest code to work properly lol this is interesting may have to resort to the book i swear why does my professor work here if she doesnt go over anything lol...
hehe oops.
• 02-11-2009
QuestionKing
Please edit your post above and move your comment out of the code so wordwrap will adjust the length. ;)

Start with an outline of your program's requirements. What it must do for an end result. Then populate a list of what will be required to accomplish the task. What variables, how many, etc.
Then think of ways some of them relate to one another, see if you can come up with a system that will be easy to understand as well as cutting down on variable use. There is more than one way to do this, but you are not yet using some very nice features you could be. I will start you out with a hint, I see first off you want the user to input 5 integers. There should be an easy way to get and store those with just a few lines of code. (I am about the same level of learning as you, so I am keeping a close eye on this thread incase someone else offers some input)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last