Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,271 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,444 people online right now. Registration is fast and FREE... Join Now!




For Loop -> While Loop

 
Reply to this topicStart new topic

For Loop -> While Loop, conversion

aznballerlee
16 Oct, 2006 - 05:58 PM
Post #1

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
Once again another question.

I want to change the inner loop from "for loop" to "while loop".
I cannot figure out my errors.

error C2065: 'j' : undeclared identifier
error C1903: unable to recover from previous error(s); stopping compilation


So I am trying to convert this "for loop":

CODE

#include <iostream>
    using namespace std;

    int main()
    {
        int len;

        cout << "Enter a number: ";
        cin >> len;

        for (int i = 0; i < len; i++)
        {
            for (int j = i+1; j < len; j++)
        {
            cout << ' ';
        }
        cout << "#\n";
        }
    }


into this (or what i have so far):

CODE

#include <iostream>
    using namespace std;

    int main()
    {
        int len;

        cout << "Enter a number: ";
        cin >> len;

        for (int i = 0; i < len; i++)
        {
        while (j < len)
        {
          int j = i+1;
          j++;
           cout << ' ';
        
        }
        cout << "#\n";
        }
    }


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: For Loop -> While Loop
16 Oct, 2006 - 06:02 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
As per the error message, you will note that in the original code, you have actually declared and instantiated the variable j
CODE

int j = i+1

in the second piece of code, you are trying to use j before you declare it...it does not exist before you try and use it, so the compiler throws an error.
User is online!Profile CardPM
+Quote Post

aznballerlee
RE: For Loop -> While Loop
16 Oct, 2006 - 06:05 PM
Post #3

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
Right, that's what I thought. Where would be the appropriate place to declare the j?
User is offlineProfile CardPM
+Quote Post

aznballerlee
RE: For Loop -> While Loop
16 Oct, 2006 - 06:08 PM
Post #4

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
I tried this code:

CODE

#include <iostream>
    using namespace std;

    int main()
    {
        int len;

        cout << "Enter a number: ";
        cin >> len;

        int j;
        for (int i = 0; i < len; i++)
        {
        while (j < len)
        {
          int j = i+1;
          j++;
           cout << ' ';
        
        }
        cout << "#\n";
        }
    }


which runs but it doesn't produce the same results as the original (with the for loops)
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: For Loop -> While Loop
16 Oct, 2006 - 06:08 PM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Before you use it. Declare j as an int at the top of your program, where you declare len, then set j to the value of i+1 right before you begin the while loop.
User is online!Profile CardPM
+Quote Post

aznballerlee
RE: For Loop -> While Loop
16 Oct, 2006 - 06:10 PM
Post #6

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
Great! Thanks Amadeus for all your help! smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:58PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month