While loop tutorial needs help

This is a discussion on While loop tutorial needs help within the C++ Programming forums, part of the General Programming Boards category; #include <iostream> using namespace std; int main() { int n1, n2; // declare two variables cout<< "Enter two numbers: "; ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    65

    While loop tutorial needs help

    #include <iostream>
    using namespace std;

    int main()
    {
    int n1, n2; // declare two variables

    cout<< "Enter two numbers: ";
    cin>> n1;
    cin>> n2;
    i = n1; // Initialize i to n1
    while( i <= n2 ){ // While i is equal or less than n2
    cout<< n1 << " "; // Print out i
    i = i + n2; // add i to n2
    }

    return 0;
    }


    I am just wondering what is wrong with this code. It just keeps saying that the variable i is not declared.

    Here is the tutorial from the book I bought, which is recommended by this website: C++ without fear. This is the exercise.

    -Write a program to print all the numbers from n1 to n2, where n1 and n2 are two numbers specified by the user.
    Last edited by kenryuakuma; 09-18-2008 at 12:45 AM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You need to declare it then.
    Code:
    int i;
    or
    Code:
    int n1, n2, i;

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    All variables you use must be defined. You haven't defined the variable i, so the compiler complains and rightly so.
    Good luck with your exercises.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Is there any alternative to write it? According to the book, some of them, if used correctly, you do not have to declare it. How? Or do we always have to declare it with no other choice?

    Besides, is it ok to have the end-user input by writing cin>> n1, n2? instead of having write cin twice( cin>> n1, and cin>> n2)?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suspect you misread something in your book - or the book is TERRIBLY BAD (and incorrect). ALL variables MUST be declared, ALWAYS. There are different places in the code where these may be declared, but if they are not declared, the compiler will say something like "undefined symbol X" where X is the name of your variable [the exact wording depends on the compiler, but the essence is that the compiler will say "I don't know what X is, and you need to tell me"].

    The comment and actual code is mismatched here:
    Code:
    cout<< n1 << " "; // Print out i
    I have no idea which one is correct (because , but misleading comments are a BAD thing. No comment is better than one that is misleading).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Is there any alternative to write it? According to the book, some of them, if used correctly, you do not have to declare it. How? Or do we always have to declare it with no other choice?
    Either the book is wrong, or you are misinterpreting the book. What example does your book give to support this claim?

    Besides, is it ok to have the end-user input by writing cin>> n1, n2? instead of having write cin twice( cin>> n1, and cin>> n2)?
    No, they mean different things. If I remember by operator precedence correctly, that expression will be evaluated as (cin >> n1), n2. This means that it will read into n1, and then just evaluate n2 (without reading anything into it).
    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

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    No, they mean different things. If I remember by operator precedence correctly, that expression will be evaluated as (cin >> n1), n2. This means that it will read into n1, and then just evaluate n2 (without reading anything into it).
    You can however write cin >> n1 >> n2, which will read two numbers in one line of code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Quote Originally Posted by kenryuakuma View Post
    Is there any alternative to write it?
    None.

    Quote Originally Posted by kenryuakuma View Post
    According to the book, some of them, if used correctly, you do not have to declare it.
    If the book says that, then it's a bad book because it's wrong.
    i has to be defined somewhere before.

    Quote Originally Posted by kenryuakuma View Post
    How? Or do we always have to declare it with no other choice?
    That is correct. It must be defined first.
    However, when first defining it, you have one instance of it, which can be reused, so you don't necessarily need to define it before all loops, just making sure to define it before using it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    It is crystal-clear by asking you guys. Thanks so much for helping you guys. But this thing sometimes requires you to think a lot before you could solve the problems.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kenryuakuma View Post
    It is crystal-clear by asking you guys. Thanks so much for helping you guys. But this thing sometimes requires you to think a lot before you could solve the problems.
    Yup, thinking is involved in computer programming.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by kenryuakuma View Post
    Is there any alternative to write it? According to the book, some of them, if used correctly, you do not have to declare it. How? Or do we always have to declare it with no other choice?
    The alternative is to use the #include directive to pull in declarations from another file. This way, you don't need to explicitly declare them, but since the file contains declarations, you are still technically declaring them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM

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