Thread: Help using loops...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    48

    Help using loops...

    Hi,

    I'm pretty much new at C++ and we've begun discussing for-loops in class today. We were given a problem for homework:

    Write a program that reads a collection of positive and negative numbers and multiplies only the positive integers. Loop exit should occur when three consecutive negative values are read.

    I'm not sure how to use the sentinel function to end when three consecutive negative values are entered. As far as I can suspect, the code should go something like this?:

    Code:
    .
    .
    .
    for (cin >> number; number != SENTINEL; cin >> number)
    {
    if (number > 0)
    number *= number;
    .
    .
    .
    That's all I can really figure out right now, any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    for loops have the syntax:
    Code:
    for(initialization; loop condition; counter control)
    {
       // your code here
    }
    for example, to print out the first 5 positive integers, one may do:
    Code:
    int i;
    for (i = 0; i < 5; i++)
    {
       cout << i << endl;
    }
    here the initialization part of the for loop declaration is 'i = 0', the loop condition is 'i < 5' and the counter control is 'i++'.

    the first part, initialization, is usually used to set the variable ('counter') to some initial ('starting') value. the loop condition (middle part) means that the loop will only execute if the statement there is true (ie loop while i < 5, here). the counter control simply increments the counter ('i') by one, after each run through the loop, or 'iteration'. note that we left out 'i++', the loop would never end, as i is always zero.

    for your situation, i would declare an int starting at 0, before the for loop. this will be used to count the number of negative numbers. that is, each time you read in a negative number, increment this negative number counter, and if you read in a non-negative number set the counter back to 0. it will be used to control your for loop. ie, you want to loop 'while the last 3 numbers entered are negative.'

    this isnt all of the solution, but i hope it helps.
    Last edited by nadroj; 10-31-2007 at 08:17 PM.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    [note: with some compilers you may have to declare ('int i') your variable before the for loop]
    I thought that was the case only in C?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by cyberfish View Post
    I thought that was the case only in C?
    i think your right cyberfish, which is why i removed it to prevent confusion to the op.
    lately, ive just been using C on solaris, and i know thats the case there.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Declaring variables in for loop initialization sections is standard C++. Only really old, pre-standard C++ compilers might not support it.

    On the other hand, some compilers mess it up. For example, MSVC 6 treats this
    Code:
    for(int x; ; ) {}
    exactly as if you'd written
    Code:
    int x;
    for( ; ; ) {}
    This can cause some problems; for example, this is valid
    Code:
    for(int x; ; ) {}
    std::cout << x;
    but this is not:
    Code:
    for(int x; ; ) {}
    for(int x; ; ) {}
    As for C, in C89 declaring variables in this way is invalid -- but in C99, it's allowed.

    Anyway, with standard C++, it's perfectly acceptable to declare variables in for loop initialization sections. Ignore my ramblings, unless you happen to be using MSVC 6, in which case get a newer compiler.

    Also see this brief lesson: http://www.cprogramming.com/tutorial/lesson3.html
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  4. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM