944,008 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1526
  • C++ RSS
May 10th, 2007
0

someone please help

Expand Post »
i'm developing a c++ program here and also i'm nwe to c++.
i have been given a program that has to print integers from 0-10 with their squares and cubes in a table form. but the problem is i'm only allowed to use if statement onyl to do this program.well this is how i did it.

#include<iostream>
using namespace std;
using std::cout;
using std::cin;
using std::endl;

int main()
{
int a;int b;int c;int d;int e;int f;int g;int h;int i;int j;int k;
int square;
int cube;

cout<<"enter any number"<<endl;
cin>>a;
square=a*a;
cube=a*a*a;

cout<<a<<"\t"<<square<<"\t"<<cube<<endl;

cin>>b;
square=b*b;
cube= b*b*b;

cout<<b<<"\t"<<square<<"\t "<<cube<<endl;

return 0;
}

i repeated the statement until i used all the variables above.
it appears like this

integer square cube
0 0 0
1
1 1 1
2
2 2 2

my problem is how to remove this numbers between because it has to appear like this

integer square cube
0 0 0
1 1 1
2 2 2
Reputation Points: 10
Solved Threads: 0
Light Poster
nkhosinathie is offline Offline
30 posts
since May 2007
May 10th, 2007
0

Re: someone please help

I imagine the problem was designed to force you to use a recursive solution. That's really the only way to loop without using a loop.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 10th, 2007
0

Re: someone please help

so how can i use the recursive loop because firstly i'm new to c++ programming and also this programming has to be done using if statement only.i'm prepared to listen and learn.i'm not here to copy any stqatements i'm here tyo learn
Reputation Points: 10
Solved Threads: 0
Light Poster
nkhosinathie is offline Offline
30 posts
since May 2007
May 10th, 2007
0

Re: someone please help

instead of writing
using std::cout;
using std::cin;
using std::endl;

you can just write
using namspace std;
Reputation Points: 11
Solved Threads: 2
Junior Poster
kylcrow is offline Offline
124 posts
since Apr 2007
May 10th, 2007
0

Re: someone please help

thanks i will do. but still whenever i compile this program it is leaving this numbers in between so i don't know how to deal with that.
Reputation Points: 10
Solved Threads: 0
Light Poster
nkhosinathie is offline Offline
30 posts
since May 2007
May 10th, 2007
0

Re: someone please help

Click to Expand / Collapse  Quote originally posted by kylcrow ...
instead of writing
using std::cout;
using std::cin;
using std::endl;

you can just write
using namspace std;
thanks i will do. but still whenever i compile this program it is leaving this numbers in between so i don't know how to deal with that.
Reputation Points: 10
Solved Threads: 0
Light Poster
nkhosinathie is offline Offline
30 posts
since May 2007
May 10th, 2007
0

Re: someone please help

>i'm prepared to listen and learn.i'm not here to copy any stqatements i'm here tyo learn


>and also this programming has to be done using if statement only
That's exactly why I think you're meant to use recursion. The only other way is to write the code inline:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. // Print the square and cube of 0
  4. // Print the square and cube of 1
  5. // Print the square and cube of 2
  6. ...
  7. // Print the square and cube of 9
  8. }
That doesn't use an if statement, and neither does your current solution. So my conclusion is that you need to approximate this:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. for ( int i = 0; i < 10; i++ ) {
  4. // Print the square and cube of i
  5. }
  6. }
Which can be done with a recursive function using an if statement as the base case:
C++ Syntax (Toggle Plain Text)
  1. void f ( int i )
  2. {
  3. if ( i < 10 ) {
  4. // Print the square and cube of i
  5. f ( i + 1 );
  6. }
  7. }
  8.  
  9. int main()
  10. {
  11. f ( 0 );
  12. }
>you can just write
>using namspace std;
That's generally considered a bad practice though because it opens up names that you don't use and defeats the purpose of namespaces in avoiding naming collisions.

>but still whenever i compile this program it is leaving this numbers in
>between so i don't know how to deal with that.
The numbers in between are the user input being echoed to the screen. You can avoid it by requiring the user to enter all numbers immediately and then simply not prompting further:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int x, y, z;
  4.  
  5. cout<<"Enter three numbers: ";
  6. cin>> x;
  7. cout<< x <<'\n';
  8. cin>> y;
  9. cout<< y <<'\n';
  10. cin>> z;
  11. cout<< z <<'\n';
  12. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 10th, 2007
0

Re: someone please help

Click to Expand / Collapse  Quote originally posted by Narue ...
>i'm prepared to listen and learn.i'm not here to copy any stqatements i'm here tyo learn


>and also this programming has to be done using if statement only
That's exactly why I think you're meant to use recursion. The only other way is to write the code inline:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. // Print the square and cube of 0
  4. // Print the square and cube of 1
  5. // Print the square and cube of 2
  6. ...
  7. // Print the square and cube of 9
  8. }
That doesn't use an if statement, and neither does your current solution. So my conclusion is that you need to approximate this:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. for ( int i = 0; i < 10; i++ ) {
  4. // Print the square and cube of i
  5. }
  6. }
Which can be done with a recursive function using an if statement as the base case:
C++ Syntax (Toggle Plain Text)
  1. void f ( int i )
  2. {
  3. if ( i < 10 ) {
  4. // Print the square and cube of i
  5. f ( i + 1 );
  6. }
  7. }
  8.  
  9. int main()
  10. {
  11. f ( 0 );
  12. }
>you can just write
>using namspace std;
That's generally considered a bad practice though because it opens up names that you don't use and defeats the purpose of namespaces in avoiding naming collisions.

>but still whenever i compile this program it is leaving this numbers in
>between so i don't know how to deal with that.
The numbers in between are the user input being echoed to the screen. You can avoid it by requiring the user to enter all numbers immediately and then simply not prompting further:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int x, y, z;
  4.  
  5. cout<<"Enter three numbers: ";
  6. cin>> x;
  7. cout<< x <<'\n';
  8. cin>> y;
  9. cout<< y <<'\n';
  10. cin>> z;
  11. cout<< z <<'\n';
  12. }
thanks so much for your help i will try it and i will definitely get back to u'thanks
Reputation Points: 10
Solved Threads: 0
Light Poster
nkhosinathie is offline Offline
30 posts
since May 2007
May 10th, 2007
0

Re: someone please help

thanks so much for your help i will try it and i will definitely get back to u'thanks
another thing i can ask if u don't mind, i'm also doing a bank account program but i will do it and come back to u,but what i want to know about it is the understanding of how i have to do.
Reputation Points: 10
Solved Threads: 0
Light Poster
nkhosinathie is offline Offline
30 posts
since May 2007
May 10th, 2007
0

Re: someone please help

>but what i want to know about it is the understanding of how i have to do.
That's a different problem and a different question, so create a new thread when you start working on it.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I have a questoin about 2Dimenional Array?
Next Thread in C++ Forum Timeline: frustrated newbie needs help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC