For Statement Question

This is a discussion on For Statement Question within the C++ Programming forums, part of the General Programming Boards category; Hello, I am working on a program that asks the user to input an integer value. It then raises X ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    21

    Question For Statement Question

    Hello,
    I am working on a program that asks the user to input an integer value. It then raises X to all the powers from 0 up to 5, using the user entered value.

    For example, if the user entered 5 the output would be:

    2 ^ 0 = 1
    2 ^ 1 = 2
    2 ^ 2 = 4
    2 ^ 3 = 8
    2 ^ 4 = 16
    2 ^ 5 = 32

    I have done this so far (Im pretty new to this stuff, so please dont completely blow me off):
    Code:
    #include <iostream>
    
    int main()
    {
            int i;
    
            cin >> i;
            for (i=i; i>=0; i*i)
            {
            cout << i;
            cout << i << " ^ 0 = " << i ^ 0 << endl;
            cout << i << " ^ 1 = " << i ^ 1 << endl;
            cout << i << " ^ 2 = " << i ^ 2 << endl;
            cout << i << " ^ 3 = " << i ^ 3 << endl;
            cout << i << " ^ 4 = " << i ^ 4 << endl;
            cout << i << " ^ 5 = " << i ^ 5 << endl;
            }
    
            cout << endl;
            return 0;
    }
    Here is a list of errors that I receive, I was wondering if anyone would be able to help me out here, what show I try changing?
    I will continue working on it, but input would be appreciated greatly!

    Errors:
    Code:
    powersof2.cpp: In function `int main()':
    powersof2.cpp:11: invalid operands `int' and `ostream & ()(ostream &)' to binary `operator <<'
    powersof2.cpp:12: invalid operands `int' and `ostream & ()(ostream &)' to binary `operator <<'
    powersof2.cpp:13: invalid operands `int' and `ostream & ()(ostream &)' to binary `operator <<'
    powersof2.cpp:14: invalid operands `int' and `ostream & ()(ostream &)' to binary `operator <<'
    powersof2.cpp:15: invalid operands `int' and `ostream & ()(ostream &)' to binary `operator <<'
    powersof2.cpp:16: invalid operands `int' and `ostream & ()(ostream &)' to binary `operator <<'
    powersof2.cpp:17: warning: statement with no effect
    I will try stuff, but It would be great if you could give me some feedback.
    Thank you!

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    The ^ operator doesn't mean "raised to the power of", it means XOR.
    You either need to add using namespace std; or use std::cout & std::endl
    Also, I'm not sure what you're trying to do in that for loop?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    If you want to do:
    x^5 (power)

    Code:
    #include <math.h>
    #include <iostream>
    using namespace std;
    
    int main(){
      float x;
      cin >> x;
      for(int i = 0; i < 5; i++){
        cout << x << " ^ " << i << " = " << pow(x,(float)i) << endl;
      }
    
    }
    Is this how you wanted it?

    This will output:

    if user enters 3:
    3 ^ 0 = 1
    3 ^ 1 = 3
    3 ^ 2 = 9
    etc etc....

    Don't use the direct ^ operator, that's XOR bitwise operator.
    pow() is the function for power.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    Quote Originally Posted by execute View Post
    Code:
    #include <cmath>
    #include <iostream>
    using namespace std;
    
    int main(){
      float x;
      cin >> x;
      for(int i = 0; i < 5; i++){
        cout << x << " ^ " << i << " = " << std::pow(x,(float)i) << endl;
      }
    
    }
    Corrections in red.
    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.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Corrections in red.
    Since it has using namespace std; the second correction is optional.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    Quote Originally Posted by cpjust View Post
    Since it has using namespace std; the second correction is optional.
    True, but I wanted to forward the message that the function is actually located inside the std namespace.
    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.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    21

    Smile Thanks!

    Wow, Thank you all so much for the excellent help.
    I am very grateful that there are people out there to help me with this, rather than just give up on it.
    I used what you all said, and it works.
    Here is the final product:

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
         int i;
         float x;
         i = 0;
         cout << "Enter Number: " << endl;
         cin >> x;
         for (int i = 0; i < 6; i++)
    {
    cout << x << " ^ " << i << " = " << std::pow(x,(float)i) << endl;
    }
    
            return 0;
    }
    Once again, thank you all so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

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