C++ Programming Help for Beginners: Questions from Dan Hawhorn's Book

  • Context: C/C++ 
  • Thread starter Thread starter peejake
  • Start date Start date
  • Tags Tags
    C++
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 3K views
peejake
Messages
69
Reaction score
0
hey people,

Im a beginner when it comes to programming and i have just stated C++..Im finding it a bit hard and boring but its only the beginning, so i think that it would get better as i move on...Anyway i need some help with these questions...

BTW for your info: I am using a book called "A gentle introduction to C++" by Dan Hawhorn...
So here are my questions:

Part A
1)Enter a number from the keyboard
2)Test the number to make sure it is a positive integer
3)If not, loop back and re enter a new number
4)Calculate the display the sum of all integers from 1...
5)e.g. If N = 8, then Sum = 1+2+3+4+5+6+7+8 = 36

Part B

1) Enter a number from the keyboard
2) Test the number to make sure it is a positive integer, less than 18
3) (otherwise the result will be too large)
4) If not, loop back to re enter a new number
5) Calculate and display the product of all integers from 1...N
6) e.g. If N = 5, then product = 1*2*3*4*5 = 120 = 5! = 5 factorial.

I would be most happy if anyone could help me with writing these programs...

The program i am using is Dev-C++, oh and i am progressing through those tutorials(stickies) on the top...

Thanks a lot
Jake:smile:
 
Physics news on Phys.org
Here's a general tip: Don't try to write the entire program at once! Do part of it, and test it, then move on to another part and test it, etc. For example, your Part A divides neatly into two sections that you can solve separately:

peejake said:
1)Enter a number from the keyboard
2)Test the number to make sure it is a positive integer
3)If not, loop back and re enter a new number

At this point, you should display the number that you finally got, with a 'count', so you can verify that it's OK. When you get this working, you can move on to the rest of it:

4)Calculate the display the sum of all integers from 1...
5)e.g. If N = 8, then Sum = 1+2+3+4+5+6+7+8 = 36
 
Ok here we go:

For part A the 2nd question:

Test the number to make sure it is a positive integer..


So far i have done this...

#include <iostream>

using namespace std;

int main()
{
int number;

count<<"Please input a number from the keyboard: ";
cin>> number;
cin.ignore();
}

It works right upto here...

Now i want to know how to assign to the program the point to make sure that it is a positive integer...SO what statement do i give for this?

Following the 3rd which says: If not, loop back to re enter a new number...im thinking for this that i have to use an IF statement but not too sure about it...

Hope that does it...
 
Last edited:
jtbell said:
Here's a general tip: Don't try to write the entire program at once! Do part of it, and test it, then move on to another part and test it, etc. For example, your Part A divides neatly into two sections that you can solve separately:
At this point, you should display the number that you finally got, with a 'count', so you can verify that it's OK. When you get this working, you can move on to the rest of it:

jtbell,

Thanks for that, but i already knew that rule...it is at this point where i think i need some help...i don't know what to enter to test to make sure whether the number is a positive integer or not following the looping part...
 
fargoth said:
I'd use a
do{...} while(-number)
loop.

That loop will not terminate as required. (Hint: in C, any non-zero value is true for Boolean purposes).

Cheers,
Tim
 
peejake said:
Ok here we go:

For part A the 2nd question:

Test the number to make sure it is a positive integer..


So far i have done this...

#include <iostream>

using namespace std;

int main()
{
int number;

count<<"Please input a number from the keyboard: ";
cin>> number;
cin.ignore();
}

It works right upto here...

Now i want to know how to assign to the program the point to make sure that it is a positive integer...SO what statement do i give for this?

Following the 3rd which says: If not, loop back to re enter a new number...im thinking for this that i have to use an IF statement but not too sure about it...

Hope that does it...

Ok so your program pulls in the number. So in order to loop back you'll have to actually use a loop which would include "for" "while" and "do while"

Using what you have there you could just put a while loop after it

while (test){
count<<"Please input a number from the keyboard: ";
cin>> number;
}

^ you can just put the same code you used to prep the loop
---------

From the sounds of it where i put (test) is where you're having the trouble?

As far as that goes, every even number divided by 2 is an even number. So you could do something like

while (num > 18 || (num % 2) > 0)

That way the loop will only go into effect if a number is greater than 18 or if the number has a remainder (which if it were even it wouldn't).