# Can't spot the problem...am i even on the right track?

• 02-05-2002
iluvmyafboys
Can't spot the problem...am i even on the right track?
this is what i have so far. i wrote the notes in the beginning so that any one who sees this knows what i'm looking for. i'm not sure if i have to display each check#, or if there's a way that i can loop it in the WHILE or if i do have to display each individual field so that i can find the largest # out of the 10....any help or suggestions are GREATLY appreciated!

// Ex. 2.20: ex220.cpp
/* Write a program that uses a counter to count to 10, display the current input, and show the largest number so far. */

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main ()
{

int check1, check2, check3, check4, check5;
int check6, check7, check8, check9, check10;
int itemCounter, largest;

// initialization phase
total = 0; // clear total
itemCounter = 1; // prepare to loop

// processing phase
while (itemCounter <= 10) {
cout << "Enter check amount: ";
cin >> check1;
cin >> check2;
cin >> check3;
cin >> check4;
cin >> check5;
cin >> check6;
cin >> check7;
cin >> check8;
cin >> check9;
cin >> check10;
itemCounter = itemCounter + 1;
}
• 02-05-2002
OneStiffRod
You can tell that that is MESSY Newbie code:

Firstly, use arrays to hold your check values, arrays are indexed which means they are numbered incrementily so it is easier to program a LOOP with them.

Secondly, use a FOR loop to do this as it fits better for what you want to accomplish.

MAIN:

float checks[10] ={0};
int itmCounter = 1;
float lgAmout = 0;

for (int i = 0; i < 10; i++){
cout<<"Enter amount for check "<<itmCounter<<endl;
cin>>checks[i];
if(lgAmount < checks[i]){
lgAmount = checks[i];
}
cout<<endl<<"The Largest amount so far is: "<<lgAmount<<endl;
itmCounter = i+1;

}

Try this, it has significantly reduced the size of your code.
• 02-05-2002
Stoned_Coder
by applying a little structure we can make this look a whole lot better....
Code:

```#include <iostream> using namespace std; int main () { int check[10]={0} ; // heres your 10 ints cout<<"Enter 10 numbers :-"; for (int i=0;i<10;++i) // heres your counter { cin>>check[i]; // get 10 inputs } int total=0; for(int i=0;i<10;i++) { total += check[i]; } cout << endl<<"The total is :-"<<total<<endl; int largest=check[0]; // lets say the largest is the first value for(int i=1;i<10;i++) { if (largest<check[i]) largest=check[i]; } cout<<"Largest is :-"<<largest<<endl; return 0; }```
• 02-05-2002
Stoned_Coder
now your exercise is to get that down to about 8 lines!
• 02-05-2002
iluvmyafboys
thanks but...
thanks for the advice, but i'm not familiar with arrays...any other suggestions?
• 02-05-2002
OneStiffRod
Since you don't know how to use arrays goto this tutorial on THIS site:

http://www.cprogramming.com/tutorial/lesson8.html

Arrays are easy, they are basically equivalent to what you were doing in the first place.

int Checks[10]; //This is

EQUAL TO:

int Checks1, Checks2, Checks3, Checks4... and so on to 10;

using an array is just an easier way of declaring a bunch of the same type variables with similar names.

*just remeber to start counting at 0 which is customary in C++ so when declaring an array like so:

int Checks[10]; //You have really declared an array of variables from 0 - 9

Checks[0], Checks[1], Checks[3] ... and so on to Checks[9]

There's 10 places but we start counting at 0.
• 02-05-2002
Arrays arent that simple, and makes the code a hecka of lot easier. You can have all 10 checks in

check[10];

So if you want to show 1 number, you can do this:

cout<<check[0]<<'\n';

That will show whats in check1. Uhhhh....that should help
• 02-05-2002