Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Simple C++ Help (http://www.programmingforums.org/showthread.php?t=14640)

Yutxz Nov 28th, 2007 9:54 PM

Simple C++ Help
 
I just recently purchased a book by the title of "C++ Primer", to help me pick up the language. After going through forty-five pages of text, I came across an exercise that states "Write a program that prompts the user to input two numbers, the base and exponent. Print the result of raising the base to the power of the exponent". I attempted to try and solve this problem, but I believe my script may contain errors. I also believe that I went about the code the wrong way (writing unnecessary script and such). Unfortunately, the book does not provide an answer key and I can't install a compiler on this computer.

The book proves difficult to understand, and it would mean a great deal to me if you guys could help me out.

Thanks,
Josh
:

  1.  
  2. // Started November 28th
  3. // Last edited November 28th
  4. #include <iostream>
  5. int main ()
  6. {
  7.         std::cout << "Enter a base:";
  8.         int v1;
  9.         std::cin >> v1;
  10.         std::cout << "Now enter an exponent:";
  11.                 int v2;
  12.         std::cin >> v2;
  13.         int value(v1);
  14.         int pow(v2);
  15.         int result(1);
  16.         // repeat calculation of result until cnt is equal to pow
  17.         for (int cnt = 0; cnt != pow; ++cnt)
  18.                 result *= value; // result * value = result
  19.         std:: cout << value << " Raised to the power of " << pow << ": /t" << result;
  20.         return 0;
  21. }


WaltP Nov 28th, 2007 10:47 PM

Re: Simple C++ Help
 
Quote:

Originally Posted by Yutxz (Post 137779)
I attempted to try and solve this problem, but I believe my script may contain errors.

May? What makes you think so? What type of errors? Compile errors? Do you think it will crash? It's logically incorrect?

Quote:

Originally Posted by Yutxz (Post 137779)
I also believe that I went about the code the wrong way (writing unnecessary script and such). Unfortunately, the book does not provide an answer key and I can't install a compiler on this computer.

Again, why? And it's not a script, it a program :icon_wink:

And unfortunately, if you can't install a compiler, it's going to be very difficult to learn to program. Programming is not a spectator sport.

You can get Borland 5.5 and put it on USB Drive and carry the compiler with you. If you'd like to try that, let me know. I'll explain my setup after you have it installed on a Drive.

peaceofpi Nov 29th, 2007 2:51 PM

Re: Simple C++ Help
 
Quote:

Originally Posted by Yutxz (Post 137779)
:

  1.  
  2.         int value(v1);
  3.         int pow(v2);
  4.         int result(1);


What is this supposed to be? You're making things too confusing with that many variables, just use the ones you created in the first place (minus result). Your code compiles fine but making sense of it is another thing. Also, your /t should be \t.

Yutxz Dec 2nd, 2007 1:08 PM

Re: Simple C++ Help
 
Hey, thanks for all the help. I recently updated my program following your guy's advice. I also downloaded a compiler and it seems to build fine.

Thanks Again.

:

  1. // Started November 28th
  2. // Last edited December 2nd
  3. #include <iostream>
  4. int main ()
  5. {
  6. int base;
  7. int exp;
  8. int result(1);
  9. std::cout << "Enter a base:";
  10. std::cin >> base;
  11. std::cout << "Now enter an exponent:";
  12. std::cin >> exp;
  13. // repeat calculation of result until cnt is equal to pow
  14. for (int cnt = 0; cnt != exp; ++cnt)
  15.   result *= base; // exp * base = result
  16. std:: cout << base << " Raised to the power of " << exp << ": \t" << result;
  17. return 0;
  18. }


peaceofpi Dec 2nd, 2007 1:33 PM

Re: Simple C++ Help
 
You should look into getting an IDE, or at least an advanced text editor. Indentation means nothing to the compiler but a lot to you. Also, when your programs start to get bigger, I'd recommend the using keyword, so you don't have to qualify each std member.

:

  1. // Started November 28th
  2. // Last edited December 2nd
  3. #include <iostream>
  4. using std::cout;
  5. using std::cin;
  6. int main ()
  7. {
  8.     int base;
  9.     int exp;
  10.     int result(1);
  11.     cout << "Enter a base:";
  12.     cin >> base;
  13.     cout << "Now enter an exponent:";
  14.     cin >> exp;
  15.     // repeat calculation of result until cnt is equal to pow
  16.     for (int cnt = 0; cnt != exp; ++cnt)
  17.         result *= base; // exp * base = result
  18.     cout << base << " Raised to the power of " << exp << ": \t" << result;
  19.     return 0;
  20. }


Yutxz Dec 2nd, 2007 1:51 PM

Re: Simple C++ Help
 
I'm currently using the Visual Studios 2008 compiler, does it come equipped with an IDE? If not, how do I acquire an IDE? And thanks for recommending the using keyword. It seems to build my program much faster.

peaceofpi Dec 2nd, 2007 2:32 PM

Re: Simple C++ Help
 
http://ecuador.latindevelopers.net/p...2/640x476.aspx
This is something like what your IDE should be. Check your start menu, or if you really can't find it see if double clicking a CPP file will open it.

Jimbo Dec 2nd, 2007 2:35 PM

Re: Simple C++ Help
 
Visual Studio 2008 is an IDE, and it contains various compilers for the different languages supported (e.g. the CLR). A compiler is just the program that takes your text file and outputs an executable. And IDE is a nice spiffy text editor that automatically invokes the compiler to compile your code when you want to. Some people prefer to use a text editor and invoke the compiler separately from the editing experience. It's a choice, and each side has some benefits.


All times are GMT -5. The time now is 3:16 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC