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++
Click For Summary

Discussion Overview

The discussion revolves around programming help for beginners in C++, specifically addressing questions from Dan Hawhorn's book "A Gentle Introduction to C++". The focus is on writing programs that involve user input, validation of positive integers, and calculations of sums and products.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant expresses difficulty with C++ programming and seeks help with specific tasks involving user input and calculations.
  • Another participant suggests showing thoughts on how to write the programs to facilitate assistance.
  • A participant advises breaking down the program into smaller parts for easier testing and debugging.
  • One participant shares a code snippet for inputting a number and asks how to validate that it is a positive integer.
  • Another participant recommends using a do-while loop for input validation, but later points out that the suggested loop may not terminate correctly.
  • A participant proposes using a while loop for re-entering the number based on a condition, indicating uncertainty about the correct condition to use.

Areas of Agreement / Disagreement

Participants generally agree on the approach of breaking down the programming tasks into smaller sections. However, there is no consensus on the specific implementation details for validating positive integers and looping back for re-entry, as different methods are suggested.

Contextual Notes

There are unresolved questions regarding the exact conditions for validating input and the proper use of loops in C++. Some participants express uncertainty about the logic needed for these tasks.

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:
 
Technology news on Phys.org
Can you show us your thoughts on how these programs might be written? We can help you, if you can show us where you're stuck.

- Warren
 
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).
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
Replies
12
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
Replies
14
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
69
Views
11K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
47
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K