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

This is a discussion on Can't spot the problem...am i even on the right track? within the C++ Programming forums, part of the General Programming Boards category; this is what i have so far. i wrote the notes in the beginning so that any one who sees ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    19

    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;
    }
    Last edited by iluvmyafboys; 02-06-2002 at 06:59 AM.

  2. #2
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    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.
    Last edited by OneStiffRod; 02-05-2002 at 08:23 PM.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    now your exercise is to get that down to about 8 lines!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    19

    thanks but...

    thanks for the advice, but i'm not familiar with arrays...any other suggestions?

  6. #6
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    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.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  7. #7
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    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
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  8. #8
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    Arrays arent that simple
    Sorry, i meant that arrays arent that difficult
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  9. #9
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    Bravo Stoned_Coder. your idea worked without any debuging and you didnt add any insults either.

    BTW - the New Mexico state legislature tabled the legalization of medical pot. maybe next year.

    M.R.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21