Skip to content
View in the app

A better way to browse. Learn more.

The Armory

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Help C++ program

Featured Replies

I have to write a program where I input a number greater than or equal to 1.I then have to display the square root and cube root of all the whole numbers from 1 to the number entered.I'm really stuck here, I have no idea how to setup my while statement.Can somebody help me out?
I can't remember the command for square root or cube root, so I'll just call it sqrt and thrt. 8)int original, x;float root2, root3;cout<<"Input an integer. The smaller the better."<<endl;cin>>original;x = original;clrscr(); //hope you know what this does. :lol:do { root2 = sqrt(x); root3 = thrt(x); cout<<"The square root of "<<x<<" is "<<root2<<endl; cout<<"The cube root of "<<x<<" is "<<root3<<endl; cout<<"Press enter to continue..."<<endl; /*<---you can omit this line*/ x = x - 1; getch(); /*<---but make sure to omit this one as well*/} while (x > 0)But I make no promises of it's correctness. Been more than a year since I've done C++ programming. :wink:BTW, what compiler are you using? I'm giving info based on the old Borland compiler for DOS. Also, before using getch() and clrscr() make sure you've initialized the proper library (conio.h) beforehand.
bah..... i use java. not c++ -G
  • Author
Wow,I read the whole chapter for the program i have to right and I have no idea what Hayabasa is talking about.I use MVS.net 2003
  • Author
Here's what I have so far.../*The user wishes to make a table of square roots and cube roots for whole numbers from one tosome upper value that the user chooses. Write a program that prompts and inputs the user'schoice of an upper limit. Then, from one to that number, display the current number, square rootand cube root of that current number. Display the results in columns as shown:Number Square Root Cube Root1 1.0000 1.00002 1.4142 .....and so on through the number entered by the user. Repeat the process until the user enters anegative number or a zero.*/#include <iostream>#include <cmath>#include <iomanip>using namespace std;int main(){ cout.setf (ios::fixed, ios::floatfield); cout.setf (ios::showpoint); char limit = 0; long squareRoot, cubeRoot; cout << "Enter a number greater than or equal to 1, enter 0 to quit: " cin >> limit; while (limit >=1 && number != 0) { cout << "Number Square Root Cube Root"; cout << number << setprecision(4) << squareRoot << cubeRoot; cout << endl << endl; cout << "Enter a number greater than or equal to 1, enter 0 to quit: " cin >> number; } return 0;}
  • Author
What I'm not understanding is how I output for all the numbers between 1 and my input number.

Replace this:

 

while (limit >=1 && number != 0)

 

{

 

cout << "Number Square Root Cube Root";

 

cout << number << setprecision(4) << squareRoot << cubeRoot;

 

cout << endl << endl;

 

cout << "Enter a number greater than or equal to 1, enter 0 to quit: "

 

cin >> number;

 

}

With this:

x = limit;

 

do

 

{

 

//here goes code to figure out square and cube root

 

cout << "Number Square Root Cube Root";

 

cout << number << setprecision(4) << squareRoot << cubeRoot;

 

x = x - 1; //this line is very important

 

}while (x > 0)

Basically, you would be better off with a do-while loop. But if you haven't gotten to that yet:

x = limit;

 

while (x > 0)

 

{

 

//here goes code to figure out square and cube root

 

cout << "Number Square Root Cube Root";

 

cout << number << setprecision(4) << squareRoot << cubeRoot;

 

x = x - 1; //this line is very important

 

}

 

//x will initially start as the number the user puts in. Then it will get 1 value smaller until it gets to 0 and stops.

wow it's been years since i took c++.i just found some of my old books,and looking throught them.and i guess it's not like riding a bike.i forgot most of all of it.so all i can offer is good luck :roll: :mrgreen:
  • Author
Thanks for the help Hayabusa.But I'm still not getting it...omg I'm seriously about ready to cryI don't even know how to find the cube root.I'm in class right now trying to put it all together but I don't get it...I have to write 2 more on the same subject and I can't even get the 1st one :LOL:
  • Author
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { cout.setf (ios::fixed, ios::floatfield); cout.setf (ios::showpoint); char limit = 0; double squareRoot, cubeRoot, x; cout << "Enter a number greater than or equal to 1, enter 0 to quit: "; cin >> limit; x = limit; do { squareRoot = sqrt(x);cubeRoot = pow(x, -3);//here goes code to figure out square and cube root cout << "Number Square Root Cube Root"; cout << limit << setprecision(4) << squareRoot << cubeRoot; x = x - 1; //this line is very important }while (x > 0);return 0; }I have this now but it is an infinite loop
.. pun what r you having trouble with.. finding the square root& cube root, or displaying your results??, or both... i dont know C++ but i cnahelp with the logic
What I'm not understanding is how I output for all the numbers between 1 and my input number.

try a while loop that says something like:

 

 

while(x<= input)

{

system.out.println(x);

x +1;

}

 

// that code wil print out all #'s less that or equal to the input # like this:

 

1

2

3

4

5

6

7

... ect

 

that the the code in java anway let me know if this helps...

 

I'll be on ventrilo tonight after 9:30 i can give you more help then if u want it

char limit = 0;

double squareRoot, cubeRoot, x;

Yeah, that's probably your problem right there. You want to make sure that x remains an integer-type variable. No idea why you chose "char" as a variable type, but it was a bad choice.

 

int x, limit;

double squareRoot, cubeRoot;

that's a better choice of variable types.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.