Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 28th, 2007, 9:54 PM   #1
Yutxz
Newbah
 
Yutxz's Avatar
 
Join Date: Nov 2007
Location: RI
Posts: 3
Rep Power: 0 Yutxz is on a distinguished road
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
c++ Syntax (Toggle Plain Text)
  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. }
Yutxz is offline   Reply With Quote
Old Nov 28th, 2007, 10:47 PM   #2
WaltP
Programmer
 
Join Date: Oct 2007
Posts: 39
Rep Power: 0 WaltP is on a distinguished road
Re: Simple C++ Help

Quote:
Originally Posted by Yutxz View Post
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 View Post
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

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.
WaltP is offline   Reply With Quote
Old Nov 29th, 2007, 2:51 PM   #3
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 115
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Simple C++ Help

Quote:
Originally Posted by Yutxz View Post
c++ Syntax (Toggle Plain Text)
  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.
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Dec 2nd, 2007, 1:08 PM   #4
Yutxz
Newbah
 
Yutxz's Avatar
 
Join Date: Nov 2007
Location: RI
Posts: 3
Rep Power: 0 Yutxz is on a distinguished road
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.

c++ Syntax (Toggle Plain Text)
  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. }
Yutxz is offline   Reply With Quote
Old Dec 2nd, 2007, 1:33 PM   #5
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 115
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
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.

c++ Syntax (Toggle Plain Text)
  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. }
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Dec 2nd, 2007, 1:51 PM   #6
Yutxz
Newbah
 
Yutxz's Avatar
 
Join Date: Nov 2007
Location: RI
Posts: 3
Rep Power: 0 Yutxz is on a distinguished road
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.
Yutxz is offline   Reply With Quote
Old Dec 2nd, 2007, 2:32 PM   #7
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 115
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Simple C++ Help


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.
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Dec 2nd, 2007, 2:35 PM   #8
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
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.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Tic Tac Toe Game smita Existing Project Development 0 Mar 15th, 2007 8:57 AM
PLS Help me in a very simple (noob) program about textfields and textareas. javaN00b Java 3 Mar 29th, 2006 10:45 PM
Simple input Oddball Java 6 Mar 12th, 2006 7:30 PM
a simple linking loader for SIC/XE programmingnoob C++ 3 Feb 27th, 2006 12:35 AM
Simple Function Questions meverha1 C++ 16 Sep 12th, 2005 2:25 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:24 AM.

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