Author Topic: quick question about a program to start  (Read 3831 times)

Offline srlake314

  • Single posting newcomer
  • *
  • Posts: 2
quick question about a program to start
« on: February 04, 2011, 10:39:30 pm »
Hey,
It's been a very long time since i programmed and I have been using codeblocks with "C++ for dummies".  I came across a program and decided, hey, I want to change it to do this:
It's the beginning of functions.  I thought I'd like to take an input from a user and give the result instead of assigning set numbers.  The code in the book is taking double to return a number with power of 3. 

I wanted it to take the user input instead...I am able to get the first response but then it ends the program and displays inf with no chance of entering the second cin...

what am I missing?

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int n;
    int e;

    double number = n;
    double exponent = e;
{

    cout << "Enter base number " <<endl;
    cin >> n;

    cout << "Enter exponent number "<< endl;
    cin >> e;
}

    cout << pow(number, exponent) <<endl;
    return 0;
}


Offline pirx67

  • Multiple posting newcomer
  • *
  • Posts: 40
Re: quick question about a program to start
« Reply #1 on: February 04, 2011, 11:04:05 pm »
The mistake is that you work on random numbers.

Look at your code. You assign number and exponent from n and e before you even have got the input.

Below the assignment was moved after the input. This works.

Code
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int n;
    int e;

    cout << "Enter base number " <<endl;
    cin >> n;

    cout << "Enter exponent number "<< endl;
    cin >> e;
    cout << "Base number is" << n << endl;
    cout << "Exponent is" << e << endl;

    {
double number = n;
double exponent = e;
cout << pow(number, exponent) <<endl;
    }
    return 0;
}

But keep in mind that this is the wrong forum to ask newbie questions about the C++ language. Therefore this question is off-topic and the thread will be locked soon.
Ask these questions in a forum dealing with the C++ language.

Code::Blocks is only an IDE and not the compiler that's responsible for building the program.

Hope that helps,
   pirx67

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: quick question about a program to start
« Reply #2 on: February 04, 2011, 11:05:32 pm »
Note: I am a C programming taking a shoot in the dark; are you skipping the linefeed?

http://www.cplusplus.com/reference/iostream/manipulators/skipws/

Note: This is NOT a valid Code::Blocks topic and it likely to be locked or deleted.
I suggest cboard http://cboard.cprogramming.com/general-programming-boards/

Edit: pirx67 seems to have right idea.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline srlake314

  • Single posting newcomer
  • *
  • Posts: 2
Re: quick question about a program to start
« Reply #3 on: February 04, 2011, 11:07:30 pm »
ty all, was not sure where to post this.  thank you for the replies though.  didnt mean any harm.