Thread: What is wrong?

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    7

    What is wrong?

    I need to fix each of these codes so that it will print numbers between 1 and 10. what is wrong?
    Code:
    #include <iostream>
    using namespace std;
    int main (void){
    for (int n=1; n<=10; n++);
    cout << n << endl;
    return 0;
    }
    and

    Code:
    #include <iostream>
    using namespace std;
    int main (void)
    { n=1;
    while (n<11)
    cout << n << endl;
    n++;
    return 0;
    }
    any help would be appreciated, thanks
    Last edited by girliegti; 09-18-2003 at 09:22 AM.

  2. #2
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Code:
    #include <iostream>
    using namespace std;
    int main ()     // void not necessary, but you can still put it there
    {
     for (int n = 2 ; n<10  ; n++) // no semi colon at end of for
         cout << n << endl;
    
    return 0;
    }
    Leave the other for you to do. If you meant you wanted to print between 0 and 11, then you should be able to change that easily.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    Thanks for the help. Could you possibly explain why making the change is necessary? I'm new to programming, and am having some difficulty understanding what everything means.
    thanks
    Last edited by girliegti; 09-18-2003 at 12:09 PM.

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    well the symmantics of the for loop are as follows:
    Code:
    for ( int i = 0; i < 9; i++ ){
          cout << i << endl;
    }
    the int i = 0 is setting the starting value for i to zero.
    the i < 9 sets the upper limit of the counter, ie do whatever is in the braces until i is less than 9,
    the i++ increments the i by 1 each time the loop is run

    consequently the output for i will be
    0
    1
    2
    3
    and so on till
    8

    hope this helps...you could also look into your book, or the cprogramming.com tutorials,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    well if this is the case, why are the two codes that i gave wrong?
    Code:
    #include <iostream>
    using namespace std;
    int main (void){
    for (int n=1; n<=10; n++);
    cout << n << endl;
    return 0;
    }

    Code:
    #include <iostream>
    using namespace std;
    int main (void)
    { n=1;
    while (n<11)
    cout << n << endl;
    n++;
    return 0;
    }
    why wouldn't these output the integers 1 through 10?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by girliegti
    well if this is the case, why are the two codes that i gave wrong?
    Code:
    #include <iostream>
    using namespace std;
    int main (void){
    for (int n=1; n<=10; n++);
    cout << n << endl;
    return 0;
    }

    Code:
    #include <iostream>
    using namespace std;
    int main (void)
    { n=1;
    while (n<11)
    cout << n << endl;
    n++;
    return 0;
    }
    why wouldn't these output the integers 1 through 10?
    The first is wrong because you have a semi-colon on the end of the for() line.

    The second is wrong becuase the n++; line is not within the loop. As you have 2 lines of code that you want to run inside the loop, you must encase them with {}.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    Ok, thank you. That makes a lot more sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM