Do I need a LOOP?

This is a discussion on Do I need a LOOP? within the C++ Programming forums, part of the General Programming Boards category; I've written this very simple program that Will raise a series of intreger values the nth power. And it has ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    9

    Smile Do I need a LOOP?

    I've written this very simple program that Will raise a series of intreger values the nth power. And it has to have the ouput as I have written it"2 Raised to the Power=8. This will run believe I did but!! it didn't see =8. And now I have to decide what loop or what ever makes since to RUN A SERIES. I would appreciate some help. And thank you this board is SO much help I do have the tutorial printed out in front of me. I completed three other programs today that I have figured out and ran fine. So thank you again for any suggestions.

    #include<iostream.h>
    #include<math.h>
    void main(void)

    {
    int number=0,
    x=0,
    power=0,
    ans=0;

    cout<<"Enter Number ";
    cin>>number;
    cout<<"Enter Power ";
    cin>>power;

    x=pow(number,power);

    cout<<number<<" Raised to the the Power "<<endl;

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    cout<<number<<" Raised to the the Power "<<endl;
    try cout << number << " raised to the power " << power << endl;
    As for a loop, what kind do you want? One that runs till the user ends it? one that loops a certain number of times?

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    9

    I need

    I guess one that the person can stop either would be fine. Thank you for answering me. I can say I have a new friend, Brown drake) Am getting this programming. Thank you sooo much.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    21
    while (counter < 10)//test conditions for loop
    {
    cout << counter;//code executed during loop
    counter++;
    }

    perhaps you could invite the user to enter the value for counter, or whatever you decide to call your variable.

    Hope this helps.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    #include<iostream.h>
    #include<math.h>
    int main()

    {
    int number=0,
    x=0,
    power=0,
    ans=0;

    char flag = 'y';

    //a loop that allows user to exit at any time after entering the
    //two initial values.
    while(flag == 'y')
    {
    cout<<"Enter Number ";
    cin>>number;
    cout<<"Enter Power ";
    cin>>power;

    x=pow(number,power);

    cout<<number<<" Raised to the the Power "<< power << " = " << x << endl;

    cout << "enter y if you want to try it again and any other key to stop." << endl;
    cin >> flag;
    }

    return 0;
    }

    //demonstrates the use of a user controlled loop.
    Last edited by guest; 12-03-2001 at 11:23 AM.

  6. #6
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    What book are you doing this from?

    Because I was using Sams Teach Yourself C++ in 24 hours and I was doing something similar, are you sure you don't have to use recursions?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  7. #7
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    This might help

    #include <windows.h>

    using namespace std;
    typedef unsigned short USHORT;
    typedef unsigned long ULONG;
    ULONG GetPower (USHORT number, USHORT power);
    int main()
    {

    USHORT number, power;
    double answer;
    std::cout<<"Enter a number:";
    cin>>number;
    cout<<"To what power?";
    cin>>power;
    answer=GetPower(number, power);
    cout<<number<<"to the"<<power<<"th power is"<< answer<<endl;

    return 0;
    }
    ULONG GetPower (USHORT number, USHORT power)

    {
    if (power==1)
    return number;
    else
    return (number*GetPower (number, power -1 ));
    }
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    I haven'tcompiled and ran the program, but it should work fine without recursion. Liberty is a fine author in my opinion. Read on!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

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