do and while loops

This is a discussion on do and while loops within the C++ Programming forums, part of the General Programming Boards category; Hi this is my first post, I am having a problem with loops. I am reading through South-Western's Introduction to ...

  1. #1
    Registered User cj_h4x0r's Avatar
    Join Date
    Oct 2006
    Posts
    1

    do and while loops

    Hi this is my first post, I am having a problem with loops. I am reading through South-Western's Introduction to C++ 3rd ed. In chapeter 6, activity 6-2 it tells you to make a program that asks or a series of integers one at a time except for 0. When you enter 0 the program takes the numbers you put in and finds the average, the largest number you put in, the smallest number you put in, and the range of the numbers you put in. I got it to do all of the following, but for some reason it wont print to the screen. I think I just have my statements in the wrong order or something, I'm using Dev-C++ and here is my source:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int i, lar, sma, non;
        float av;
        av=0.0;
        non=0;
        sma=2147483647;
        lar=-2147483648;
        do
        {
              cin >> i;
              cout << "You Entered " << i << endl;
              av=av+i;
              non+=1;
              
              if(i>lar)
              {lar=i;}k
              
              if(i<sma)
              {sma=i;}
              
              if(i=0)
              {cout << "Average = " << av/non << endl;
              cout << "Largest = " << lar << endl;
              cout << "Smallest = " << sma << endl;
              cout << "Range = " << lar-sma << endl;
              break;}
        }
        while(1);
    
    return 0;
    }
    Can anyone help me out? I've been messing around with it for an hour and decided to ask for help.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    after the first if statement you have a 'k' all by itself, must be a typo, get rid of it. it shouldnt compile with this in it anyway.

    your third if statement (if (i=0)) is the solution to your problem. what is the '=' (assignment) operator for? what should you be using instead? if you dont know search for c++ operators..

    also, besides having this last if statement, you should change your while(1) to loop while 'i' does NOT equal '0' (zero) then move the 4 couts and put them after the while statement, and get rid of the break.... this is why do loops have a while clause, for the purpose you are trying to achieve, that is: "do something while [condition]". in your case, 'do something while i does not equal zero'.

    if you dont understand try and edit as much of your code as i have said and post it.. or if you get other errors post the updated code and error message.

    good luck.
    Last edited by nadroj; 10-22-2006 at 05:50 PM.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if anyone else is watching, how come sma and lar can store the values he has assigned to it? is the default 'int' = 'long int'?

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Quote Originally Posted by nadroj
    if anyone else is watching, how come sma and lar can store the values he has assigned to it? is the default 'int' = 'long int'?
    An int is a 32 bit value, so those numbers are perfectly valid and are the min/max signed values of a 32 bit integer.

    Also, to cj_h4x0r:
    Code:
    if(i=0)
    As stated by nadroj, this line will cause the problem. Think of the code this way:

    Code:
    int i = 10;
    i = 0;
    cout << i << endl;
    What is i at the end of this program? You should be saying 0.

    Applying this same logic to the if statement, you are assigning i to become 0, so you then are testing if (0) which will always be false. This is a very common error when beginning programming, so don't fret. Merely switch the = (assignment) operator to the == (comparison) operator and you will be fine.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i thought the max for int was (-/+)32XXX? ya i did sizeof on both int and long and they are both 4 bytes.. why can long int store such a greater value while occupying the same amount of memory.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Uh ...

    All types are of course largely platform-dependent, but on the x86 sizes are like thus:
    char: 8 bits. Unsigned 0 - 255, signed -128 - 127.
    short: 16 bits. Unsigned 0 - 65xxx (2^16), signed -32xxx - 32xxx.
    int == long: 32 bits. Unsigned 0 - 4 billion something, signed -2 billion - 2 billion.

    The values of sma and lar happen to be INT_MIN and INT_MAX of a 32-bit signed integer.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if int and long can store the same range whats the difference?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    They do not necessarily have the same range, but it happens to be the case here.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    due to what? i know it works on my system too (VS.net 8).

    ive read afew sites that say the ranges as i had said.... but just a second ago i say one that says its for 'portability'. ie, an 'int' can store (no matter what OS or CPU) -/+32k, while long int can no matter what store -/+(whatever the number)

  10. #10
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    in most cases long int is the same thing as int. The same range and everything.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > ie, an 'int' can store (no matter what OS or CPU) -/+32k
    The standard expresses such things as minimum requirements, not absolute requirements.

    That is, and int needs to be able to store at least +/-32K, not that it has to be limited to only +/-32K.

    However, where the standard allows the freedom to choose, most implementations go with the default machine 'word' for storing ints where this exceeds the minimum standard requirements.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    An int is only guaranteed to hold from -16383 to +16383.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > An int is only guaranteed to hold from -16383 to +16383.
    Read Dave's links here - C Draft Standards
    Annex E of the first one is C99 TC2, which states
    #define INT_MAX +32767
    #define INT_MIN -32767

    The C89/C90 versions say the same.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by nadroj
    due to what?
    Due to your compiler manufacturer's choices. If you don't like it, write your own

    On my Linux AMD64 box, long is 64 bits while int is still 32. But only under GCC. If I were to install a 64-bit Windows and run a 64-bit VC++ there, its compiler would still use 32 bits for long.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

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