I've got the initial part but....

This is a discussion on I've got the initial part but.... within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; #include <conio.h> int main() { int n; int x=1; cout << "Please ask for ...

  1. #1
    MB1
    MB1 is offline
    Registered User
    Join Date
    Feb 2005
    Posts
    38

    I've got the initial part but....

    Code:
    #include <iostream>
    using namespace std;
    #include <conio.h>
    
    int main()
    {
    	int n;
    	int x=1;
    
    	cout << "Please ask for nonnegative integer: ";
    	cin >> n;
    	cout << endl;
    	while ((n > 0) && (x != (n + 1)))
    	     {cout << x << " ";
    	          x++;}
                    if (x = n)
                          n--;
                    else
                         { cout << x << " ";}
                   cout << endl;
         getch();
         return 0;
    }
    I'm supposed to write code that will accept a positive integer 'n' and display (ie if integer is 7):
    1 2 3 4 5 6 7
    1 2 3 4 5 6
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1

    I get the first line to print out but am a loss to get the rest of it to print.

    Suggestions please!!!
    mb
    Last edited by MB1; 03-02-2005 at 09:56 AM. Reason: Accidental send

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You'll need a double loop, the inner one increases x and the outer one decreases n.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    You need to use nested loops, i.e. a loop within a loop. The outer loop will control how many lines you write out (7) and the inner loop will print the values 1-X.

    [edit]Drats, beaten...[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    MB1
    MB1 is offline
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Thanks for the help!

    I finally have it. Though it may not be very elegant, it still works. Suggestions welcome for cleaning it up.



    Code:
    #include <iostream>
    using namespace std;
    #include <conio.h>
    
    int main()
    {
    	int n;
    	int x = 1;
    	cout << "Please enter a nonnegative number: ";
    	cin >> n;
    	while (n > 0)
    		{{x = 1;
    		n--;
    		cout << endl;}
    			while (x != (n + 2))
    			{cout << x << " ";
    			x++;}}
    	cout << endl;
    	getch();
    	return 0;
    }

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Code:
    #include <iostream>
    using namespace std;
    #include <conio.h>
    
    int main()
    {
        int n;
        int x;
    
        cout << "Please enter a nonnegative number: ";
        cin >> n;
    
        while (n > 0)
        {
            x = 1;
            while ( x <= n )
                cout << x++ << " ";
            cout << endl;         
            --n;
        }
    
        cout << endl;
        getch();
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to get domain part from URL
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 07-23-2008, 12:06 PM
  2. How to get a part of a string from a point onwards
    By pseudonoma in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 04:09 PM
  3. Replies: 9
    Last Post: 07-11-2006, 04:28 AM
  4. Can someone look over my code? part 2
    By brooklyn in forum C++ Programming
    Replies: 10
    Last Post: 04-18-2006, 06:33 AM
  5. Suspicious Pointer Conversion
    By mr_spanky202 in forum C Programming
    Replies: 35
    Last Post: 04-11-2003, 12:35 PM

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