Thread: I need to make an infinity "loop" without using "loop" until user input 0

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    13

    I need to make an infinity "loop" without using "loop" until user input 0

    Hello guys the title states the problem but here's my code, the problem I am facing right now is how to add the value of number[0] to number[1].

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int i=0;
    int number[1000];
    int equal;
    int main(){
    cout<<"\nEnter number: ";
    cin>>number[i];
    if (number[i] == 0)
    {
    getch();
    cout<<"You have entered 0, Goodbye!";
    return 0;
    }
    equal = number[i] + number[i];
    cout<<"\n"<<equal;
    i++;
    
    getch();
    main();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What's wrong with a loop?

    Is this one of those pointless "Do X without using the obvious thing" problems some tutors seem to think are worthwhile?

    1. You can't call main recursively.

    2. Using recursion is a loop in disguise, so is it even allowed?

    3. Using recursion for a loop is awful, as it typically consumes stack space (a finite resource). With enough iterations, the program will fail.

    4. I suppose you could use a goto statement, but at least it isn't a loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    >> Is this one of those pointless "Do X without using the obvious thing" problems some tutors seem to think are worthwhile?

    Looking at the headers they're being taught to use, I wouldn't be surprised.

    Quote Originally Posted by abujuguluy View Post
    ... the problem I am facing right now is how to add the value of number[0] to number[1].
    If you were allowed to use a loop (which is the most natural way to approach this), then you could just keep a running total - add each input to the previous sum. This would work with Salem's suggestion #4.

    Then I would suggest you find a new teacher, or learn proper programming techniques on your own with a good book. There are plenty of good brain teasers that do not actively discourage good programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  2. nbin=fopen("input.txt","a"); doesn't work?
    By Adam Rinkleff in forum C Programming
    Replies: 2
    Last Post: 06-23-2011, 02:57 PM
  3. Need "if","for loop",&"else" source codes
    By dn_angel_07 in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2009, 10:01 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread