Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-25-2007
stressedstudent
I can not seem to get this program to work, I am having no luck with loops for anything, very lost. This is my latest attempt and it is not working. Any help I can get would be greatly appriciated. Thank you in advance.
Code:

``` // This program should use a loop that lets the user enter a series of integers. // The user should enter -99 to signal the end of the series. // After all the numbers have been entered, // the program should display the largest and smallest numbers entered. #include <iostream? using namespace std; int main() {           //enter up to 5 integers         int least, int greatest;        //determine least and greatest double num1 = 0; double num2 = 0; double num3 = 0; double num4 = 0; double num5 = 0;                 cout << " Please enter up to 5 integers:\n ";                 cin >> num1,  num2,  num3, num4, num5;         cout << " The integer with the least value is: \n";         cin >>  least;                 cout << " Of the integers you choose the one with the greatest value is: \n"                 cin >>  greatest; if ( num1 < num2 )         least = num1;         else if ( num2 < num3)                 least = num2;         else if ( num3 < num4)                 least = num3;         else if ( num4 < num5)                 least = num3;         else if ( num 5 < num1 )                         least = num5 if ( num1 > num2 )         greatest = num1;         else if ( num2 > num3)                 greatest = num2;         else if ( num3 > num4)                 greatest = num3;         else if ( num4 > num5)                 greatest = num3;         else if ( num 5 > num1 )                         greatest = num5 if( user input == -99 ) break; return 0; }```
• 09-25-2007
Dave_Sinkula
For starters a loop will need a loop construct such as for or while.

Find the missing semicolon(s) to make this code compile.
• 09-26-2007
QuantumPete
Also, do post the errors that you get!

QuantumPete
• 09-26-2007
matsp
Code:

`if( user input == -99 ) break;`
What's this supposed to be? It's two variables (user, input) that haven't been declared at all.

--
Mats
• 09-26-2007
matsp
And whilst it's not REALLY wrong (it would be worse the other way around), your text says "Enter 5 integers", whilst your variables are of the type double, which means that they are floating point variables - it's a bit confusing.

--
Mats
• 09-26-2007
Salem
> #include <iostream?
Perhaps the first trivial problem?
Replace the ? with a >

Just so you know, just saying "there are errors" isn't good enough sometimes. You need to be specific, by posting actual compile logs for example.
• 09-26-2007
Yarin
The list of 'if else's is really quite a bad way of doing things, you need to loop. And don't do num1, num2, ect, rather do num[5].
• 09-26-2007
dwks
Perhaps you should have a look at some tutorials, such as
• 09-26-2007
Yarin
Here. As you can see, loops make my code alot smaller and more flexible.
Code:

```// By Yarin, public domain. #include <iostream> using namespace std; int main(void) {   int amount;   cout << "How many arguments are you inputing? ";   cin >> amount;   int *num = new int[amount];   int x;   cout << "Enter your arguments: ";   for(x = 0; x < amount; x++)       cin >> num[x];   cout << "Arguments submited..." << endl;     // Now we find the max and min numbers:   int max = 0, min = 0;   for(x = 0; x < amount; x++)   {       if(num[x] > num[max]) max = x;       if(num[x] < num[min]) min = x;   }   cout << "The largest argument submited is " << num[max] << "." << endl;   cout << "The smallest argument submited is " << num[min] << "." << endl;   delete [] num;   cin.get();   return 0; }```
I'm in an okay mood today. So you don't expect handouts from everyone on the board like this.
Welcome to the boards btw.
• 09-26-2007
laserlight
A warning: Yarin's code uses variable length arrays, which is a non-standard feature provided as an extension by some compilers. getch() is also non-standard and probably unnecessary in this case.
• 09-26-2007
dwks
It wouldn't even compile. ;)
Code:

`for(x = 0; x < amount, x++)`
->
Code:

`for(x = 0; x < amount; x++)`
And what's this? . . .
Code:

```  cout << "The largest argument submited is " << num[max] << "." << endl;   cout << "The smallest argument submited is " << num[max] << "." << endl;```
 Also, getch() would likely be in <conio.h>. But cin.get() is a much better idea anyway. [/edit]
• 09-26-2007
Yarin
True. Hey, I didn't compile to check, and those 3 problems are very small. Fixed anyway. :)
• 09-26-2007
laserlight
Quote:

I didn't compile to check, and those 3 problems are very small.
I thought "they are there to test stressedstudent" would be a better comeback :p

Honestly though, fix the use of VLAs (variable length arrays) and remove conio.h. It is a bad habit to inculcate in a stressed student.
• 09-26-2007
Daved
Don't change the VLA's to dynamic array, change it to a vector. There's no good reason to use a dynamic array over a vector here.
• 09-26-2007
Yarin
Good now? What are VLAs?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last